Skip to content

Instantly share code, notes, and snippets.

@neonexus
Last active January 28, 2022 13:39
Show Gist options
  • Save neonexus/3062b34b09896fa027e22d332dd65069 to your computer and use it in GitHub Desktop.
Save neonexus/3062b34b09896fa027e22d332dd65069 to your computer and use it in GitHub Desktop.
Update Lambda@Edge Function
const fs = require('fs');
const {exec} = require('child_process');
const cloudFrontDistributionId = 'EXXXXXXXXXXXXX';
const currentCloudFrontConfigFile = 'cf_config.json';
const updatedCloudFrontConfigFile = 'cf_config_updated.json';
const lambdaPublishResponseFile = 'lambda_publish_response.json';
exec(`aws cloudfront get-distribution-config --id ${cloudFrontDistributionId} > ${currentCloudFrontConfigFile}`, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return process.exit(1);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return process.exit(1);
}
if (!fs.existsSync(lambdaPublishResponseFile)) {
console.error('Run this first: `aws lambda publish-version --function-name LambdaFunctionName > lambda_publish_response.json`');
return process.exit(1);
}
let cfConfig = JSON.parse(fs.readFileSync(currentCloudFrontConfigFile));
const etag = cfConfig.ETag;
const lambdaPublishData = JSON.parse(fs.readFileSync(lambdaPublishResponseFile));
cfConfig.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items[0].LambdaFunctionARN = lambdaPublishData.FunctionArn;
fs.writeFileSync(updatedCloudFrontConfigFile, JSON.stringify(cfConfig.DistributionConfig));
exec(`aws cloudfront update-distribution --distribution-config file://${updatedCloudFrontConfigFile} --id ${cloudFrontDistributionId} --if-match ${etag}`, (error, stdout, stderr) => {
if (error) {
return console.error(`error: ${error.message}`);
}
if (stderr) {
return console.error(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
fs.unlinkSync(lambdaPublishResponseFile);
fs.unlinkSync(currentCloudFrontConfigFile);
fs.unlinkSync(updatedCloudFrontConfigFile);
});
});

First, zip up your function:

rm lambda.zip; zip -r lambda.zip lambda-function/*

Then, update the current working function:

aws lambda update-function-code --zip-file fileb://lambda.zip --function-name LambdaFunctionName

Next, publish the current working function, and capture the response:

aws lambda publish-version --function-name LambdaFunctionName > lambda_publish_response.json

Finally, run deploy-to-edge.js. It will take the response found in lambda_publish_response.json, and update the CloudFront Distribution specified to use the new version of the Lambda function.

@viki53
Copy link

viki53 commented Oct 15, 2020

I just stumbled upon this via a Google search that led me to this StackOverflow comment… you just saved my day!

I was gonna spend hours building this script when I ended up just copying and adapting it a bit (mostly to use process.env to get some variables like the CloudFront Distribution ID and the lambda function's name and add some process.exitCode = 1 to show the pipeline has failed).

THANK YOU SO MUCH!

@neonexus
Copy link
Author

@viki53 I'm glad I could help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment