Infrastructure In Code using AWS Cloud Development Kit (AWS CDK using Python)

Creating resources in AWS has always been a challenge for the growing enterprises or for the enterprises which are using AWS resources or services in large scale. There are indeed different approaches, as below:
1. Manual
2. Using AWS CLI (Call API)
3. Declarative (using Cloudformation)
4. DOM (Using a different language to create the CF templates and deploying the same, example: Python troposphere)
5. Componentized

In last few years DOM was heavily used, but that comes with different set of challenges. As an example using the same if someone wants to create a VPC might end up writing hundred lines of code.

To overcome the problems of the DOM model, CDK was developed, default AWS library comes with CDK, plus the best practices of AWS comes with it. It uses construct to deploy the stacks. Moreover the same features are available in five different languages: Typescript, Javascript, Python, Java and C#.

Setup CDK in local using Python:

Here we have used Windows machine, on the top of that we have installed virtual linux box on the top of it.

Install below softwares and set them up in your system:

  1. Virtual Linux
  2. Python
  3. NPM
  4. Visual Studio
  5. AWS-CLI
  6. Git

Setup the language:

This applies the python template theme on the project.

cdk init app –language python

On running the above command below things happened in the work space:

  1. A blank project in CDK python is created.
  2. cdk.json gets created which tells the CDK how to execute the app.
  3. The initialization process creates a virtual environment.
  4. The AWS packages can be downloaded in the requirements file. Using command: $ pip install -r requirements.txt

Some Useful CDK commands:

## Useful commands

  •  * `cdk ls`          list all stacks in the app or compiles
  •  * `cdk synth`       emits the synthesized CloudFormation template
  •  * `cdk deploy`      deploy this stack to your default AWS account/region
  •  * `cdk diff`        compare deployed stack with current state
  •  * `cdk docs`        open CDK documentation

Enable the virtual environment with the below command:

$source .venv/bin/active

Install the aws-cdk libraries as follows:

Create a S3 bucket in the python file.

After synthesizing the python file the template.json file gets created:

For first time, if you are running the project then use the below command: $cdk bootstrap

Once the above command is run, below changes are visible in the S3 and Cloudformation services:

A bucket gets created:

New CDK stack gets created.

Then deploy using the below command:

$cdk deploy

The Bucket gets created:

You can check the same from the stack:

Plan and visualize deployment:

$ Cdk diff – keyword to check the differences between previous and what we have changed.

We can use Cdk token, which is cfn intrinsic function (part of aws-cdk.core library).

Now using Cdk token we can save the state of the aws resource and that can be used in the different project as well.

Example:

While deploying:

In cloud formation stack:

To destroy the resources use below command:

$cdk destroy

Be the first to comment

Leave a Reply

Your email address will not be published.


*