AWS CDK Workshop

1. New Project

Installing TypeScript

$ npm install -g typescript

Making a New Directory

$ mkdir cdk-workshop && cd cdk-workshop

CDK Init

chloe@chloe-XPS-15-9570 ~/Workspace/aws-test/cdk-workshop
$ cdk init sample-app --language typescript
Applying project template sample-app for typescript
Initializing a new git repository...
Executing npm install...
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported

> core-js@2.6.11 postinstall /home/chloe/Workspace/aws-test/cdk-workshop/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN cdk-workshop@0.1.0 No repository field.
npm WARN cdk-workshop@0.1.0 No license field.

added 254 packages from 184 contributors and audited 900 packages in 17.017s

29 packages are looking for funding
  run `npm fund` for details

found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details

 βœ… Your CDK TypeScript project is ready!

   cat README.md for next steps

Project Structure

  • bin/cdk-workshop.ts is the entry point of the CDK application.

    • It loads the stack defined in lib/cdk-workshop-stack.ts

  • lib/cdk-workshop-stack.ts is where the main stack for the CDK application is defined.

  • cdk.json tells the toolkit how to execute the app

    • In this case, it tells to run "npx ts-node bin/cdk-workshop.ts"

  • package.json defines the npm module.

    • Includes npm scripts and dependencies.

  • tsconfig.json is the TypeScript compiler configuration file.

2. CDK Application

Setting up

  • cdk-workshop.ts is the entry point of the CDK app

  • This code loads and instantiates the CdkWorkshopStack class from the lib/cdk-workshop-stack.ts file

  • Let's take a look at lib/cdk-workshop-stack.ts:

Analysis

  • Stack defines:

  1. SQS Queue (new sqs.Queue)

  2. SNS Topic (new sns.Topic)

  3. Subscription between the queue and topic, so all messages sent to the topic will be delivered to the queue

3. CDK Concepts

  • The CDK application is a collection of stacks

  • This CDK app contains a single stack (CdkWorkshopStack)

  • A stack is a unit of deployment:

    • All resources in a single stack are deployed together

  • A stack contains constructs

    • Each construct defines one or more AWS resources

  • Constructs (from AWS CDK):

    • CDK offers a collection of constructs called AWS Construct Library

    • The constructs represent all AWS services

    • Three different levels of constructs:

1. CFN Resources (Level 1)

  • These constructs directly represent all resources available in AWS CloudFormation

    • CFN resources have names starting with Cfn

    • For example, in the EC2 library, CfnInstance represents the AWS::EC2::Instance CFN resource

  • You must explicitly configure all resource properties, which requires a complete understanding of the details of the underlying CloudFormation resource

2. AWS Constructs (Level 2)

  • Level 2 constructs are also called AWS constructs

  • Level 2 constructs provide the same AWS resources as Level 1, but with richer API and sensible defaults

    • If a well-designed class can be built for a CloudFormation resource, it will be included in the AWS Construct Library

  • AWS constructs offer convenient defaults and boilerplate, making it easy to work without being an expert on the specific AWS service

3. Patterns (Level 3)

  • Patterns declare multiple resources in order to create architectural patterns

  • Helps you get up and running on AWS easily because patterns encode AWS best practices and offer a simple intention-based API

Example of Three Levels:

  • For Amazon S3:

  1. s3.CfnBucket represents the AWS::S3::Bucket CFN resource

  2. s3.Bucket represents a Level 2 construct for an S3 bucket

  3. s3-deployment.BucketDeployment represents a Level 3 construct

    • It populates S3 buckets with the contents of .zip files from other S3 buckets or local disk

4. CDK Synthesize

  • CDK applications are effectively high-level code

    • When deployed, it needs to be synthesized to CloudFormation templates

  • Synthesizing means converting source code to CloudFormation template

  • The cdk synth command is used to synthesize CDK apps

  • Running the cdk synth command in the directory where cdk.json file exists outputs the CloudFormation template as shown above

  • This template creates the following 4 resources:

  1. AWS::SQS::Queue

    • SQS queue

  2. AWS::SNS::Topic

    • SNS topic

  3. AWS::SNS::Subscription

    • Subscription definition between the queue and topic

  4. AWS::SQS::QueuePolicy

    • IAM policy that allows the topic to send messages to the queue

  • AWS::CDK::Metadata is a resource automatically created in every stack by the CDK toolkit

    • It is used by the CDK team to identify and analyze security issues!

5. CDK Deploy

Environment Bootstrap

  • To deploy an AWS CDK app to an environment (account/region), you must first install a bootstrap stack

    • The bootstrap stack contains resources required for the toolkit's operation

      • e.g., S3 bucket to store CFN templates and assets created during the deployment process

  • You can install the bootstrap stack for an environment using the cdk bootstrap command

  • If you get an Access Denied error here, it means:

    1. AWS CLI is not properly configured, or

    2. The AWS profile being used doesn't have permission to perform the cloudformation:CreateChangeSet operation

  • Once the above command runs successfully, you can deploy CDK apps!

Deploying

  • Use the cdk deploy command to deploy the CDK app

  • The above warning is displayed when the app being deployed includes items that require security review

    • Since the Topic needs to send messages to the Queue, enter y to deploy the stack and create the resources!

  • Execution result explanation:

    • us-west-2 is the region where the app was created,

    • 213888382832 is the account ID, and

    • fa564140-f078-11ea-b665-0a050e07f862 is the stack ID

CloudFormation Console

  • CDK apps are deployed through AWS CloudFormation

  • CDK stacks have a 1:1 mapping with CloudFormation stacks

    • This means you can use CloudFormation to manage the stack!

CloudFormation console

image-20200907050327196
  • Select CdkWorkshopStack and click the Resources tab to check the physical IDs of the created resources

Checking physical IDs

image-20200907050258865

Last updated