16.3 C
London
Monday, September 9, 2024

Learn how to create a Lambda in CloudFormation


You possibly can create a Lambda in CloudFormation as follows:

Choice 1 – Inline code

Assets:
  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('Hey from Lambda!')
              }
      Function: !GetAtt MyLambdaExecutionRole.Arn

On this instance, as an alternative of specifying the S3Bucket and S3Key properties beneath the Code part, you employ the ZipFile property to offer 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 dimensions of the CloudFormation template, so in case your Lambda code is giant or complicated, it’s usually really useful to retailer it in an exterior location like an S3 bucket and reference it utilizing the S3Bucket and S3Key properties.

Choice 2 – Embody a Zip file of code

Assets:
  MyLambdaFunction:
    Kind: 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:

  1. Assets: This part defines the sources you wish to create. On this case, you’re making a Lambda operate named MyLambdaFunction.

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

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

  • FunctionName: This units the title of the Lambda operate.
  • Runtime: Specify the runtime setting to your operate. On this instance, we’re utilizing python3.8, however you may select a special runtime.
  • Handler: Set the title of the file and the operate inside the file that ought to be executed when the Lambda operate is invoked.
  • Code: Specify the situation of the code to your Lambda operate. On this instance, we’re utilizing code saved in an S3 bucket.
  • Function: Present the ARN (Amazon Useful resource Identify) of an IAM function that grants vital permissions to the Lambda operate.
    !GetAtt MyLambdaExecutionRole.Arn: This retrieves the ARN of an present IAM function named MyLambdaExecutionRole. You would wish to outline this IAM function individually in your CloudFormation template.

Make sure that to regulate the values in accordance with your necessities. After you have outlined this useful resource in your CloudFormation template, you may deploy the template to create the Lambda operate utilizing AWS CloudFormation.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here