[ad_1]
You possibly can create a Lambda in CloudFormation as follows:
Possibility 1 – Inline code
Sources:
MyLambdaFunction:
Sort: AWS::Lambda::Perform
Properties:
FunctionName: MyLambdaFunction
Runtime: python3.8
Handler: index.lambda_handler
Code:
ZipFile: |
import json
def lambda_handler(occasion, context):
# Your Lambda perform code right here
return {
'statusCode': 200,
'physique': json.dumps('Hey from Lambda!')
}
Function: !GetAtt MyLambdaExecutionRole.Arn
On this instance, as an alternative of specifying the S3Bucket and S3Key properties underneath the Code part, you utilize the ZipFile property to supply the precise code as a multiline string. The code is written in Python and features a easy Lambda handler perform.
Keep in mind that there’s a restrict to the scale of the CloudFormation template, so in case your Lambda code is giant or advanced, it’s typically advisable to retailer it in an exterior location like an S3 bucket and reference it utilizing the S3Bucket and S3Key properties.
Possibility 2 – Embrace a Zip file of code
Sources:
MyLambdaFunction:
Sort: AWS::Lambda::Perform
Properties:
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 instance:
-
Sources: This part defines the sources you need to create. On this case, you’re making a Lambda perform namedMyLambdaFunction. -
Sort:AWS::Lambda::Perform: This specifies that you simply need to create a Lambda perform useful resource. -
Properties: Right here, you outline the properties of the Lambda perform.
FunctionName: This units the identify of the Lambda perform.Runtime: Specify the runtime setting to your perform. On this instance, we’re utilizingpython3.8, however you possibly can select a distinct runtime.Handler: Set the identify of the file and the perform throughout the file that ought to be executed when the Lambda perform is invoked.Code: Specify the placement of the code to your Lambda perform. On this instance, we’re utilizing code saved in an S3 bucket.Function: Present the ARN (Amazon Useful resource Title) of an IAM position that grants vital permissions to the Lambda perform.
!GetAtt MyLambdaExecutionRole.Arn: This retrieves the ARN of an present IAM position namedMyLambdaExecutionRole. You would want to outline this IAM position individually in your CloudFormation template.
Be sure to regulate the values in keeping with your necessities. Upon getting outlined this useful resource in your CloudFormation template, you possibly can deploy the template to create the Lambda perform utilizing AWS CloudFormation.
[ad_2]
