I am using a Docker-based bundling option to install necessary dependencies and create a Lambda Layer. The deployment succeeds. If I try to deploy a few more times - the bundling is initiated, however, in the end, it states "No changes". And this is expected behavior, since I haven't changed anything, no changes are introduced.
Now the real hocus-pocus happens is when I run the deployment command in a CI/CD pipeline which installs a fresh environment every time. The CDK gets confused and even though no changes to the asset are introduced, it redeploys my layer every single time.
The Question What do I have to do in order to have deployments only when the changes are introduced to the asset?
When bundling artifacts CDK hashes the files in the artifact, if the hash doesn't match then it believes changes have been made and re-uploaded the artifacts.
You can either figure out what's causing the hash to rotate...if subsequent cdk deploy
invocations locally don't rotate the hash it's probably because of something you should be caching in your CI/CD pipeline (like the download of the dependencies)
OR
You can specify your own hash:
const sha = crypto.createHash('sha256');
const dirents = fs.readdirSync(codepath, { withFileTypes: true });
const filesNames = dirents
.filter(dirent => dirent.isFile())
.filter(dirent => new RegExp('(.*\.go)|(.*\.(mod|sum))$').test(dirent.name))
.map(dirent => dirent.name);
for (const file of filesNames) {
const data = fs.readFileSync(path.join(codepath, file));
sha.update(`<file name=${file}>`);
sha.update(data);
sha.update('</file>');
}
const codeHash = sha.digest('hex');
lambda.Code.fromAsset(basePath, {
assetHashType: cdk.AssetHashType.CUSTOM,
assetHash: codeHash,
bundling: { // whatever you're doing today