I want to deploy and update Lambda function using CloudFormation stacks.
My Stack creation workflows are:
- upload my Lambda function code(zip file) to S3
- Use the s3 bucket(where the lambda function code is stored) and the LambdaFileName(zip file name) as CloudFormation parameters to create CloudFormation stacks by deploying the CloudFormation template
However, I have no idea how to update the Lambda function using the template. I have tried to upload the new function code to S3 and save as a new file name. And use the new file as parameters to update stacks. But CloudFormation can not detect any changes due to the file is a zip file.
My questions:
- How to update Lambda function using CloudFormation template
- Should I use
AWS::Lambda::FunctionorAWS::Serverless::Functionresources? I want to have version control supported.
Thank you.
My CloudFormation template is as below.
AWSTemplateFormatVersion: "2010-09-09"
Metadata: ""
Description: ""
Parameters:
LambdaS3:
Description: Api Gateway Authorizer Lambda S3Bucket Name
Type: String
Lambdafilename:
Description: Api Gateway Authorizer Lambda file Name (Latest)
Type: String
Transform: AWS::Serverless-2016-10-31
Resources:
# LambdaFunction:
# DeletionPolicy: "Delete"
# Type: "AWS::Lambda::Function"
# Properties:
# Description: ""
# FunctionName: "LambdaFunction"
# Handler: "lambda_function.lambda_handler"
# Code:
# S3Bucket: !Ref LambdaS3
# S3Key: !Sub '${Lambdafilename}.zip'
# MemorySize: 512
# Role: !GetAtt IAMRole2.Arn
# Runtime: "python3.8"
# Timeout: 20
# TracingConfig:
# Mode: "PassThrough"
LambdaFunction:
DeletionPolicy: "Delete"
Type: "AWS::Serverless::Function"
Properties:
Description: ""
FunctionName: "LambdaFunction"
Handler: "lambda_function.lambda_handler"
CodeUri:
Bucket: !Ref LambdaS3
Key: !Sub '${Lambdafilename}.zip'
MemorySize: 512
Role: !GetAtt IAMRole2.Arn
Runtime: "python3.8"
Timeout: 20
Tracing: "PassThrough"
AutoPublishAlias: live
DeploymentPreference:
Type: Linear10PercentEvery10Minutes