What is AWS CDK (Cloud Development Kit)?
AWS CDK is an open-source framework that lets you model and provision AWS cloud resources using the programming language of your choice. 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.
You may have to do manual processes or write special scripts while creating your resources and it can be longer. AWS CDK allows you to use a programming language you know and use the power of that language when creating these resources. Let’s start to use AWS CDK 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.
When an AWS CDK application is synthesized, the result is a cloud assembly, which contains the generated AWS CloudFormation templates for your stacks including the assets of your application. This package can then be deployed by the AWS CDK CLI to your preferred AWS account and region.
Advantages of AWS CDK
* 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.
* 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.
* 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 helps you and your teammates get started faster.
* 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.
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. When an object is put in the AWS S3 bucket, the AWS Lambda function will be triggered and writes the name of the object to the AWS DynamoDB table and then notify the action developer via email.
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 cdk command because you installed 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 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 new IAM group and IAM user.
Add 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 permisson for AWS Lambda and user.
The following code snippet can be used to create a DynamoDB table.
The following code snippet can be used to create a SNS topic and an email subscription for this topic.
The following code snippet can be used to create an S3 bucke. In this example, we are using the environment variables to use in AWS Lambda function.
In this step, our code path should be 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 S3 trigger for AWS Lambda function.
The following code snippet can be used to create event 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 AWS CloudFormation template from AWS CDK stack in your project path:
cdk synth Alternative: npx aws-cdk synth
For building application step, 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 the AWS CDK is and how it 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. Overall, 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 code from here.