Skip to the content.

Getting Started with AWS CDK Infrastructure as Code

Learn how to write your first AWS CDK app in your laptop and deploy resources in your AWS account right from your laptop. I am using MAC os for this demo. Later on I will use cloud9 to deploy resources in AWS cloud and try to setup CI/CD for my CDK app as well. I will try to setup CI/CD right from my GitHub repository also.

You CDK app has high level class called Stage. Stage can have many Stacks. You can synthesize ( convert code in cloudformation api ) & deploy one or more stacks to your AWS account. This tutorial walks you through creating and deploying a simple AWS CDK app, from initializing the project to deploying the resulting AWS CloudFormation template. The app contains one stack, which contains one resource: an Amazon S3 bucket.

We’ll also show what happens when you make a change and re-deploy, and how to clean up when you’re done.

http://www.rupeshtiwari.com/getting-started-with-aws-cdk/

Step 1: First time environment setup steps

  1. Open sudo vim ~/.aws/credentials and set your access key and security key.
  2. Install aws-cdk globally npm i -g aws-cdk check version cdk --version
  3. Run aws sts get-caller-identity to know your account number and run aws configuration get region to know your region
  4. Run cdk bootstrap aws://ACCOUNT-NUMBER/REGION. Bootstrap AWS CDK that will create S3 container to store assets required for deployment.
  5. If this is for production then make sure you create dedicated account to deploy your infrastructure and assign this account role to deploy on another accounts.
  6. Install AWS toolkit extension in your VSCode that will help you to create, debug and deploy applications on AWS. You can also see all of your AWS CDK projects.

Step 2: Create first AWS CDK project

  1. Create the app
    mkdir hello-cdk
    cd hello-cdk
    
  2. cdk init app --language typescript, it will create cdk template project also initialize git.
  3. Build the app npm run build
  4. cdk ls see stacks
  5. Goto lib/stack.ts add below line
    // create new s3 bucket
    new s3.Bucket(this, 'CdkCreatedBucket', { versioned: true })
    

Step 3: Synthesize an AWS CloudFormation template

Run cdk synth. The cdk synth generates a perfectly valid AWS CloudFormation template. You could take it and deploy it using the AWS CloudFormation console or another tool. But the AWS CDK Toolkit can also do that.

Step 4: Deploy the stack

Run cdk deploy. CDK will deploy the stack using AWS CloudFormation. The AWS CDK synthesizes your stack before each deployment.

Cloudformation stack created at AWS account on my selected region.

Deployment successful from my laptop

S3 created in my AWS account hurray!

Step 5: Modify the app

The AWS CDK can update your deployed resources after you modify your app. Update lib/hello-cdk-stack.ts.

new s3.Bucket(this, 'MyFirstBucket', {
  versioned: true,
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  autoDeleteObjects: true
});

Run cdk diff to check the differences

Run cdk deploy to deploy changes.

Before deploy S3 policy

After deploy S3 policy changed

Step 6: Destroying the app’s resources

Now that you’re done with the quick tour, destroy your app’s resources to avoid incurring any costs from the bucket you created, as follows.

cdk destroy

Enter y to approve the changes and delete any stack resources.

Destroy was super quick!!

Reference