How to convert a CDK app from TypeScript to Python
- Add your codePaste it into the AWS CDK (TypeScript) editor, drag in a file, or keep the sample to see how it works.
- Convert to PythonClick the button or press Ctrl + Enter. The result streams into the right-hand editor.
- Review and use itCopy or download the result, then test it before you rely on it. AI output is a strong first draft, not a guarantee.
What changes when CDK code moves to Python
The AWS CDK is written in TypeScript and published to Python through jsii, so the same constructs exist in both languages. The conversion is mostly syntax: new Table(this, "Orders", { partitionKey }) becomes dynamodb.Table(self, "Orders", partition_key=...), camelCase props become snake_case keyword arguments, and aws-cdk-lib/aws-lambda becomes aws_cdk.aws_lambda, usually imported as lambda_ because lambda is a Python keyword.
The part that matters most for a deployed app is the construct IDs. CloudFormation logical IDs are built from each construct's ID and path, so if the Python app keeps the same stack names and construct IDs, deploying it updates the existing stack instead of replacing resources. cdk diff against the deployed stack should show no resource changes.
AWS's guide to working with the CDK in Python covers the virtual environment and the cdk.json app command for the new project.
Tips for better results
- Keep every construct ID unchanged. Renaming
"OrderQueue"to"order_queue"changes the logical ID and makes CloudFormation replace the queue. - Check third-party constructs. Libraries built with jsii publish Python packages. Constructs written only for TypeScript have to be rewritten.
- Convert one stack per paste. Keep shared constructs in their own module and convert them separately.
- Confirm with
cdk diff. After switching theappcommand incdk.json, a diff against the deployed stack should show no resource changes. - Port the tests. TypeScript Jest assertions translate to pytest with
aws_cdk.assertions.Template.
Frequently asked questions
Will switching the CDK app to Python replace my deployed resources?
Not if the stack names and construct IDs stay exactly the same. CloudFormation logical IDs come from the construct tree, so an unchanged tree produces the same template. Run cdk diff before deploying to confirm.
Why does the Python code use <code>lambda_</code>?
lambda is a reserved word in Python, so the CDK module is imported as from aws_cdk import aws_lambda as lambda_. The classes are identical to the TypeScript ones.
Do all CDK construct libraries work in Python?
Libraries built with jsii, including aws-cdk-lib and many Construct Hub packages, publish Python bindings. A construct written only in TypeScript for your project needs converting too.
What do I change in cdk.json?
Point the app command at the Python entry point, typically python3 app.py, and run it from a virtual environment with aws-cdk-lib and constructs installed.