Upgrading my blog to CDK v2

I recently updated the CDK code that is used to provision AWS resources for this blog to CDK v2. I think it is worth looking at the process a bit to see what changes were made.

Package Differences

The first thing you should be aware of with CDK v2 is it consolidates most of the CDK resource packages into a single library called aws-cdk-lib. So the first, initial change, is to remove all @aws-cdk/aws-* resources from the package.json file. This is easiest to see in the GitHub diff here. A new package-lock.json file will need to be generated from these new depencies by running an npm install.

Once this is complete any imports using these libraries must be updated to import them correctly. Many of these changes go from looking like this:

1
import * as s3 from '@aws-cdk/aws-s3';

To this:

1
import * as s3 from 'aws-cdk-lib/aws-s3';

And also any libraries part of @aws-cdk/core will need to be imported individually from aws-cdk-lib. Here is an example of that with the Duration class.

1
import { Duration } from 'aws-cdk-lib';

If you references the classes as cdk.Duration they will need to be modifed to remove cdk to be just Duration.

The Construct class has also been moved into its own package resource constructs package. Any references to cdk.Construct will need to have both the imports and references updated.

cdk.json Changes

There are some feature flags included in the cdk.json file that will need to be removed as well. These were added to CDK v1 over time and have become defaults now with v2.

1
2
3
4
5
6
7
{
"app": "npx ts-node bin/infra.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true"
}
}

In this case both @aws-cdk/core:enableStackNameDuplicates and aws-cdk:enableDiffNoFail can be removed completely.

Deprecated Resources

Finally, some deprecated resources have been removed with v2 and will need to be updated to migrate. The best example I have of this is the removal of aliasConfiguration from the cloudfront resource.

In my case this was able to be replaced with the ViewerCertificate resource. That example can be found here

CDK Bootstrap

If you haven’t recently done it, most AWS accounts in use may need to be re-bootstrapped. This is due to additional dependencies added to the CDK bootstrap stack deployed to AWS accounts. More details can be found here. Luckily the fix if you encounter errors with the older v1 bootstrap is easy. A quick command below takes care of it:

1
cdk boostrap --profile <account_profile>

Conclusion

This is a relatively simple CDK application so the transition from CDK v1 to v2 was easy. I have done some more complex applications for my $DAYJOB and even more complex ones haven’t been too difficult. Overall I think the move to a single repo for most resources will be well received.