How to develop a Lambda in CloudFormation

You can develop a Lambda in CloudFormation as follows:

Alternative 1 – Inline code

 Resources:
MyLambdaFunction:
Type: AWS:: Lambda:: Function.
Characteristic:.
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(' Hey there from Lambda!').
}
Function:! GetAtt MyLambdaExecutionRole.Arn.

In this example, rather of defining the S3Bucket and S3Key homes under the Code area, you utilize the ZipFile home to supply the real code as a multiline string. The code is composed in Python and consists of an easy Lambda handler function.

Bear 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 advised to save it in an external place like an S3 pail and referral it utilizing the S3Bucket and S3Key homes.

Alternative 2 – Consist of a Zip file of code

 Resources:.
MyLambdaFunction:.
Type: AWS:: Lambda:: Function.
Characteristic:.
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 develop. In this case, you’re developing a Lambda function called MyLambdaFunction

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

  3. Characteristics: Here, you specify the homes 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 carried out 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 saved in an S3 pail.
  • Function: Offer the ARN (Amazon Resource Call) of an IAM function that approves needed authorizations to the Lambda function.
    ! GetAtt MyLambdaExecutionRole.Arn: This recovers the ARN of an existing IAM function called MyLambdaExecutionRole You would require to specify this IAM function individually in your CloudFormation design template.

Make certain 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 develop 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: :???: :?: :!: