Deploy docker image to AWS Lambda

Introduction

In my previous article, I provided instructions on using lambda to implement a function to resize images on demand; now, I will provide instructions on deploying a docker image to lambda.

The advantage of using Lambda is pay on demand, suitable for types of projects newly released in the early stages when there are not yet many uses; deployment with Lambda helps simplify the process for the dev team to focus on feature development; resource management will be managed by AWS, and will automatically scale according to user usage needs.

Detail

Using AWS CDK, create the file lib/deploy-lambda-stack.ts

import * as cdk from "aws-cdk-lib"
import * as ecr from "aws-cdk-lib/aws-ecr"
import * as iam from "aws-cdk-lib/aws-iam"
import * as lambda from "aws-cdk-lib/aws-lambda"
import { Construct } from "constructs"

export class DeployLambdaStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)

const imageUri = process.env.LAMBDA_IMAGE || ""
const repoName = imageUri.split("/")[1].split(":")[0]
const tagOrDigest = imageUri.split(":")[1]

const repository = ecr.Repository.fromRepositoryName(
this,
"ExistingRepo",
repoName,
)

const nestJsLambda = new lambda.DockerImageFunction(this, "NestJsHandler", {
code: lambda.DockerImageCode.fromEcr(repository, { tagOrDigest }),
memorySize: 1024,
timeout: cdk.Duration.seconds(30),
environment: {
BUCKET: process.env.BUCKET || "",
},
})

nestJsLambda.addToRolePolicy(
new iam.PolicyStatement({
actions: ["s3:*"],
resources: ["*"],
}),
)

const fnUrl = nestJsLambda.addFunctionUrl({
authType: lambda.FunctionUrlAuthType.NONE,
})
new cdk.CfnOutput(this, "NestJsFunctionUrl", {
value: fnUrl.url,
})
}
}

The above code block performs the following steps:

  • Extract Repository name and Tag information from environment variables.
  • Connect to an existing image repository (ECR).
  • Initialize a Lambda Function running with a Docker Image with configuration for memory size, timeout, and environment variables.
  • Grant permission to Lambda to be able to operate with the S3 storage service.
  • Create and export a public URL path (Function URL) so you can call Lambda directly from a browser or Postman without going through API Gateway.


Update file bin/aws-cdk.ts

#!/usr/bin/env node
import 'dotenv/config';
import * as cdk from "aws-cdk-lib/core"
import { DeployLambdaStack } from '../lib/lambda/deploy-lambda-stack';

const app = new cdk.App()
new DeployLambdaStack(app, "DeployLambdaStack")


Result when successfully deployed

DeployLambdaStack
Deployment time: 23.98s

Outputs:
DeployLambdaStack.NestJsFunctionUrl = https://ph6fdwnabuimneajfrs5dwhswa0gxksy.lambda-url.ap-southeast-1.on.aws/

Total time: 29.66s


Resource created on AWS


Try accessing using Postman


You can view logs via AWS CloudWatch


Or use AWS Cli to view logs as follows

$ aws logs tail /aws/lambda/DeployLambdaStack-NestJsHandler8CA6287B-PMAEQJENjEMe --follow
2026-03-18T03:49:33.857000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:33.855Z info [NestFactory] Starting Nest application... +0ms
2026-03-18T03:49:33.875000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:33.875Z info [InstanceLoader] ConfigHostModule dependencies initialized +19ms
2026-03-18T03:49:33.887000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:33.887Z info [InstanceLoader] ConfigModule dependencies initialized +12ms
2026-03-18T03:49:33.890000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:33.890Z info [InstanceLoader] AppModule dependencies initialized +3ms
2026-03-18T03:49:34.160000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:34.160Z info [RoutesResolver] AppController {/}: +270ms
2026-03-18T03:49:34.164000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:34.164Z info [RouterExplorer] Mapped {/, GET} route +4ms
2026-03-18T03:49:34.165000+00:00 2026/03/18/[$LATEST]9b534c626f5b44348495e3b3d0ebb09c 2026-03-18T03:49:34.165Z info [RoutesResolver] S3Controller {/s3}: +1ms


Happy coding!

See more articles here.

Comments

Popular posts from this blog

All Practice Series

Kubernetes Deployment for Zero Downtime

Deploying a NodeJS Server on Google Kubernetes Engine

Setting up Kubernetes Dashboard with Kind

Using Kafka with Docker and NodeJS

Practicing with Google Cloud Platform - Google Kubernetes Engine to deploy nginx

Monitoring with cAdvisor, Prometheus and Grafana on Docker

Kubernetes Practice Series

NodeJS Practice Series

Sitemap