Since v2, the AWS CDK has consolidated the stable parts of the Construct Library into a single package, aws-cdk-lib
. This means you no longer need to install and reference numerous individual packages for different AWS services—you can simply import them from the base package.
In a previous article, I shared a stack for deploying a static Jekyll site on AWS using S3 and CloudFront. The import section of that stack looked like this:
import * as s3 from "aws-cdk-lib/aws-s3";
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import * as acm from "aws-cdk-lib/aws-certificatemanager";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
import * as targets from "aws-cdk-lib/aws-route53-targets";
import * as s3deploy from "aws-cdk-lib/aws-s3-deployment";
Instead of this approach, the best practice is to use v2 imports:
import { aws_s3 as s3 } from "aws-cdk-lib";
import { aws_cloudfront as cloudfront } from "aws-cdk-lib";
import { aws_certificatemanager as acm } from "aws-cdk-lib";
import { aws_route53 as route53 } from "aws-cdk-lib";
import { aws_cloudfront_origins as origins } from "aws-cdk-lib";
import { aws_route53_targets as targets } from "aws-cdk-lib";
import { aws_s3_deployment as s3deploy } from "aws-cdk-lib";
I’ve been working to adopt this new import style, and I actually prefer it because I find it more readable. The ironic thing is that v2 was released about four years ago, and I didn’t start learning CDK until last year. I simply missed the memo (and most of the resources I learned from used the old import syntax).
Regardless, it’s worth adopting the current best practice!
I hope you find this useful!