A Comprehensive Guide to AWS CDK: Creating and Deploying Resources

AWS to CDK Detailed Guide

admin

Product Manager

What is AWS CDK?

AWS Cloud Development Kit (AWS CDK) is an open-source software development framework used to define cloud infrastructure in code and provision it through AWS CloudFormation. AWS CDK allows developers to use familiar programming languages to define infrastructure, making it easier to create reusable, version-controlled, and testable infrastructure code.

Prerequisites

Before starting, ensure the following prerequisites are met:

  • Node.js and npm : Installed on your machine.
  • AWS CLI : Installed and configured with appropriate credentials.
  • CDK Toolkit : Installed globally using the command: npm install -g aws-cdk
  • Basic understanding : Familiarity with AWS services, especially Lambda and CloudFormation.

Why Use AWS CDK?

  • Code Reusability : Write infrastructure code once and reuse it across projects.
  • Programming Language Support : Use preferred languages instead of YAML/JSON.
  • Improved Productivity : Built-in constructs simplify and accelerate development.
  • Integrated Testing : Validate infrastructure with standard unit testing tools.

What is AWS Lambda?

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You simply upload your code, and Lambda takes care of the rest. It scales automatically based on the number of incoming requests and ensures high availability.

In this project, AWS Lambda is used to deploy a simple ‘Hello World’ function. This function will be triggered automatically whenever required, demonstrating how AWS CDK simplifies serverless application deployment.

Step-by-Step Implementation: Creating an AWS Lambda Function

Step 1: Initialize an AWS CDK Project

Run the following command to initialize a new CDK app in TypeScript:
init app –language typescript

This command sets up the basic structure of a CDK project, including:

  • bin/ : Contains the entry point for the CDK app.
  • lib/ : Contains the stack definition file (e.g., aws-cdk-stack.ts).
  • cdk.json : Contains configuration for the CDK CLI.
  • package.json : Manages project dependencies.

Step 2: Modify Files

Next, create and modify the following files:

  • Create a new file : `helloworld.js `

    exports.handler = async (event, context) => { return "Hello World CDK"; };

  • Modify aws-cdk-stack.ts : Define a Lambda function.

    import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda'; import { join } from 'path'; export class AwsCdkStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const handler = new Function(this, 'Hello-World-CDK', { runtime: Runtime.NODEJS_LATEST, memorySize: 512, handler: 'helloworld.handler', code: Code.fromAsset(join(__dirname, '../lambdas')), }); } }

Step 3: Deploy Resources

Before deploying the resources, ensure your AWS environment is bootstrapped. The following commands prepare, synthesize, and deploy the resources:

  • Bootstrap your AWS environment :

    cdk bootstrap The `cdk bootstrap` command sets up an S3 bucket and other required resources for the deployment process.

  • Synthesize the stack :

    cdk synth The `cdk synth` command synthesizes the stack and generates a CloudFormation template for the defined resources.

  • Deploy the stack :

    cdk deploy The `cdk deploy` command deploys the synthesized CloudFormation stack, creating the resources in AWS.

Created Resources:

These commands create a CloudFormation stack, which provisions the required resources like Lambda functions. Here’s a summary of the created resources:

  • Lambda Function : Executes the handler `helloworld.handler`.
  • CloudFormation Stack :Manages the deployment lifecycle.

Step 4: Validate Resources

Once deployed, you can view the created resources in the AWS Management Console. The Lambda function should appear under the Lambda service. Below is a sample test output of the deployed function:
     {“statusCode”: 200, “body”: “Hello World CDK”}.

Code Reference

You can refer to the complete code for this blog in my GitHub repository: [GitHub Repository Link](https://github.com/rasheshved/aws-cdk-lambda.git).