Home Software Engineering The way to create a Lambda in CloudFormation

The way to create a Lambda in CloudFormation

0
The way to create a Lambda in CloudFormation

[ad_1]

You’ll be able to 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('Howdy from Lambda!')
              }
      Position: !GetAtt MyLambdaExecutionRole.Arn

On this instance, as a substitute of specifying the S3Bucket and S3Key properties beneath 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 operate.

Keep in mind that there’s a restrict to the dimensions of the CloudFormation template, so in case your Lambda code is massive or complicated, it’s typically really helpful 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 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 on your operate. On this instance, we’re utilizing python3.8, however you may select a unique runtime.
  • Handler: Set the title of the file and the operate throughout the file that ought to be executed when the Lambda operate is invoked.
  • Code: Specify the placement of the code on your Lambda operate. On this instance, we’re utilizing code saved in an S3 bucket.
  • Position: Present the ARN (Amazon Useful resource Title) of an IAM function that grants essential 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.

Be sure that to regulate the values in response to 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.

[ad_2]