Should anyone stumble upon this, I created a simple Node.js script to handle the CloudFront update.
It requires you capture the response of the aws lambda publish-version command into lambda_publish_response.json, and that you change the cloudFrontDistributionId variable, but everything else "should just work":
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);
});
});
Here is a Gist listing all the commands required to make this happen: https://gist.github.com/neonexus/3062b34b09896fa027e22d332dd65069