Translating TypeScript AWS CDK code to other languages
TypeScript was the first language supported for developing AWS CDK applications, and for that reason, there is a substantial amount of example CDK code written in TypeScript. If you are developing in another language, it may be useful to compare how AWS CDK code is implemented in TypeScript and your language of choice, so you can, with a little effort, make use of these examples.
For more details on working with the AWS CDK in its supported programming languages, see:
• the section called “In TypeScript” (p. 28)
• the section called “In JavaScript” (p. 31)
• the section called “In Python” (p. 38)
• the section called “In Java” (p. 43)
• the section called “In C#” (p. 46)
Importing a module
TypeScript/JavaScript
TypeScript supports importing either an entire namespace, or individual objects from a namespace.
Each namespace includes constructs and other classes for use with a given AWS service.
// Import main CDK library as cdk
import * as cdk from 'aws-cdk-lib'; // ES6 import preferred in TS const cdk = require('aws-cdk-lib'); // Node.js require() preferred in JS // Import specific core CDK classes
import { Stack, App } from 'aws-cdk-lib';
const { Stack, App } = require('aws-cdk-lib');
// Import AWS S3 namespace as s3 into current namespace import { aws_s3 as s3 } from 'aws-cdk-lib'; // TypeScript const s3 = require('aws-cdk-lib/aws-s3'); // JavaScript // Having imported cdk already as above, this is also valid const s3 = cdk.aws_s3;
// Now use s3 to access the S3 types const bucket = s3.Bucket(...);
// Selective import of s3.Bucket
import { Bucket } from 'aws-cdk-lib/aws-s3'; // TypeScript const { Bucket } = require('aws-cdk-lib/aws-s3'); // JavaScript // Now use Bucket to instantiate an S3 bucket
const bucket = Bucket(...);
Python
Like TypeScript, Python supports namespaced module imports and selective imports. Namespaces 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).
Importing a module
# Import main CDK library as cdk import aws_cdk as cdk
# Selective import of specific core classes from aws_cdk import Stack, App
# Import entire module as s3 into current namespace import aws_cdk.aws_s3 as s3
# s3 can now be used to access classes it contains bucket = s3.Bucket(...)
# Selective import of s3.Bucket into current namespace from aws_cdk.s3 import Bucket
# Bucket can now be used to instantiate a bucket bucket = Bucket(...)
Java
Java's imports work differently from TypeScript's. Each import statement imports either a single class name from a given package, or all classes defined in that package (using *). Classes may be accessed using either the class name by itself if it has been imported, or the qualified class name including its package.
Libraries are named like software.amazon.awscdk.services.xxx for the AWS Construct Library (the main library is software.amazon.awscdk). The Maven group ID for AWS CDK packages is software.amazon.awscdk.
// Make certain core classes available import software.amazon.awscdk.Stack;
import software.amazon.awscdk.App;
// Make all Amazon S3 construct library classes available import software.amazon.awscdk.services.s3.*;
// Make only Bucket and EventType classes available import software.amazon.awscdk.services.s3.Bucket;
import software.amazon.awscdk.services.s3.EventType;
// An imported class may now be accessed using the simple class name (assuming that name
// does not conflict with another class) Bucket bucket = new Bucket(...);
// We can always use the qualified name of a class (including its package) even without an
// import directive
software.amazon.awscdk.services.s3.Bucket bucket = new software.amazon.awscdk.services.s3.Bucket(...);
C#
In C#, you import types with the using directive. There are two styles, which give you access either all the types in the specified namespace using their plain names, or let you refer to the namespace itself using an alias.
Packages are named like Amazon.CDK.AWS.xxx for AWS Construct Library packages (the core module is Amazon.CDK).
// Make CDK base classes available under cdk using cdk = Amazon.CDK;
Instantiating a construct
// Make all Amazon S3 construct library classes available using Amazon.CDK.AWS.S3;
// Now we can access any S3 type using its name var bucket = new Bucket(...);
// Import the S3 namespace under an alias using s3 = Amazon.CDK.AWS.S3;
// Now we can access an S3 type through the namespace alias var bucket = new s3.Bucket(...);
// We can always use the qualified name of a type (including its namespace) even without a
// using directive
var bucket = new Amazon.CDK.AWS.S3.Bucket(...)
Instantiating a construct
AWS CDK construct classes have the same name in all supported languages. Most languages use the new keyword to instantiate a class (Python is the only one that doesn't). Also, in most languages, the keyword this refers to the current instance. Python, again, is the exception (it uses self by convention). You should pass a reference to the current instance as the scope parameter to every construct you create.
The third argument to a AWS CDK construct is props, an object containing attributes needed to build the construct. This argument may be optional, but when it is required, the supported languages handle it in idiomatic ways. The names of the attributes are also adapted to the language's standard naming patterns.
TypeScript/JavaScript
// Instantiate default Bucket
const bucket = new s3.Bucket(this, 'MyBucket');
// Instantiate Bucket with bucketName and versioned properties const bucket = new s3.Bucket(this, 'MyBucket', {
bucketName: 'my-bucket', versioned: true, });
// Instantiate Bucket with websiteRedirect, which has its own sub-properties const bucket = new s3.Bucket(this, 'MyBucket', {
websiteRedirect: {host: 'aws.amazon.com'}});
Python
Python doesn't use a new keyword when instantiating a class. The properties argument is represented using keyword arguments, and the arguments are named using snake_case.
If a props value is itself a bundle of attributes, it is represented by a class named after the property, which accepts keyword arguments for the sub-properties.
In Python, the current instance is passed to methods as the first argument, which is named self by convention.
# Instantiate default Bucket
bucket = s3.Bucket(self, "MyBucket")