Skip to content

CloudFormation Template for Lambda

0

I am working on a product that uses lambda, I want to create lambda function and attach the code file(.jar) at the same time using cloud formation, can you provide the different options to do that other than inline code.

3 Answers
1

Hello.

If you want to create a Lambda function with CloudFormation, you can use AWS SAM to make it a little easier.
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html

You can deploy the code by creating a template like the one below and placing Lambda's lambda_function.py in "lambda_function/".

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Sample SAM Template for waf rule create and delete

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

    # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig.
    LoggingConfig:
      LogFormat: JSON

Resources:
  Function:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      Handler: lambda_handler
      Runtime: python3.12
      CodeUri: lambda_function/
      Role: 'arn:aws:iam::11111111111:role/lambda'
      Architectures:
      - x86_64

If you want to deploy Lambda directly with CloudFormation instead of AWS SAM, the following template will be used.

  Lambda:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          import json
          def lambda_handler(event, context):
              # TODO implement
              print(event)
              return {
                  'statusCode': 200,
                  'body': json.dumps('Hello from Lambda!')
              }
      FunctionName: lambda
      Handler: lambda_function.lambda_handler
      Role: arn:aws:iam::11111111111:role/lambda
      Runtime: python3.12
      Timeout: 30
EXPERT
answered 2 years ago
AWS
EXPERT
reviewed 2 years ago
0

Besides ZIP file, you can embed your code inside CloudFormation as a inline function

Example snippet below

  lambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: MyLambdaFunction
      Handler: "index.lambda_handler"
      MemorySize: 128
      Runtime: python3.12
      Timeout: 900
      Architectures: 
         - arm64
      Code:
        ZipFile: |         
          import os
          import json
          import boto3

          def lambda_handler(event, context): 

            return {
              'statusCode': 200,
              'body': 'hello world'
            }
AWS
EXPERT
answered 2 years ago
0

You can use sam with combination of maven to create the jar before uploading the template. Here is example project that uses SAM and Maven. You can use it as a reference https://github.com/MichaelShapira/ConfluenceForAwsAppFlow

AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.