DEV Community

desawsume
desawsume

Posted on

How to upgrade AWS CDK stacks from CDKv1 to v2

Background

On June 1, 2023, AWS CDK version 1 will reach the end-of-support. AWS CDK Maintenance Policy

The Old Way

With CDKv1, we defined our CDK app packages through a requirements.txt file

aws-cdk.core=={cdk_version1}",
aws-cdk.aws_codebuild=={cdk_version1}",
aws-cdk.aws_cloudtrail=={cdk_version1}",
aws-cdk.aws_codepipeline=={cdk_version1}",
aws-cdk.aws_codepipeline_actions=={cdk_version1}",
aws-cdk.aws_events=={cdk_version1}",
aws-cdk.aws_events_targets=={cdk_version1}",
aws-cdk.aws_iam=={cdk_version1}",
aws-cdk.aws_lambda_event_sources=={cdk_version1}",
aws-cdk.aws_lambda=={cdk_version1}",
aws-cdk.aws_s3=={cdk_version1}",
aws-cdk.aws_kms=={cdk_version1}
aws-cdk.aws-ec2==cdk_version1
aws-cdk.aws-elasticloadbalancingv2==cdk_version1
aws-cdk.aws-lambda==cdk_version1

boto3
pytest
-e .
Enter fullscreen mode Exit fullscreen mode

It could potentially be blocked by the package manager and we might get overwhelmed by the aws-cdk packages name.

CDK version 2

Now, CDK and all stable constructs are now combined in 1 package/module. Experimental modules still need to be installed one by one.

It makes things much easier with aws-cdk-lib

Preparation

To make changes easier, I would create a virtual environment isolated it from my CDKv1, because most my CDK apps are written in python.

python3 -m venv .cdkv2

And activate your new virtual environment and run pip install to install the cdk v2 lib

source .cdkv2/bin/activate

Note: Move all the old aws-cdk v1 package to another txt file, for example - requirements-dev.txt
Image description

The requirement.txt look similar to below depending on your CDK version, use cdk --version to find out.

aws-cdk-lib==2.15.0
constructs>=10.0.0,<11.0.0
Enter fullscreen mode Exit fullscreen mode

The cdk.json needs to be adjusted as well, some of the options in CDK v1 are now deprecated, which also simplify a lot in the cdk.json file

overall, it looks something like below:

{
  "app": "python app.py",
  "context": {
    "@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": false,
    "@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": false,
    "@aws-cdk/aws-rds:lowercaseDbIdentifier": false,
    "@aws-cdk/core:stackRelativeExports": false,
  }
}
Enter fullscreen mode Exit fullscreen mode

## Change the imports

All the mature and stable release are in the aws-cdk-lib. Most cdk apps only need to change import statements

Example that I have -

from aws_cdk import core
from aws_cdk.core import Tags
import aws_cdk.aws_iam as iam
import aws_cdk.aws_lambda as _lambda 
Enter fullscreen mode Exit fullscreen mode

Change to

from constructs import Construct

from aws_cdk import (
    aws_iam as iam,
    aws_lambda as _lambda,
    Stack,
    CfnOutput,
    Tags
)
Enter fullscreen mode Exit fullscreen mode

now re-bootstrap the env to the account by simply run

cdk bootrap
Enter fullscreen mode Exit fullscreen mode

Lukcy that I don't have any error, I saw other people have issue with cdk bootrap version conflict.

Top comments (0)