If you are seeking a solution that simplifies building and deploying cloud infrastructure, AWS CDK may be the answer you’re looking for. 

Let’s discover AWS Cloud Development Kit, examine the advantages of the AWS CDK, and learn how to use it.

What is AWS CDK (Cloud Development Kit)?

AWS Cloud Development Kit

 

AWS CDK (Cloud Development Kit) is an open-source software development framework that: 

  • defines cloud infrastructure in code
  • provisions it 

Basically, it enables you to model application infrastructure using TypeScript, Python, Java, or .NET. Behind the scenes, it uses AWS CloudFormation to provision resources in a safe and repeatable manner.

With AWS CDK, you can define cloud resources such as Amazon S3 buckets, Amazon EC2 instances, and AWS Lambda functions using object-oriented programming languages. AWS CDK then generates CloudFormation templates from the code, which can be used to deploy the defined infrastructure on AWS.

AWS CDK also provides a library of pre-built AWS constructs, which are reusable cloud components that can be customized and extended to fit specific use cases. This allows you to build and deploy cloud infrastructure quickly and efficiently. So you will not have to write complex CloudFormation templates from scratch.

Overall, AWS CDK provides a modern approach to cloud infrastructure development. It allows you to use your preferred programming languages and tools to define and deploy cloud resources on AWS.

Okay, let’s approach this together. 

AWS CDK Concepts

AWS CDK consists of 3 major building blocks:

  • App – is the root of your construct tree and consolidates all stacks and constructs in one application that can then be used to deploy on AWS Cloud.
  • Stack – is similar to a CloudFormation stack. It’s a template that holds the AWS resources in the form of constructs and can be used for deployment.
  • Construct – is the basic building block that can contain a single AWS resource or multiple AWS resources combined. You’re free to build and combine AWS resources in your own constructs.

 

AWS CloudFormation

AWS CDK applications are synthesized into a cloud assembly. This assembly includes the AWS CloudFormation templates and assets for your stacks.

This package can then be deployed by the AWS CDK CLI to your preferred AWS account and region.

Advantages of AWS CDK

AWS CDK provides several advantages for you to build and deploy infrastructure as code.

  1. Powered by AWS CloudFormation: AWS CDK allows you to define your resources with code and provision them through AWS CloudFormation. You get all the benefits of CloudFormation. Deployment, rollback, etc.
  2. Faster development process: With AWS CDK, you can use the power of programming language to create resources. Familiar features such as objects, loops, and conditions speed up your development process. In addition, you can develop resources using the AWS CDK in any IDE you want.
  3. Customize, share, and reuse constructs: With AWS CDK, you can customize, share, and reuse constructs within your team. This helps you to build constructs and enables you and your teammates to get started faster.
  4. No context switching: AWS CDK allows you to build your cloud application on AWS without leaving the ide environment you are using. Write your runtime code and define your AWS resources with the same programming language.

Also, learn about AWS Serverless Application Model.

Demo with AWS CDK

Let’s examine how AWS CDK operates through a scenario.

Demo with AWS CDK

Scenario: We have a developer who wants to be notified when a user uploads a specific file type to a specific AWS S3 bucket. They also want to save the file name to the database. How can we design this project?

Solution: We will build a CDK app that consists of S3-triggered AWS Lambda and SNS Topic created by AWS CDK. AWS Lambda function is triggered when an object is added to the S3 bucket. The function writes the object name to the AWS DynamoDB table and notifies the action developer by email.

You may like this: How to Securely Share AWS S3 Files

Step 1: How to install and configure AWS CDK?

Prerequisites: AWS CLI, AWS Account and User, Node.js, IDE for your programming language, AWS CDK Toolkit, your programming language (Python, Java, Go, etc.) 

In our demo, Python is going to be our programming language.

Note: Node.js is always required to use CDK, but all other language prerequisites are only required if building a CDK app in that language.

Install the AWS CDK Toolkit globally using the following Node Package Manager command:

npm install -g aws-cdk

Run the following command to verify the correct installation and print the version number of the AWS CDK.

cdk –version
Alternative: npx aws-cdk –version

Tip: Alternative means, normally you can check only the CDK command because you installed it globally. If it does not work you can use “npx aws-cdk ….” commands for your local.

Step 2: Creating Project – CDK App

Each AWS CDK app should be in its own directory, with its own local module dependencies. You can start with your home directory or what you want. Open your CMD and create your project folder:

mkdir s3-trigger-lambda-project
cd s3-trigger-lambda-project


Start the application with the “cdk init” command:

cdk init app --language python 
Alternative: npx aws-cdk init app --language python

Tip: If you received a message like “Unable to initialize git repository for your project.” Then follow this:

python -m venv .venv

After the app has been created, to activate the app’s Python virtual environment follow this command:

source .venv/bin/activate

Install the AWS CDK core dependencies:

python -m pip install -r requirements.txt

You can list stacks in your app:

cdk ls
Alternative: npx aws-cdk ls

Tip: If you receive “The security token included in the request is expired”, you need to configure your AWS account.

Step 3: Create the application code

Open your s3-trigger-lambda-project/s3_trigger_lambda_project/s3_trigger_lambda_project_stack.py file.

Import what we need for our application to create resources.

Create a new IAM group and IAM user.

Add an IAM user to the group.

The following code snippet can be used to create an S3 bucket. In this example, we are using the grand_read_write command to give permission for AWS Lambda and the user.

The following code snippet can be used to create a DynamoDB table.

The following code snippet can be used to create an SNS topic and an email subscription for this topic.

The following code snippet can be used to create an S3 bucket. In this example, we are using the environment variables to use in the AWS Lambda function. 

In this step, our code path should be the AWS Lambda handler path or our lambda.zip address.

We are using the grand_read_write command to give permission for AWS Lambda and using grand_publish to publish our message from AWS Lambda.

The following code snippet can be used to create an S3 trigger for the AWS Lambda function.

The following code snippet can be used to create events only for .jpg files.

The following code is our AWS Lambda function. 

Step 4: Build and Deploy Application

Synthesize an AWS CloudFormation Template: You need to run this command to create an AWS CloudFormation template from the AWS CDK stack in your project path:

cdk synth
Alternative: npx aws-cdk synth

In the step to build the application, you need to use this command:

cdk bootstrap
Alternative: npx aws-cdk bootstrap

For deploying the stack:

cdk deploy
Alternative: npx aws-cdk deploy

Step 5: Test the Application

To test the application, you need to upload a file to the S3 bucket from AWS Console or CLI. Then you need to check your email. You should see something like the below:

Besides, you should check the DynamoDB table whether the photo name is written.

Conclusion

In this blog, you have learned 

  • what AWS CDK is and 
  • how AWS CDK can build your project. 

You’ve read about how the AWS CDK stacks on AWS CloudFormation and what advantages and features it has when you want to use the AWS CDK. 

In a nutshell, the AWS Cloud Development Kit (CDK) simplifies building and deploying cloud infrastructure. This makes managing complex, multi-service applications easier and ensures consistency across environments.

The AWS CDK includes best practices for security, compliance, and performance, providing a modern approach to infrastructure management. This helps you avoid mistakes and ensures your infrastructure is secure, reliable, and scalable.

We can say that AWS CDK will create a new bridge between infrastructure and DevOps, enabling infrastructure definitions in many programming languages such as Python, Java, Go, etc. 

You can access all demo codes from here.