AWS CDK offers an easy way to create AWS resources using a familiar programming language. This blog will teach us how to create the AWS Lambda functions using CDK. We will use typescript for working with CDK. Please let me know if you need the same code in Python language.

Getting Started

Before creating the AWS Lambda function using CDK, you must have the following things ready on your machine.

  • Node installed on your machine – This will be used to install the aws-cdk package.
  • Install AWS CDK on your machine. You can do that using this command `npm install -g aws-cdk`
  • AWS Profile or user credentials – These will be used by CDK project to create resources in your AWS account.

Creating Lambda Function in CDK

For creating the Lambda function, we need to pass lambda code, handler name, and runtime to the Lambda construct of CDK.

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';

export class AwsLambdaStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    new lambda.Function(this, "LambdaFunctionFromCDK", {
      runtime: lambda.Runtime.NODEJS_16_X,
      handler: "index.handler",
      code: lambda.AssetCode.fromAsset("../lambda-code"),
    });
  }
}

For code param, you can pass the path of lambda code on your machine. You can also change runtime depending upon the language you want to use for your lambda code.

Once you are ready with the above code, we can run`cdk deploy` command, and this will create a lambda function in the AWS account. This command also creates a basic Lambda execution role, and you will need to confirm this when prompted for it.

create lambda function prompt
create lambda function prompt

We can verify the lambda function on the AWS console. Here, we can verify that the lambda function has been created with the code we passed.

Lambda function in AWS Console.
Lambda function in AWS Console.

Configuring Lambda memory size and timeout in CDK

We can also configure Lambda memory size and timeout when creating or updating the lambda function from AWS CDK.

new lambda.Function(this, "LambdaFunctionFromCDK", {
      runtime: lambda.Runtime.NODEJS_16_X,
      handler: "index.handler",
      code: lambda.AssetCode.fromAsset("../lambda-code"),
      memorySize: 512,
      timeout: Duration.seconds(10),
    });

The above code will update the existing lambda function to have the size of 512MB and a timeout of 10 seconds. Let us run `cdk diff`and see what we get.

Lambda configuration using CDK
Lambda configuration using CDK

When we deploy this code, we can verify on the AWS console if the lambda function has a memory of 512MB and a timeout of 10 seconds or not.

Lambda Configuration
Lambda Configuration

What happens when we change our lambda code?

Now next question is what will happen if we change our lambda code? Will CDK identify it? Let us check. I have made a small change in lambda code and run `cdk diff`. You should see the output something like below.

cdk diff on lambda code change
cdk diff on lambda code change

We can see that CDK has identified that we have changed our lambda code. So when we run `cdk deploy`, our code will also be updated in the AWS console.

Deleting Lambda Function

Once you are done with testing, we can run `cdk destroy` command. This will delete the lambda function from the AWS console.

Conclusion

In this blog, we have learned how to create a Lambda function using CDK. As you can see, CDK makes it very easy to work with aws resources. Hope to see you in the next blog.

Share with your friends

Similar Posts

Leave a Reply

Your email address will not be published.