Open In App

AWS CDK(Cloud Development Kit)

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS CDK or Cloud Development Kit is a framework that allows you to use reusable components called constructs to build cloud environments. Developers can build and create cloud services using Integrated Development Environments with the help of libraries and components. AWS CDK provides a simple and easy approach to building infrastructure with the help of programming languages. So, let’s learn more about AWS CDK.

Primary Terminologies of AWS CDK

  • Constructs: Constructs are components containing configuration, bootstrap code, and logic for running any of the AWS services
  • Infrastructure As A Code (IaaC): Infrastructure as a Code is a model or framework that provides the ability to provision and build a cloud environment with the help of code.
  • Cloud Formation: IaaC service provided by AWS for provisioning AWS resources using code.

What is AWS CDK?

  • AWS CDK includes a Command Line Interface or Toolkit that defines the design and implementation of of application where the design and implementation are written in different programming languages such as JavaScript, typescript etc.
  • AWS CDK requires the definition of one or more stacks such as a cloud formation stack.
  • Stack uses the construct defined above. Constructs are programming language component that contains the exact definition and structure of AWS resources.
  • CDK converts this construct into cloud formation templates and then deploys or provisions the requested resources minimizing the need for complex operations.
    AWS CDK Workflow

CDK Constructs

Constructs are further divided into 3 levels L1, L2 and L3:

L1 Constructs

These constructs represent constructs that are defined by cloud formation. These constructs are generated from cloud formation resource specification.

Below is example for L1 construct for S3 bucket which is CfnBucket in javascript:

Javascript




const gfgbucket = new s3.CfnBucket(stackname, "gfgbucket", {
  bucketName: gfgbucket"
});


Below is example for L1 construct for Dynamo DB which is CfnTable in javascript:

Javascript




new dynamodb.CfnTable(
   stack name , 
   "gfgtable"
   {
      keySchema: [
         {
            attributeName: props.attributeName,
            keyType: "HASH",
         },
      ],
      attributeDefinitions: [
         {
            attributeName: props.attributeName,
            attributeType: "S",
         },
      ],
   },
);


L2 Constructs

These constructs are higher level constructs built over L1 constructs. These include predefined configurations and methods for configuring services reducing complexity and providing more abstraction.

L3 Constructs

These constructs are top level constructs which are used to define patterns for multiple resource provisioning or designing higher level architectures using AWS CDK.

CDK Stacks and Apps

CDK Apps

  • CDK apps are just like components that contain definition of one or more stacks in the aws.
  • App is the entry point for CDK application. It is defined as a class in most of the languages.
  • The app is initiated first then the stacks are initiated for provisioning the resources.

CDK Stacks

  • CDK stacks are collections of aws resources that are provisioned together.
  • Stacks contain definition for one or more cloud resources in AWS.
  • Stacks are also called as unit of deployment.
  • Any number of stacks can be defined in an application in CDK.

Benefits of AWS CDK

  • Reusability: Constructs in CDK can be used multiple times to create same type of resources. Once written in a programming language construct can be reused as required.
  • Language Support: AWS CDK provides support for multiple languages including JavaScript, Typescript, Python, Java and many more.
  • IDE Usage: Developer can use IDE for both programming as well infrastructure management. Same code can be used from the IDE.
  • Integration Of Other Services: Cloud formation integration in AWS CDK provides users with benefits of cloud formation allowing stack management, templating etc.
  • Updates And Maintain: With the help of cloud formation the infrastructure can be updated easily.

Using the CDK Toolkit

Now we will see how we can use AWS CDK with javascript.

Step 1: Setup The Environment To Use AWS CDK

  • Install the aws cdk using below command.
npm install aws-cdk-lib

Step 2: Create The App And Initialize With CDK:

  • First create project folder with the name required.
  • Go to the folder and initialize the cdk with below command:
cdk init app --language javascript
  • Once the app is initialized add stack to your app.
  • create a new .js file with stack name. let’s add s3 bucket configuration in stack.

Javascript




const cdk = require('aws-cdk-lib');
const s3 = require('aws-cdk-lib/aws-s3');
  
class GfgCdkStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);
  
    new s3.Bucket(this, 'GfgBucket', {
      versioned: true
    });
  }
}
  
module.exports = { GfgCdkStack }


Step 3: Synthesize An AWS CloudFormation Template

  • Now synthesize the application using below command.
cdk synth

The output will display the YAML for the resource and .out file will be generated.

Step 4: Deploy The Stack

  • Finally run below command to deploy the stack.
cdk deploy

The command will create a stack in cloud formation and also provision s3 bucket.

Best Practices For Scaling AWS CDK In Your Organization

  • A development team must be able to monitor and create resources in their account.
  • CI /CD pipelines can be developed to deploy the CDK applications on AWS.
  • Multiple accounts can be used for deploying the applications.
  • pre-configured, secure, scalable, multi-account AWS environment can be created using best architectures.

Conclusion

From this article we had an overlook of AWS Cloud Development Kit. Various use cases of AWS CDK and its working. AWS CDK is a beneficial framework in cloud as the features it offers redefines the process of writing and provisioning cloud resources.

AWS CDK (Cloud Development Kit) – FAQs

How Does AWS CDK Differ From AWS CloudFormation?

While AWS CloudFormation is a declarative language for defining AWS infrastructure as code. AWS CDK allows developers to use imperative programming languages to define infrastructure. CDK generates CloudFormation templates under the hood.

Which Programming Languages Are Supported By AWS CDK?

AWS CDK supports several programming languages, including TypeScript, Python, Java, C#, and others. Developers can choose the language that best suits their preferences and expertise.

Can I Use AWS CDK With Existing AWS Resources?

Yes, AWS CDK can be used with existing AWS resources. You can import existing resources into your CDK application and manage them alongside resources defined in the CDK application.

Is AWS CDK Suitable For Production Environments?

Yes, AWS CDK is suitable for production environments. It is designed to help developers efficiently manage and deploy cloud infrastructure at scale. Many organizations use AWS CDK in their production systems.

How Does AWS CDK Handle Updates And Changes To Infrastructure?

AWS CDK generates AWS CloudFormation templates, and it leverages CloudFormation for stack management. Developers can use CloudFormation features like change sets to review and apply updates to their infrastructure.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads