How to produce a Lambda in CloudFormation

You can produce a Lambda in CloudFormation as follows:

Alternative 1 – Inline code

 Resources:
MyLambdaFunction:
Type: AWS:: Lambda:: Function.
Residence:.
FunctionName: MyLambdaFunction.
Runtime: python3.8.
Handler: index.lambda _ handler.
Code:.
ZipFile: |
import json.

def lambda_handler( occasion, context):.
# Your Lambda function code here.
return {
' statusCode': 200,.
' body': json.dumps(' Hi from Lambda!').
}
Function:! GetAtt MyLambdaExecutionRole.Arn.

In this example, rather of defining the S3Bucket and S3Key residential or commercial properties under the Code area, you utilize the ZipFile residential or commercial property to supply the real code as a multiline string. The code is composed in Python and consists of a basic Lambda handler function.

Keep In Mind That there is a limitation to the size of the CloudFormation design template, so if your Lambda code is big or complicated, it’s typically suggested to keep it in an external place like an S3 container and recommendation it utilizing the S3Bucket and S3Key residential or commercial properties.

Alternative 2 – Consist of a Zip file of code

 Resources:.
MyLambdaFunction:.
Type: AWS:: Lambda:: Function.
Residence:.
FunctionName: MyLambdaFunction.
Runtime: python3.8.
Handler: index.lambda _ handler.
Code:.
S3Bucket: my-lambda-bucket.
S3Key: lambda-code. zip.
Function:! GetAtt MyLambdaExecutionRole.Arn.

Let’s break down the example:

  1. Resources: This area specifies the resources you wish to produce. In this case, you’re developing a Lambda function called MyLambdaFunction

  2. Type: AWS:: Lambda:: Function: This defines that you wish to produce a Lambda function resource.

  3. Characteristics: Here, you specify the residential or commercial properties of the Lambda function.

  • FunctionName: This sets the name of the Lambda function.
  • Runtime: Define the runtime environment for your function. In this example, we’re utilizing python3.8, however you can pick a various runtime.
  • Handler: Set the name of the file and the function within the file that must be performed when the Lambda function is conjured up.
  • Code: Define the place of the code for your Lambda function. In this example, we’re utilizing code kept in an S3 container.
  • Function: Offer the ARN (Amazon Resource Call) of an IAM function that gives needed approvals to the Lambda function.
    ! GetAtt MyLambdaExecutionRole.Arn: This obtains the ARN of an existing IAM function called MyLambdaExecutionRole You would require to specify this IAM function independently in your CloudFormation design template.

Ensure to change the worths according to your requirements. When you have actually specified this resource in your CloudFormation design template, you can release the design template to produce the Lambda function utilizing AWS CloudFormation.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: