In some cases the example code in the AWS CDK documentation is available only in TypeScript. This topic describes how to read TypeScript code and translate it into Python. This is currently the only other stable programming language that the AWS CDK supports (the AWS CDK supports a developer preview version of Java and C#/.NET). See Hello World Tutorial for an example of creating an AWS CDK app in a supported language.
Both TypeScript and Python support namespaced module imports and selective imports. Module names in Python look like **aws_cdk.**xxx, where xxx represents an AWS service name, such as s3 for Amazon S3 (we'll use Amazon S3 for our examples). Replace the dashes in the TypeScript module name with underscores to get the Python module name.
The following is how you import the entire Amazon S3 module or just a Stack
class in both languages.
TypeScript | Python |
---|---|
// Import entire module |
# Import entire module |
Classes have the same name in TypeScript and in Python. TypeScript uses new to instantiate classes, whereas in Python you call the class object directly, like a function. Props objects at the end of an argument list become keyword-only arguments in Python, and their names become snake_case
. The keyword this in TypeScript translates to self in Python.
The following table shows how you can translate TypeScript class instantiations to Python class instantiations.
TypeScript | Python |
---|---|
// Instantiate Bucket class |
# Instantiate Bucket class |
// Instantiate Bucket with props |
# Instantiate Bucket with props |
Methods names and argument names in TypeScript are camelCased
, whereas in Python they are snake_cased
. Props objects at the end of an argument list in TypeScript are translated into keyword-only arguments in Python, and their names become snake_case
.
The following table shows how you can translate TypeScript methods to Python methods.
TypeScript | Python |
---|---|
// Call method |
# Call method |
Enum constants are scoped to a class, and have uppercase names with underscores in both languages (sometimes referred to as SCREAMING_SNAKE_CASE
). TypeScript enum constants and Python enum constants are identical.
TypeScript | Python |
---|---|
s3.BucketEncryption.KMS_MANAGED |
s3.BucketEncryption.KMS_MANAGED |
In TypeScript, a construct's props are defined with a shape-based interface, whereas Python takes keyword (or keyword-only, see PEP3102) arguments.
The following table shows how TypeScript construct definitions translate to Python construct definitions.
TypeScript | Python |
---|---|
interface MyConstructProps { |
class MyConstruct(Construct): |
Structs are TypeScript interfaces that represent a set of values. You can recognize them because their name doesn't start with an I
, and all of their fields are read-only.
In TypeScript, structs are passed as object literals. In Python, if the struct is the last argument to a method, its fields are lifted into the method call itself. If the argument list contains nested structs, wrap them in a class named after the struct.
The following table shows how to call a method with two levels of structs.
TypeScript | Python |
---|---|
bucket.addLifecycleRule({ |
bucket.add_lifecycle_rule( |
The AWS CDK uses TypeScript object interfaces to indicate that a class implements an expected set of methods and properties. You can recognize an object interface because its name starts with I
.
Typically, Python users don't explicitly indicate that a class implements an interface. However, for the AWS CDK you can do this by decorating your class with @jsii.implements(interface)
.
TypeScript | Python |
---|---|
import {IAspect, IConstruct } from '@aws-cdk/core'; |
from aws_cdk.core import IAspect, IConstruct |