[go: up one dir, main page]

DEV Community

Sharad Khambe
Sharad Khambe

Posted on

AWS CDK

In this blog, we will cover up basic of what is AWS CDK, it's features, use cases, cdk commands, how to use them and deploy into AWS.

AWS Cloud Development Kit

AWS Cloud Development Kit is open source development framework used to model and provision cloud resources using familiar programming languages such as JS, TS, Python, Go etc.
You can use any of supported languages to define your cloud resources and when deployed those resources are provisioned through AWS CloudFormation.

Key Benifits of using CDK

Familiar : Benefits of using programming language of your's/team's choice. This gives advantage of using same programming language for your infrastructure build as well as for your application development.

Tools Support : Lot of languages have in built support for linting, testing, debugging which is inherited by cdk framework.

Abstraction : CDK provides secure first defaults properties for most of resources according best practices and implementation. e.g. when you create S3 bucket it will have public access disabled by default. However you have option to set each properties as well if you chose to do so.

CDK Main Components

1. Core Framework : With core framework you can create App which can contain one more multiple structured components called as Stacks.
Stacks are logical unit of infrastructure which contains one or many resources. Resources are mapped one-to-one to Cloudformation resources. Stack in CDK is one to one relationship with CloudFormation stack. Similar to CloudFormation you can have nested stack in CDK(we will discuss this in another blog with example!).
It's good practice to divide resources into stack that have different purpose or lifecycles. E.g. stateless(SQS) vs stateful(RDS) OR front-end applications Vs backend API's for front end.

CDK Core Framework

2. AWS Construct Library: AWS construct library is set of components designed and created by AWS to create resources for specific services. These are build considering best practices and security implementation for given services. AWS constructs are categorised into three levels

Level 1(L1) constructs : These constructs are mapped one to one with single CloudFormation resource specification. E.g. S3 bucket. They are named starting with "Cfn". e.g. CfnBucket

Level 2(L2) constructs : These constructs provide higher level abstraction compared to L1 constructs. L2 constructs is very simple and requires very little input to generate complex CloudFormation templates. However they can have many defaults properties and options to be set.

Level 3(L3) constructs : These constructs are known as patterns and it can contain collection of resources that are configured to work together to achieve specific functionality. E.g. Appsync API with dynamoDB and step functions integration.

3. AWS CDK CLI : CLI commands helps to interact with core framework such as initialise our project, inspect differences between various deployments versions. Let's look at workflow on how to use CLI to develop infrastructure and deployment into AWS.

Development and Deployment Workflow

cdk bootstrap
CDK requires specific dedicated resources to be created for you to be able to deploy resources using cdk. cdk bootstrap cli command helps with achieving necessary preconfiguration to be done. This is just one time activity.

cdk bootstrap

Workflow

workflow

Create new project with name of directory e.g. "cdk-todo-app-api".
🔄 cdk init app --language=javascript

Build Project
🦾 npm run build

Create templates from given code
🧬 cdk synth

Validate change set that will result as part of deployment
🔎 cdk diff

Deploy changes to resources into cloud.
🚀 cdk deploy

Top comments (0)