A basic express.js application example. You can build and test it locally as a typical Express.js application.
Using AWS Lambda Adapter, you can package this web application into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.
The application can be deployed in an AWS account using the Serverless Application Model. The template.yaml
file in the root folder contains the application definition.
The top level folder is a typical AWS SAM project. The app
directory is an express.js application with a Dockerfile.
FROM public.ecr.aws/docker/library/node:16.13.2-stretch-slim
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.1 /lambda-adapter /opt/extensions/lambda-adapter
EXPOSE 8080
WORKDIR "/var/task"
ADD src/package.json /var/task/package.json
ADD src/package-lock.json /var/task/package-lock.json
RUN npm install --omit=dev
ADD src/ /var/task
CMD ["node", "index.js"]
Line 2 copies lambda adapter binary into /opt/extenions. This is the only change to run the express.js application on Lambda.
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.6.1 /lambda-adapter /opt/extensions/lambda-adapter
The following tools should be installed and configured.
Navigate to the sample's folder and use the SAM CLI to build a container image
$ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
$ sam build
This command compiles the application and prepares a deployment package in the .aws-sam
sub-directory.
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
$ sam deploy --guided
Please take note of the container image name.
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use curl
or a web browser to make a call to the URL
...
---------------------------------------------------------------------------------------------------------
OutputKey-Description OutputValue
---------------------------------------------------------------------------------------------------------
PetStoreApi - URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
---------------------------------------------------------------------------------------------------------
...
$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
We can run the same docker image locally, so that we know it can be deployed to ECS Fargate and EKS EC2 without code changes.
$ docker run -d -p 8080:8080 {ECR Image}
Use curl to verify the docker container works.
$ curl localhost:8080/