How I used Cloud Development Kit to deploy AWS Cloud Infrastructure (Beginner Level)

Cong Pham
Geek Culture
Published in
6 min readAug 1, 2022
image source (https://aws.amazon.com/blogs/devops/developing-application-patterns-cdk/)

AWS Cloud Development Kit has increasingly become popular as a programming tool to build and manage AWS cloud resources.

I started to learn and use CDK as part of my job. The more I learn, the more I find that it is incredibly beneficial and easy to use. We can write the CDK code in one of the popular programming languages that it supports (JavaScript, TypeScript, Python, Java, C#, and Go) in our preferred IDE. Thus, we can share the CDK design and components to multiple teams around an organization. Also, I find it is extremely useful to identify all the AWS resources that have been created in a project as they can be seen as components inside the CDK code package that we have designed.

Here is the step by step process on how I used the CDK to create and manage AWS cloud infrastructure.

1. Install NodeJS

First, I installed NodeJS. Ideally, we want any version that is 14 or higher. To check the node version after you have downloaded it, we can run the following command in your terminal

node --version

2. Create project directory

Within my Workplace directory, I created a new project called hello-cdk and jumped into it using the following commands

mkdir hello-cdk

cd hello-cdk

3. Install AWS CDK Toolkit (AWS CDK CLI)

The AWS CDK Toolkit is the command line interface(CLI) for working with the application such as listing, bootstrapping, or deploying the cloud infrastructure. Run the following command in the terminal to download

npm install -g aws-cdk

4. Install AWS CLI and Setup your AWS account

The AWS CLI is a tool to access AWS services and create the access point from our laptops to our AWS accounts; so when we deploy the cloud infrastructure using AWS CDK CLI, the system knows which account to deploy into.

I installed the AWS CLI using the following link provided by AWS

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Once it is installed, we need to specify the access point in our laptop using Access Key Id and Secret Access Key from our AWS account.

Head over to the AWS Console from our web browser and search for IAM Console. Then choose User Groups and then click on Create group button to create a permission group. Give our permission group a named such as CloudAdmin and attach AdministratorAccess policy to that group.

Click on Users tab right below User groups and then click on Add users button. We can give this user any name and select Access key — Programmatic access option.

Step 2 is to add permission group and we choose the CloudAdmin permission group that we created before.

We can skip step 3 and then create user in step 4. Once it is done, the Access Key ID and Secret Access Key are available at step 5 and we can copy it into a note or csv.

Head back to the terminal in our laptop and run the following command to connect our laptop to the AWS account.

aws configure

When prompted, copy and paste AWS Access Key Id and AWS Secret Access Key. For the region, ideally, we want to choose the region we don’t have prior project that has been deployed in it. In my case, I choose us-west-1. For Default output format, we can leave it as text.

5. Initialize the CDK application with Typescript

In the terminal, within hello-cdk directory, run the following command to Initialize the CDK application with Typescript.

cdk init app --language typescript

This command create a CDK template code with all the dependencies the project needs to run and deploy cloud infrastructure.

When opening the project folder in your preferred IDE, the project structure should look like this.

The two most important files are hello-cdk.ts and hello-cdk-stack.ts under bin and lib directories.

hello-cdk.ts initializes the cloud application and then it builds the cloud infrastructure resources that are defined inside the hello-cdk-stack.ts. Thus, you can define as many stack class files as you like and each stack has as many resources (such as Amazon S3, Lambda, Dynamo, SQS, ….).

To keep it simple, we keep the one auto-generated stack hello-cdk-stack.ts and add Amazon S3 resource to our stack.

6. Add Amazon S3 resource to Stack

In hello-cdk-stack.ts , we can specify any resource that we want the CDK to provision. I choose Amazon S3 bucket as an example. First, we need to import the Bucket class and Bucket’s related class by adding this line to the top of the file. For more Amazon S3 bucket class properties, you can visit its website.

import { Bucket, BlockPublicAccess, BucketEncryption } from ‘aws-cdk-lib/aws-s3’;

Then you can create the Bucket class in the Stack’s constructor with bucket id, bucket name, and all its properties. The bucket name and bucket id must be unique in the world like our usernames.

7. Build the project

To build the project, simply run tsc in the terminal.

tsc

Once it finishes building, in the bin and lib directories, we can see that our typescript files have been translated into javascript files.

8. Bootstrapping

When deploying the stack to our AWS account, Amazon S3 bucket (not the bucket we are deploying) and other containers must be available. For more understanding about bootstrapping, you can follow this link from AWS.

To bootstrap, run this following command in your terminal

cdk bootstrap

You will see there will be a loading bar running in the terminal.

9. Deploy your first AWS cloud infrastructure using CDK

Once the bootstrapping process finishes, to deploy our stack to AWS account, we run the following command in the terminal

cdk deploy

We’ll see the progress bar will popup showing that our application is in deployment process.

10. Verify the deployment stack in AWS account.

Once it is completed, we can go to the AWS console and visit CloudFormation template in the us-east-1 region (N.California) to see our stack is successfully created.

CDKToolKit is the stack that was created during bootstrap.

HelloCdkStack is our stack that has Amazon S3 resource defined in it.

Then we can head over to Amazon S3 and see our bucket has been created.

And Voila! We are officially cloud engineers!

--

--

Cong Pham
Cong Pham

Written by Cong Pham

Hi! My name is Cong from Calgary, Canada. I have a Bachelor degree in Software Engineering and I am trying to share everything I know with you guys :)

No responses yet