How to create a custom VPC using AWS CDK

Reading Time: 4 minutes

Hello Readers! In this blog we will get familiar with AWS Cloud Development Kit (AWS CDK) which is a new framework amazon has released to make developer life easy. Therefore CDK allows you to create AWS resources in the language that the developer is already comfortable with. 

For example if you are writing an application in Java or python then you can use the same language for provisioning your resources. There are two major versions of CDK, v1 and v2. There is no charge for using the AWS CDK, but you have to pay for creating chargeable resources. So, let’s see how we can create a VPC using AWS CDK.

Let’s get started!

Installation of AWS CDK:

We will install the AWS CDK toolkit with the Node Package Manager. Use the following command to install latest version of it:

$ npm install -g aws-cdk
AWS CDK

Similarly For a specific version use this command:

$ npm install -g aws-cdk@X.YY.Z

Once the installation is completed, Check its version using:

$ cdk –version
AWS CDK

You can get the list of commands you can use with CDK by the help command:

 CDK help

Create a custom VPC using AWS CDK:

Here We will use AWS CDK(Typescript) to create a custom VPC with as minimal coding as possible. Let’s get started.

Step1: Firstly Create a directory for custom-vpc and move to it.

$ mkdir custom-vpc
$ cd custom-vpc

Step2: Initialize your folder using CDK. It will create the necessary folder structure. By initializing I will tell CDK that I want folder structure for the Typescript format. You can choose it as per your wish.

$ cdk init --language typescript 
CDK init

So, It has created a blank project for language typescript with CDK. This we will use for creating a custom VPC along with the necessary packages.

Step3: When you will see your directory structure you will find one file inside /bin that is custom-vpc.ts. This is actually the cloudformation stack that our app is going to create. The code contains:

import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { CustomVpcStack } from '../lib/custom-vpc-stack';
const app = new cdk.App();
new CustomVpcStack(app, 'CustomVpcStack');

Now, we are all set and ready to write the code in /lib/custom-vpc-stack.ts. We will import the ec2 library here. After importing you must have to install it by using the command:

$ npm install @aws-cdk/aws-ec2
AWS CDK

Put this whole code in your /lib/custom-vpc-stack.ts file for creating the VPC:

import * as cdk from "@aws-cdk/core";
import * as ec2 from "@aws-cdk/aws-ec2";
export class CustomVpcStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new ec2.Vpc(this, "Mycdkvpc");
  }
}

Step4: Verify that everything is good by using the command:

$ cdk ls
CDK ls

Step5: Run the command that will compile our typescript.

 $ npm run build
AWS CDK

Step6: Bootstrap your environment by running the following command:

$ cdk bootstrap
CDK Bootstrap

The bootstrap command will create a CloudFormation stack in the environment passed on the command line.This will take sometime.

You can check it on console after finishing. It has created CloudFormation stack.

AWS stack

Step7: Run the following command to create a VPC.

$ cdk deploy
CDK deploy

This command will take a while as it will create a number of resources. It will create two public and private subnets. Routing tables, internet gateway will be created. It is using a construct to create this VPC. It saves a lot of time for creating the resources. So, With a very small code we can just play with AWS in very less time and effort. 

You can find here the progress is going on…

CDK deploy

After successfully completing the process you can move to the console and find the VPC you have created using AWS CDK.

custom VPC

So, Total four public and private subnets are created in the availability zones.

AWS VPC

If you want to delete all the resources you have to use only a single command:

$ cdk destroy

Conclusion

Thank you for sticking to the end. So, In this blog we have seen how to use AWS CDK to create a custom VPC. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.

HAPPY LEARNING!

knoldus

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.