How to Deploy Dockerized AWS Lambda Using AWS CDK

How to Deploy Dockerized AWS Lambda Using AWS CDK

In this blog post, we'll explore a step-by-step guide on how to deploy Dockerized AWS Lambda functions using the AWS Cloud Development Kit (CDK) with TypeScript.

AWS CDK, a software development framework for defining cloud infrastructure in code, has eased cloud resources management by facilitating the use of familiar programming languages. The combination of Docker, AWS Lambda, and CDK takes it to another level, providing a seamless development and deployment process.

Prerequisites

Before we start, make sure you have the following:

  • An AWS Account

  • AWS CLI installed and configured

  • Docker installed

  • Node.js and npm installed

  • AWS CDK installed

Step 1: Initialize a new AWS CDK project

First, create a new directory for your project and navigate to it:

mkdir cdk-docker-lambda && cd cdk-docker-lambda

Initialize a new AWS CDK project:

cdk init --language=typescript

This command creates a new CDK project with TypeScript as the programming language.

Step 2: Create a Dockerfile

Now, let's create a Dockerfile. This file is a set of instructions Docker will use to create your Docker image. Here's an example of a Dockerfile for a Node.js application:

FROM public.ecr.aws/lambda/nodejs:14

WORKDIR /var/task

COPY . .

RUN npm install

CMD [ "index.handler" ]

This Dockerfile starts from a public Amazon ECR image for Node.js 14, copies your application code into the image, installs your npm dependencies, and sets index.handler as the command that will be executed when your Lambda function is invoked.

Note: The Folder Structure should be like this

.
├── bin
├── lambdas
│   └── index.js
│   └── Dockerfile
├── lib

Step 3: Write your AWS CDK Stack

Now, let's define our AWS CDK stack. In the lib directory, there's a file called cdk-docker-lambda-stack.ts Open this file and write your stack:

import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { DockerImageCode, DockerImageFunction } from 'aws-cdk-lib/aws-lambda';
import path = require('path');

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

    new DockerImageFunction(this, 'docker-lambda-function', {
      functionName: "docker-lambda-fn",
      code: DockerImageCode.fromImageAsset(path.join(__dirname, '../lambdas')),
      memorySize: 512,
      timeout: Duration.minutes(2),
      description: "This is dockerized AWS Lambda function"
    });
  }
}

In this code, we're defining a new Lambda function that uses a Docker image as its code. lambda.DockerImageCode.fromImageAsset takes the path to the directory containing your Dockerfile and builds your Docker image.

Step 4: Deploy your AWS CDK Stack

Compile your TypeScript code:

npm run build

Next, you'll need to bootstrap your AWS environment to use the CDK if you haven't already:

cdk bootstrap

Finally, deploy your AWS CDK stack:

cdk deploy

This command will build your Docker image, push it to an Amazon ECR repository, and create your AWS Lambda function with the Docker image.

Note: If the deploy command failed due to ECR login

You can use the following command:

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin aws-account-id.dkr.ecr.us-east-1.amazonaws.com

After login success, you can cdk deploy again

Conclusion

In this blog post, we've seen how to use the AWS CDK with TypeScript to deploy Dockerized AWS Lambda functions. This combination allows you to define your AWS infrastructure using a familiar programming language, leverage the power of modern software practices like version control and CI/CD, and package your Lambda functions as Docker containers. This last point is particularly powerful, as it enables you to use any programming language that can run inside a Docker container, not just those natively supported by AWS Lambda.

Did you find this article valuable?

Support Mikaeels Blog by becoming a sponsor. Any amount is appreciated!