Skip to content

Create Lambda function using AWS CDK

Posted on:August 5, 2022 at 05:17 AM

AWS CDK offers an easy way to create AWS resources using a familiar programming language. This blog will teach you how to create AWS Lambda functions using CDK. We will use TypeScript to work with CDK. You can also use other languages like Python or Go to work with CDK.

Table of contents

Open Table of contents

Getting Started

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

Creating Lambda Function in CDK

To create the Lambda function, we must 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 on the language you want to use for your lambda code.

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

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

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 a size of 512MB and a timeout of 10 seconds. Let us run cdk diffand see what we get.

Lambda configuration using CDK

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

Lambda Configuration

What happens when we change our lambda code?

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

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 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. I hope to see you in the next blog.