I Deployed My First AWS Cloud Development Kit App. Here’s What I Learned.

AWS Cloud Development Kit can help you to deploy your AWS CloudFormation stack in few lines of code

Johan Rin
4 min readApr 26, 2020
Photo by Aaron Birch on Unsplash

I’m doing the #AWSCertified challenge introduced by freeCodeCamp and I decided to write about my journey. I’m preparing now the AWS Certified Developer Associate exam with the course developed by Andrew Brown of ExamPro. To pass the certification, I decided to experiment more and share with you what I learn and also the pain points I encounter during the process.

During the past few days, I decided to explore AWS Cloud Development Kit (CDK) and feel the difference with AWS CloudFormation.

My objective was to rewrite partially the VPC Follow Along with the CDK and use mostly command lines.

Prerequisites

If you want to follow along and run the commands below, you simply need to create a Cloud9 environment. AWS already installed the CDK for us and we can start right away.

Initialize the app

In your Cloud9 terminal, type the following commands:

It will create a new folder for your app (e.g. helloworld) and initialize the CDK with one of the supported languages: C#, F#, Java, Python, JavaScript, or TypeScript.

Install dependencies

The initialized app only has the CDK core package installed. Other AWS services are in their own packages as defined in the API reference.

Because the objective is to create a Virtual Private Cloud (VPC) and define an Elastic Compute Cloud (EC2) within, you need to install @aws-sdk/aws-ec2 package by typing the following command in your terminal:

Implement the logic

Open lib/helloworld-stack.js file and be ready to write some code!

There are 4 steps to deploy an EC2 instance as in the VPC Follow Along:

  1. Create a VPC
  2. Create a Security Group (SG) and define an HTTP ingress rule
  3. Select an Amazon Machine Image (AMI) and create the EC2 instance
  4. Add a script to the EC2 instance

But first, include the package installed previously at the top of lib/helloworld-stack.js before continuing:

Create a Virtual Private Cloud

To define a VPC with the CDK, you have to write the following lines of code in the constructor:

This will create a VPC with public and private subnets and spread over at most 2 or 3 Availability Zones.

Create a Security Group and define an HTTP ingress rule

Let’s add an HTTP rule for the future EC2 instance now that a VPC is defined:

Select an Amazon Machine Image and create the EC2 instance

Before creating the EC2 instance within the VPC, you need to select which AMI you want to use:

You need to explicitly define that you want to place your EC2 instance in a public subnet. By default, the CDK places all the instances in a private subnet.

Add a script to the EC2 instance

Now the EC2 instance is defined, add the following script to have an HTML page to display after the deployment:

Deploy the app

Before deploying your app, you can define the Public DNS as output to directly access the EC2 instance:

You should normally have this result:

Now, you can deploy your app by typing the following command in your terminal:

This command will transpile your code into CloudFormation template and then invoke CloudFormation to deploy your stack in your AWS account.

Once deployed, copy the PublicDNSIPv4 displayed in your terminal and paste the URL in your favorite browser. You should have this result:

Once the app is deployed

Let’s say you want to add a new SSH rule in your SG:

Type the following command in the terminal to see how the new code impacts the deployed stack:

After that, you can redeploy your app to apply the changes:

If you’re curious about what your CloudFormation template looks like, you can type the following command in the terminal and open helloworld.yaml file:

Your file should look like that:

It’s a very long template with almost 700 lines that you wrote with less than 100 lines of code!

Of course, you can directly invoke CloudFormation to deploy this template by typing the following command in your terminal:

You need to define the stack name and also explicitly acknowledge that the stack template will affect permissions in your AWS account. Because the stack is already up to date, there will be no changes.

Destroy the app

When you are done, don’t forget to delete your app to avoid any costs and save money:

Conclusion

You created a very long CloudFormation template with only a few lines of CDK and deployed an EC2 instance! 🎉🎉🎉

I found the CDK very useful to learn how CloudFormation works. At first, I tried to learn CloudFormation alone and it was very frustrating. The syntax is very verbose and it’s hard to have a good example without writing tons of lines.

Using the CDK makes the experience nice and fun because you have results relatively quickly. I was surprised by the number of lines transpiled by the CDK: 700 lines of CloudFormation for 100 lines of CDK. The difference is huge!

What I regret is that the API reference doesn’t have a JavaScript section. I started my tutorial in JavaScript based on the CDK Getting Started and it was a little confusing to read the API reference at the beginning… But nothing to worry about! 😅

That’s it for me, hope you learned something! If you have any questions, find me on Twitter and feel free to ask me anything 🙏

Don’t forget to Stay Home, Save Lives, and Pass your AWS exam!

--

--