We have a lambda@edge function which listens to cloudfront distribution origin request
and response
events. We are trying to automate the deployment. So far we succeeding in updating the code and publishing the new version.
- npm install
- zip -r lambda.zip *
- aws lambda update-function-code --function-name LambdaFunction1 --zip-file fileb://lambda.zip
- aws lambda publish-version --function-name LambdaFunction1
But how do we update CloudFront triggers to point to the latest published version?
To change the runtime, you create a new container image. When you use a . zip file archive for the deployment package, you choose a runtime when you create the function. To change the runtime, you can update your function's configuration.
Using our solution, the pipeline automatically updates the Lambda code for you without requiring you to copy, compress, and upload . zip files to Amazon S3.
Perform the following steps-
aws lambda list-versions-by-function --function-name LAMBDA_NAME
aws cloudfront get-distribution-config --id DISTRIBUTION_ID > cf_config.json
Create a file named updated_cf_config.json
by fetching DistributionConfig key from the cf_config.json.
Now Put the FunctionARN of the latest Version of lambda inside the "LambdaFunctionAssociations" -> "LambdaFunctionARN"
Update cloudfront distribution by running the command. To update the cloudfront distribution we need ETAG from cf_config.json
:
aws cloudfront update-distribution --distribution-config file://cf_config.json --id DISTRIBUTION_ID --if-match ETAG
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With