How to convert a CDK app from Python to TypeScript
- Add your codePaste it into the AWS CDK (Python) editor, drag in a file, or keep the sample to see how it works.
- Convert to TypeScriptClick 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.
Moving CDK code from Python to TypeScript
TypeScript is the CDK's native language, so every construct your Python app uses has a direct TypeScript equivalent. Keyword arguments such as removal_policy=RemovalPolicy.RETAIN become props objects like { removalPolicy: RemovalPolicy.RETAIN }, self becomes this, and from aws_cdk import aws_s3 as s3 becomes import * as s3 from "aws-cdk-lib/aws-s3".
Teams usually switch to get the TypeScript compiler's type checking, the wider pool of CDK examples, or one language shared with their application code. As long as the stack names and construct IDs stay the same, the synthesized CloudFormation template matches the one you deployed from Python.
Set the project up with cdk init app --language typescript, then copy the converted stack into lib/. AWS's guide to working with CDK in TypeScript covers the build and cdk.json details.
Tips for better results
- Keep construct IDs identical.
"NightlyReport"must stay"NightlyReport", or CloudFormation sees a new resource and replaces the old one. - Let the compiler help. Run
npx tsc --noEmitafter converting. Wrong prop names and missing required props show up as type errors. - Convert helper modules too. Python helpers that build construct props need their own TypeScript versions, so paste them separately.
- Check
Durationand enum calls.Duration.minutes(5)is the same in both languages, but Python-only helpers must be replaced. - Confirm with
cdk diff. Oncecdk.jsonruns the TypeScript app, the diff against the deployed stack should be empty.
Frequently asked questions
Will moving the CDK app to TypeScript redeploy my resources?
Only if something changes in the construct tree. With the same stack names and construct IDs, the template is the same and cdk diff shows no resource changes.
How are Python keyword arguments converted?
Each construct's keyword arguments become a single props object with camelCase keys. For example, timeout=Duration.minutes(5) becomes timeout: Duration.minutes(5) inside the props.
Do I need to call app.synth() in TypeScript?
The default TypeScript template doesn't call it, because the CDK CLI synthesizes the app when it runs. Leaving a call in is harmless.
What command does cdk.json run for TypeScript?
The default template runs the entry file in bin/ with ts-node, for example npx ts-node --prefer-ts-exts bin/app.ts. Update cdk.json to point at your new entry file.