Container Image Creation Using Lambda Web Adapter
This document explains how to create container images for deployment to AWS Lambda.
Runtime Methods Available with Lambda Web Adapter
While there are methods to use with standard Lambda runtimes like Node.js (in which case LWA is configured in Lambda Layer), this catalog uses container images (Docker) for ease of configuration.
Dockerfile Contents
Configure the Dockerfile as follows. Please refer to inline comments for details on each description.
FROM public.ecr.aws/lambda/nodejs:22 as builder
WORKDIR "/var/task"
ADD . .
# React Router v7 doesn't include node_modules in build results, so we run npm install with --omit=dev flag.
RUN npm install \
&& npm run build \
&& cp package.json .dist/server/ \
&& cp package-lock.json .dist/server/ \
&& cd .dist/server/ \
&& npm install --omit=dev
# Recommended to use official images registered in AWS public ECR
FROM public.ecr.aws/lambda/nodejs:22
# Lambda Web Adapter
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.1 /lambda-adapter /opt/extensions/lambda-adapter
# React Router v7 generates separate .dist/server and .dist/client directories when built.
# The contents of .dist/server are put into containers and deployed to AWS Lambda,
# while .dist/client contents can either be included in the container as well or stored separately in S3 and distributed via CloudFront for cache strategy customization and optimization.
# Server side assets
COPY --from=builder /var/task/.dist/server/index.js /var/task/index.js
COPY --from=builder /var/task/.dist/server/assets /var/task/assets
COPY --from=builder /var/task/.dist/server/node_modules /var/task/node_modules
COPY --from=builder /var/task/.dist/server/package.json /var/task/package.json
# Client side assets
COPY --from=builder /var/task/.dist/client/ /var/task/public/
# Next.js and other web applications have different approaches and configurations for static files,
# so framework-specific custom headers and other configurations may need to be customized to avoid structural issues.
# Consider and verify whether to handle this in the Dockerfile or with CloudFront and Lambda@Edge.
# React Router v7, with its simple configuration and policy of aligning as much as possible with Web Standards,
# has excellent compatibility and affinity as a frontend application using Lambda Web Adapter.
WORKDIR "/var/task"
ENV NODE_ENV=production
ENV PORT=3000
CMD ["npm", "run", "start"]
Reference Materials
For those who want to learn more details about creating Dockerfiles using Lambda Web Adapter, please refer to the following official GitHub documentation: