AWS Cloud Development Kit (AWS CDK)
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

AWS CDK in Other Languages

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.

Importing a Module

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 s3 = require('@aws-cdk/aws-s3') // Selective import import { Stack } from '@aws-cdk/core';
# Import entire module import aws_cdk.aws_s3 as s3 # Selective import from aws_cdk.core import Stack

Instantiating a Class

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 const bucket = new s3.Bucket(this, 'Bucket');
# Instantiate Bucket class bucket = s3.Bucket(self, 'Bucket')
// Instantiate Bucket with props const bucket = new s3.Bucket(this, 'Bucket', { bucketName: 'my-bucket', versioned: true, });
# Instantiate Bucket with props bucket = s3.Bucket(self, 'Bucket', bucket_name='my-bucket', versioned=True)

Methods

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 bucket.addCorsRule({ allowedOrigins: ['*'], allowedMethods: [], });
# Call method bucket.add_cors_rule( allowed_origins=['*'], allowed_methods=[] )

Enum Constants

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

Defining Constructs

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 { prop1: number; prop2?: number; } class MyConstruct extends Construct { constructor(scope: Construct, id: string, props: MyConstructProps) { super(scope, id); const prop2 = props.prop2 !== undefined ? props.prop2 : 10; // Construct contents here } }
class MyConstruct(Construct): def __init__(scope, id, *, prop1, prop2=10): super().__init__(scope, id) # Construct contents here

Structs (Interfaces)

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({ transitions: [ { storageClass: StorageClass.GLACIER, transitionAfter: Duration.days(10) } ] });
bucket.add_lifecycle_rule( transitions=[ Transition( storage_class=StorageClass.GLACIER, transition_after=Duration.days(10) ) ] )

Object Interfaces

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'; class MyAspect implements IAspect { public visit(node: IConstruct) { console.log('Visited', node.node.path); } }
from aws_cdk.core import IAspect, IConstruct @jsii.implements(IAspect) class MyAspect(): def visit(self, node: IConstruct) -> None: print("Visited", node.node.path)