Skip to content

Instantly share code, notes, and snippets.

@sterlingwes
Created August 14, 2023 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sterlingwes/08ec3a68330beb2d34298c7974d2a89e to your computer and use it in GitHub Desktop.
Save sterlingwes/08ec3a68330beb2d34298c7974d2a89e to your computer and use it in GitHub Desktop.
Script for diffing code asset change for lambda function from AWS CDK diff

Written in typescript and can be run with ts-node. You'll also need colordiff (brew install colordiff). Assumes much about the cdk.out file structure and CLI output format which may certainly change. This should give you two prompts: one for your environment (and the stack to run the diff for), and one showing you a list of lambda functions where assets have changed for which you can run a colordiff.

Replace the inline stack names, in this case mine are BackgroundJobsStackStaging and one for prod.

Useful for understanding the impact of deploying bundled code when it's changed. In my case, my lambda code bundles share common code with my API so it can change quite often with no impact on the lambda itself necessarily.

import { execSync, spawnSync } from 'child_process';
import { prompt } from 'enquirer';
const resourceType = 'AWS::Lambda::Function';
const addSomeSpace = () => {
console.log(' ');
console.log(' ');
};
const run = async () => {
const envPrompt: { env: string } = await prompt({
type: 'select',
name: 'env',
message: 'Choose your env',
choices: ['staging', 'production'],
});
const stackName =
envPrompt.env === 'staging'
? 'BackgroundJobsStackStaging'
: 'BackgroundJobsStackProduction';
console.log(`running cdk diff for ${stackName}...`);
addSomeSpace();
const result = execSync(
`node_modules/.bin/cdk diff --ci ${stackName}`,
).toString();
const lines = result.split(/\n/);
const resources = lines.filter((line) => line.includes(resourceType));
if (!resources.length) {
console.log('Diff has no lambda resources with code assets to diff');
process.exit(0);
}
addSomeSpace();
const jobFunctions = resources.map((resource) => {
const [, jobFunctionWithHashName] = resource.split(`${resourceType} `);
const [jobFunctionName] = jobFunctionWithHashName.split(' ');
return jobFunctionName;
});
const jobFunction: { name: string } = await prompt({
type: 'select',
name: 'name',
message: 'Choose your job function',
choices: jobFunctions,
});
const job = jobFunction.name;
const jobIdx = lines.findIndex((line) => line.includes(job));
if (jobIdx === -1) {
console.log(`Could not match provided job name (${job}) with output`);
process.exit(1);
}
const [, firstAsset] = lines[jobIdx + 7].split('] ');
const [, secondAsset] = lines[jobIdx + 8].split('] ');
const firstFile = `cdk.out/${firstAsset}/index.js`;
const secondFile = `cdk.out/${secondAsset}/index.js`;
console.log(
`running diff for ${job} between ${firstFile} and ${secondFile}...`,
);
addSomeSpace();
spawnSync('colordiff', [firstFile, secondFile], {
stdio: 'inherit',
});
addSomeSpace();
};
run().catch((e) => console.log(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment