AWS CDK Examples

This topic contains some usage examples to help you get started understanding the AWS CDK.

Creating a DynamoDB Table

The following example creates a DynamoDB table with the partition key Alias and sort key Timestamp.

import { App, Stack, StackProps } from '@aws-cdk/core';
import { KeyAttributeType, Table } from '@aws-cdk/dynamodb'

class MyStack extends Stack {
  constructor(parent: App, name: string, props?: StackProps) {
    super(parent, name, props);

    const table = new Table(this, 'Table', {
      tableName: 'MyAppTable',
      readCapacity: 5,
      writeCapacity: 5
    });

    table.addPartitionKey('Alias', KeyAttributeType.String);
    table.addSortKey('Timestamp', KeyAttributeType.String);
  }
}

const app = new App(process.argv);

new MyStack(app, 'MyStack');

process.stdout.write(app.run());

Creating an Amazon RDS Database

The following example creates the Aurora database MyAuroraDatabase.

import { App, Stack, StackProps, Token } from '@aws-cdk/core';
import { InstanceClass, InstanceSize, InstanceTypePair, VpcNetwork } from '@aws-cdk/ec2';
import { DatabaseCluster, DatabaseClusterEngine } from '@aws-cdk/rds';

class MyStack extends Stack {
  constructor(parent: App, name: string, props?: StackProps) {
    super(parent, name, props);

    const vpc = new VpcNetwork(this, 'VPC');

    new DatabaseCluster(this, 'MyRdsDb', {
      defaultDatabaseName: 'MyAuroraDatabase',
      masterUser: {
        username: new Token('admin'),
        password: new Token('123456')
      },
      engine: DatabaseClusterEngine.Aurora,
      instanceProps: {
        instanceType: new InstanceTypePair(InstanceClass.Burstable2, InstanceSize.Small),
        vpc: vpc,
        vpcPlacement: {
          usePublicSubnets: true
        }
      }
    });
  }
}

const app = new App(process.argv);

new MyStack(app, 'MyStack');

process.stdout.write(app.run());

Creating an Amazon S3 Bucket

The following example creates the Amazon S3 bucket MyS3Bucket with server-side KMS encryption provided by Amazon S3.

import { App, Stack, StackProps } from '@aws-cdk/core';
import { Bucket, BucketEncryption } from '@aws-cdk/s3';

class MyStack extends Stack {
  constructor(parent: App, name: string, props?: StackProps) {
    super(parent, name, props);

    new Bucket(this, 'MyBucket', {
      bucketName: 'MyS3Bucket',
      encryption: BucketEncryption.KmsManaged
    });
  }
}

const app = new App(process.argv);

new MyStack(app, 'MyStack');

process.stdout.write(app.run());

Compiling the Examples

These examples apply to a hello-cdk app.

Compile the TypeScript app hello-cdk.ts into the JavaScript code hello-cdk.js using the following command.

npm run prepare

Use npm run watch to automatically re-compile those changes. Run the command in another command window and do not run it in the background.

Creating a CloudFormation Template

Use the cdk synth command to create an AWS CloudFormation template from the stack in your app. You should see output similar to the following for your DynamoDB table.

Resources:
    TableCD117FA1:
        Type: 'AWS::DynamoDB::Table'
        Properties:
            AttributeDefinitions:
                -
                    AttributeName: Alias
                    AttributeType: S
                -
                    AttributeName: Timestamp
                    AttributeType: S
            KeySchema:
                -
                    AttributeName: Alias
                    KeyType: HASH
                -
                    AttributeName: Timestamp
                    KeyType: RANGE
            ProvisionedThroughput:
                ReadCapacityUnits: 5
                WriteCapacityUnits: 5
            TableName: MyAppTable

Deploying your Stack

Use cdk deploy to deploy the stack. As cdk deploy executes you should see information messages, such as feedback from CloudFormation logs.