'Cannot add dependencies for sub packages in CDK V2 in Python

CDK v1 to v2 migration guide suggests adding requirements as below:

aws-cdk-lib>=2.0.0
constructs>=10.0.0
aws-cdk.aws-codestar-alpha>=2.0.0alpha1
...

I would like to add dependencies for CDK packages one by one as I was able to do in v1: aws-cdk.aws-lambda, aws-cdk.aws-apigateway and so on. However I found no way to do that in CDK v2. aws-cdk-lib.{service-name} would not work.

This results in a dependency layer file very large in size that it exceeds the 256MB dependency layer size limit in Lambda. So I am unable to update my stack after upgrading to v2.

Is there a way to exclude unused dependencies created by aws-cdk-lib?



Solution 1:[1]

The whole point of CDK v2 is to package all the modules together to avoid mismatched dependencies. Each CDK v2 version contains all the modules, excluding the experimental ones, which are still published separately with the alpha suffix.

So no, you cannot exclude any module from CDKv2, which is by design.

From the docs:

AWS CDK v2 consolidates the stable parts of the AWS Construct Library, including the core library, into a single package, aws-cdk-lib. Developers no longer need to install additional packages for the individual AWS services they use. This single-package approach also eliminates the need to synchronize the versions of the various CDK library packages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gshpychka