Lambda function to update other Lambda function when called via CloudFormation.
This repository contains two Lambda functions along with CloudFormation templates for each function.
- aws-cfn-lambda-updater: Function for updating an AWS Lambda function when triggered via AWS CLoudFormation.
- aws-cfn-lambda-updater-example-lambda: Simple “Hello World” function to demonstrate using aws-cfn-lambda-updater.
Deploying Both stacks can be done by clicking on "Launch Stack" below.
To use aws-cfn-lambda-updater to update another Lambda function that is deployed via CloudFormation, add a resource similar to the following to your CloudFormation template. You’ll need to add parameters for LambdaName and S3Bucket.
"LambdaInvokeUpdater": {
"Type": "Custom::LambdaUpdaterInvoke",
"Properties": {
"ServiceToken":{
"Fn::ImportValue": {
"Fn::Sub": "aws-cfn-lambda-updater-LambdaFunctionArn"
}
},
"FunctionName": {
"Ref": "LambdaName"
},
"S3Bucket": {
"Ref": "S3Bucket"
},
"S3Key": {
"Fn::Sub": "${LambdaName}.zip"
}
}
}
To ensure that the LambdaInvokeUpdater resource requires an update on each version, add a command like the following to your build process.
$ python -c "import json, sys; cfn=json.load(sys.stdin); cfn['Resources']['LambdaInvokeUpdater']['Properties']['CodeSha256'] = '$(openssl dgst -binary -sha256 dist/aws-cfn-lambda-updater.zip | openssl base64)'; json.dump(cfn, sys.stdout, indent=2)" < dist/aws-cfn-lambda-updater-example-lambda.json > dist/aws-cfn-lambda-updater-example-lambda.json.new; mv dist/aws-cfn-lambda-updater-example-lambda.json{.new,}
See this repository’s .travis.yml for an example in practice.