create a Lambda in CloudFormation


You possibly can create a Lambda in CloudFormation as follows:

Possibility 1 – Inline code

Sources:
  MyLambdaFunction:
    Kind: AWS::Lambda::Perform
    Properties:
      FunctionName: MyLambdaFunction
      Runtime: python3.8
      Handler: index.lambda_handler
      Code:
        ZipFile: |
          import json

          def lambda_handler(occasion, context):
              # Your Lambda operate code right here
              return {
                  'statusCode': 200,
                  'physique': json.dumps('Whats up from Lambda!')
              }
      Position: !GetAtt MyLambdaExecutionRole.Arn

On this instance, as a substitute of specifying the S3Bucket and S3Key properties below the Code part, you employ the ZipFile property to supply the precise code as a multiline string. The code is written in Python and features a easy Lambda handler operate.

Bear in mind that there’s a restrict to the scale of the CloudFormation template, so in case your Lambda code is massive or advanced, it’s usually beneficial 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:
    Kind: AWS::Lambda::Perform
    Properties:
      FunctionName: MyLambdaFunction
      Runtime: python3.8
      Handler: index.lambda_handler
      Code:
        S3Bucket: my-lambda-bucket
        S3Key: lambda-code.zip
      Position: !GetAtt MyLambdaExecutionRole.Arn

Let’s break down the instance:

  1. Sources: This part defines the assets you need to create. On this case, you’re making a Lambda operate named MyLambdaFunction.

  2. Kind: AWS::Lambda::Perform: This specifies that you just need to create a Lambda operate useful resource.

  3. Properties: Right here, you outline the properties of the Lambda operate.

  • FunctionName: This units the identify of the Lambda operate.
  • Runtime: Specify the runtime atmosphere in your operate. On this instance, we’re utilizing python3.8, however you possibly can select a unique runtime.
  • Handler: Set the identify of the file and the operate throughout the file that must be executed when the Lambda operate is invoked.
  • Code: Specify the situation of the code in your Lambda operate. On this instance, we’re utilizing code saved in an S3 bucket.
  • Position: Present the ARN (Amazon Useful resource Identify) of an IAM function that grants needed permissions to the Lambda operate.
    !GetAtt MyLambdaExecutionRole.Arn: This retrieves the ARN of an current IAM function named MyLambdaExecutionRole. You would want to outline this IAM function individually in your CloudFormation template.

Make sure that to regulate the values in keeping with your necessities. After getting outlined this useful resource in your CloudFormation template, you possibly can deploy the template to create the Lambda operate utilizing AWS CloudFormation.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles