- Newest
- Most votes
- Most comments
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
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'
}
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
Relevant content
- asked 2 years ago
