Deploying Lambda functions can be a challenging task, especially with frequent updates and integration with other AWS Services, this is where AWS CDK comes to the rescue.

In this tutorial, we’ll explore the process of setting up and initialising a CDK project, writing Lambda functions using TypeScript, and then deploying them via AWS CDK.

Understanding AWS CDK

Before diving into deployment, let’s briefly understand what AWS CDK is all about. 

It is an open-source software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. What makes CDK special is that, it lets you use common programming languages like TypeScript, Python, Java, and C#. This means you can use your coding skills and tools you’re already familiar with to describe your infrastructure as code (IaC) and automate the deployment process. 

Getting Started

For serverless deployment of Lambda function using AWS CDK, you’ll need to set up your development environment first. Make sure you have the AWS CLI and CDK installed, and your AWS credentials are configured properly. Once you have everything set up, you can create a new CDK project using your preferred programming language. 

For this example, we’ll use TypeScript.

Step 1:

Execute the following command to install the CDK Node package, enabling the utilisation of AWS CDK commands via the CLI.

npm install -g cdk

Confirm whether AWS-CLI and AWS-CDK are installed or not, using the following commands:

Step 2:

Create a CDK project.

Initialise a new CDK project using the following commands:

mkdir lambda-cdk-test
cd lambda-cdk-test
cdk init –language=typescript

Step 3: 

Install necessary dependencies using the following command: 
npm install @aws-cdk/aws-lambda @aws-cdk/aws-lambda-nodejs

Step 4: 

Open this repository in VS Code using the following command:
code.

It will open your code inside the VS Code.

Step 5: 

Create the Lambda function.

Modify the file lib/cdk-test-stack.ts to create our Node.js Lambda function. Update the code with the below one:

import * as cdk from ‘aws-cdk-lib’;
import * as lambda from ‘aws-cdk-lib/aws-lambda’;
export class LambdaCdkTestStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    // defines an AWS Lambda resource
    const hello = new lambda.Function(this, ‘HelloHandler’, {
      runtime: lambda.Runtime.NODEJS_16_X,   
      code: lambda.Code.fromAsset(‘./src’),  
      handler: index.handler’                
    });
  }
}

Step 6:

Now, we’re going to make a folder named “src” in the main directory of your project. And inside this folder, create a file called “index.js” and put the provided code in it.

exports.handler = async function(event) {
  console.log(“request:”, JSON.stringify(event, undefined, 2));
  return {
    statusCode: 200,
    headers: { “Content-Type”: “text/plain” },
    body: `Hello, CDK! You’ve hit`
  };
};

Your folder structure should look like this :

Step 7: 

Deploy the stack using the following commands:
cdk synth – Synthesizes and prints the CloudFormation template for one or more specified stacks.

cdk deploy – Deploys one or more specified stacks.

Step 8 :
Check your AWS Lambda console for the newly created Lambda function.

Test the Lambda function:

Final Step (Most Important): Once you’re done with your project or want to ensure everything is safely removed, you can execute the following command for the AWS CDK testing:

cdk destroy

The “cdk destroy” command is used to delete the resources that might be created by the AWS Cloud Development Kit (CDK).

Conclusion

Deploying a Lambda function using CDK AWS streamlines the development process and allows for seamless integration with other AWS services. By defining infrastructure as code, you can version-control your infrastructure, automate AWS serverless deployments, and ensure consistency across environments. 

Whether you’re building a simple microservice or a complex application, AWS CDK empowers you to focus on innovation while leaving the heavy lifting of infrastructure management to the framework. 

So why wait? Dive into the world of serverless with AWS CDK and unleash the full potential of AWS Lambda. Happy coding! 
For more such technology-based information, you can visit our CloudZenia website.

Leave a Reply

Your email address will not be published. Required fields are marked *

Ready to Dive into Your Cloud Journey?

CloudZenia can help you wherever you are in your cloud journey. We deliver high quality services at very affordable prices.

Loading...