AWS Cloud Development Kit (CDK)

To Nha Notes | Feb. 23, 2022, 11:01 a.m.

Welcome to the AWS Cloud Development Kit (CDK) Developer Guide. This document provides information about the AWS CDK, a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.

The AWS CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and (in developer preview) Go. Developers can use one of these supported programming languages to define reusable cloud components known as Constructs. You compose these together into Stacks and Apps.

 

Installation:
  • TypeScript 2.7 or later
    npm -g install typescript

  • Install the AWS CDK Toolkit globally 
    npm install -g aws-cdk

  • Install dependencies
    npm install

  • The actual package name of the main CDK package varies by language (not required?).
    npm install aws-cdk-lib

Useful commands:
  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

You can specify the names of multiple stacks to be synthesized or deployed in a single command. If your app defines only one stack, you do not need to specify it.

cdk ls  # list stacks
cdk synth # app defines single stack
cdk synth "Stack?" # Stack1, StackA, etc.
cdk deploy Happy Grumpy # app defines two or more stacks; two are deployed
cdk deploy "*Stack" # PipeStack, LambdaStack, etc.

Tip: you don't need to explicitly synthesize stacks before deploying them; cdk deploy performs this step for you to make sure your latest code gets deployed.

Following these guidelines will help make your code consistent with other AWS CDK applications as well as easier to understand.

  • Use ES6-style import directives, not require().

  • Generally, import individual classes from aws-cdk-lib.

    import { App, Stack } from 'aws-cdk-lib';
  • If you need many classes from aws-cdk-lib, you may use a namespace alias of cdk instead of importing the individual classes. Avoid doing both.

    import * as cdk from 'aws-cdk-lib';
  • Generally, import AWS service constructs using short namespace aliases.

    import { aws_s3 as s3 } from 'aws-cdk-lib';
    import * as s3 from 'aws-cdk-lib/aws-s3';
Writing your own constructs

 Adding a topic property allows consumers to access the inner topic, as shown in the following example:

export class NotifyingBucket extends Construct {
  public readonly topic: sns.Topic;

  constructor(scope: Construct, id: string, props: NotifyingBucketProps) {
    super(scope, id);
    const bucket = new s3.Bucket(this, 'bucket');
    this.topic = new sns.Topic(this, 'topic');
    bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix });
  }
}

Now, consumers can subscribe to the topic, for example:

const queue = new sqs.Queue(this, 'NewImagesQueue');
const images = new NotifyingBucket(this, '/images');
images.topic.addSubscription(new sns_sub.SqsSubscription(queue));

IAM reference

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam-readme.html

https://bobbyhadz.com/blog/managed-policy-aws-cdk

https://github.com/bobbyhadz/managed-policy-aws-cdk/blob/master/lib/cdk-starter-stack.ts

https://bobbyhadz.com/blog/aws-cdk-iam-role

https://github.com/bobbyhadz/aws-cdk-iam-role/blob/master/lib/cdk-starter-stack.ts

CDK context

https://docs.aws.amazon.com/cdk/v2/guide/context.html

https://yshen4.github.io/infrastructure/AWS/CDK_context.html

References

https://docs.aws.amazon.com/cdk/v2/guide/home.html

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html

https://cdkworkshop.com/

https://github.com/kevinslin/open-cdk

https://github.com/kolomied/awesome-cdk

https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript

https://bobbyhadz.com/blog/aws-cdk-tutorial-typescript

https://bobbyhadz.com/blog/aws-cdk-s3-lifecycle-rules

AWS Toolkit for Visual Studio Code

https://dev.to/aws/using-aws-cdk-to-deploy-your-amazon-managed-workflows-for-apache-airflow-environment-12cf