MORTIMERDEV LABS / AWS / CDK FREE · BETA
CDK in TypeScript ⇄ Python.
Paste one, get the other.
Found the perfect CDK example in the wrong language? Paste it here. Imports, constructs, props, method names, enums, template strings and stack classes are translated; anything it can’t translate reliably, like loops or arrow functions, is kept as a TODO comment and flagged, never guessed.
Translate
What is the CDK?
The AWS Cloud Development Kit (CDK) lets you define
infrastructure in a programming language instead of YAML. Your code
builds a tree of constructs, and
cdk synth turns it into CloudFormation templates that
cdk deploy hands to CloudFormation. The same library is
published for TypeScript, Python, Java, C# and Go, so an example in one
language maps closely onto the others.
- Construct class
- What to create (L2 here; Cfn* would be L1)
- Scope
- Where it lives in the tree: self in Python
- Id
- Unique in its scope; part of the logical ID
- Props
- Settings: keyword arguments in Python
Where you’ll use it
- Following AWS examples, which are often in only one language.
- Joining a team that uses the other language.
- The other Labs AWS tools, which give CDK in both.
Easy to confuse
- Names change, classes don’t:
grantReadbecomesgrant_read, butBucketandBLOCK_ALLstay the same. - Props become keyword arguments in Python, not a dict.
- Ids aren’t names: the bucket’s real name is generated
unless you set
bucketName.
Read more: AWS CDK developer guideWorking with the CDK in PythonWorking with the CDK in TypeScriptCDK API reference
TEST YOURSELF
How well do you know the AWS CDK?
Ten questions on apps, stacks, constructs, L1/L2/L3, synth, diff, deploy, bootstrap, environments and context, with an explanation and a link to the reference after each one. Three ask you to translate a line, checked by the translator.
AWS CDK REFERENCE
The CDK, in plain English.
Building blocks
AppThe root of a CDK program. It holds one or more stacks and is what cdk synth runs.
Your entry file (bin/app.ts or app.py) creates the App, adds stacks to it and calls app.synth() (Python) or lets the CLI synthesise it.
StackOne CloudFormation stack: the unit you deploy. Everything inside it is created, updated or deleted together.
Split stacks by lifecycle (a database that rarely changes vs. an API you deploy daily) and by environment. Stacks can share values through references, which CDK turns into exports or parameters.
ConstructAny building block in the tree: a bucket, a function, or your own group of resources. Every construct has a scope, an id and optional props.
new X(scope, id, props) in TypeScript, X(scope, id, **props) in Python. The id must be unique within its scope; CDK combines ids along the path to make logical IDs, so renaming an id replaces the resource.
L1, L2 and L3L1 (Cfn*) map one-to-one to CloudFormation; L2 add sensible defaults and helpers; L3 (patterns) combine several resources.
s3.CfnBucket is L1, s3.Bucket is L2 with grant methods and secure defaults, and ecs_patterns.ApplicationLoadBalancedFargateService is L3. Drop to L1 (or an escape hatch) only for settings an L2 doesn’t expose.
TokensPlaceholders for values only known at deploy time, like bucket.bucketName, that look like strings in your code.
You can pass them around and put them in strings, but you can’t inspect them in code (for example with if or string methods) because the real value doesn’t exist until CloudFormation runs.
Commands
cdk synthRuns your app and writes the CloudFormation templates and assets to cdk.out without deploying anything.
Good for CI: if synth fails, the code is broken. Reviewing the template shows exactly what will be created.
cdk diffCompares your app with what’s deployed and shows the changes, including IAM and security group changes.
Run it before every deploy. Watch for resources that will be replaced, which can mean data loss for stateful resources.
cdk deploySynthesises and deploys stacks through CloudFormation, uploading assets first.
It asks before broadening security permissions unless you pass --require-approval never. CloudFormation rolls back a failed update.
cdk bootstrapOne-time setup per account and region: creates the CDKToolkit stack with an assets bucket, a container registry and deployment roles.
Deploys fail with a bootstrap error until it’s done. Re-run it after CDK upgrades that need a newer bootstrap version, and once for each account and region you deploy to.
Configuration
EnvironmentsThe account and region a stack deploys to, set with env: { account, region }.
Without env a stack is environment-agnostic: it can deploy anywhere but can’t look things up (like an existing VPC). Use CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION for the CLI’s current credentials.
ContextKey–value settings from cdk.json, cdk.context.json or --context, read with node.tryGetContext().
Lookups such as availability zones and VPCs are cached in cdk.context.json. Commit that file so synth gives the same result on every machine.
Construct ids and logical IDsCloudFormation logical IDs come from the construct path plus a hash, so moving or renaming a construct replaces its resource.
Keep ids stable once deployed. For stateful resources, renaming can mean a new, empty database or bucket.
TypeScript and Python
camelCase and snake_caseProperties and methods are camelCase in TypeScript and snake_case in Python: bucketName → bucket_name, grantRead → grant_read.
Class names (Bucket), enum members (BLOCK_ALL) and static method namespaces stay the same. Acronyms don’t always survive the trip: enableXRay is enable_x_ray, enforceSSL is enforce_ssl.
Props objects and keyword argumentsTypeScript passes a props object as the last argument; Python passes the same props as keyword arguments.
new s3.Bucket(this, "B", { versioned: true }) is s3.Bucket(self, "B", versioned=True). Nested props are objects in TypeScript and dicts (or struct classes) in Python.
this, self and newThe scope is this in TypeScript and self in Python, and Python has no new keyword.
Inside a stack, constructs are created with this (or self) as their scope. Python reserves some module names, so aws_lambda is usually imported as lambda_.
Importsaws-cdk-lib/aws-s3 in TypeScript is aws_cdk.aws_s3 in Python; constructs is the same in both.
import * as s3 from "aws-cdk-lib/aws-s3" is from aws_cdk import aws_s3 as s3. Experimental alpha modules are separate packages in both languages.
Honest about its limits.
It translates the shapes most CDK code uses. Control flow, functions, comprehensions and spreads are kept as TODO comments with a warning, so nothing is silently wrong. It doesn’t know CDK’s types, so check acronyms and Python struct classes.
Nothing leaves this page.
Your code is translated in your browser and nothing is saved. We count anonymous page views and button use, never what you paste.
Beta feedback helps shape Labs. Tell Andy what would help
Learn Step 3 of 3 in AWS foundations for web developers
