On this page
How to Deploy Deno to AWS Lambda
AWS Lambda is a serverless computing service provided by Amazon Web Services. Itallows you to run code without provisioning or managing servers.
Here's a step by step guide to deploying a Deno app to AWS Lambda using Docker.
The pre-requisites for this are:
Step 1: Create a Deno AppJump to heading
Create a new Deno app using the following code:
Deno.serve((req)=>newResponse("Hello World!"));
Save this code in a file namedmain.ts
.
Step 2: Create a DockerfileJump to heading
Create a new file namedDockerfile
with the following content:
# Set up the base imageFROM public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 AS aws-lambda-adapterFROM denoland/deno:bin-1.45.2 AS deno_binFROM debian:bookworm-20230703-slim AS deno_runtimeCOPY --from=aws-lambda-adapter /lambda-adapter /opt/extensions/lambda-adapterCOPY --from=deno_bin /deno /usr/local/bin/denoENV PORT=8000EXPOSE 8000RUN mkdir /var/deno_dirENV DENO_DIR=/var/deno_dir# Copy the function codeWORKDIR "/var/task"COPY . /var/task# Warmup cachesRUN timeout 10s deno run -A main.ts || [ $? -eq 124 ] || exit 1CMD ["deno", "run", "-A", "main.ts"]
This Dockerfile uses theaws-lambda-adapter
project to adapt regular HTTP servers, like Deno'sDeno.serve
, to the AWSLambda runtime API.
We also use thedenoland/deno:bin-1.45.2
image to get the Deno binary anddebian:bookworm-20230703-slim
as the base image. Thedebian:bookworm-20230703-slim
image is used to keep the image size small.
ThePORT
environment variable is set to8000
to tell the AWS Lambda adapterthat we are listening on port8000
.
We set theDENO_DIR
environment variable to/var/deno_dir
to store cachedDeno source code and transpiled modules in the/var/deno_dir
directory.
The warmup caches step is used to warm up the Deno cache before the function isinvoked. This is done to reduce the cold start time of the function. Thesecaches contain the compiled code and dependencies of your function code. Thisstep starts your server for 10 seconds and then exits.
When using a package.json, remember to rundeno install
to installnode_modules
from yourpackage.json
file before warming up the caches orrunning the function.
Step 3: Build the Docker ImageJump to heading
Build the Docker image using the following command:
docker build-t hello-world.
Step 4: Create an ECR Docker repository and push the imageJump to heading
With the AWS CLI, create an ECR repository and push the Docker image to it:
aws ecr create-repository --repository-name hello-world--region us-east-1|grep repositoryUri
This should output a repository URI that looks like<account_id>.dkr.ecr.us-east-1.amazonaws.com/hello-world
.
Authenticate Docker with ECR, using the repository URI from the previous step:
aws ecr get-login-password--region us-east-1|docker login--username AWS --password-stdin<account_id>.dkr.ecr.us-east-1.amazonaws.com
Tag the Docker image with the repository URI, again using the repository URIfrom the previous steps:
docker tag hello-world:latest<account_id>.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
Finally, push the Docker image to the ECR repository, using the repository URIfrom the previous steps:
docker push<account_id>.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
Step 5: Create an AWS Lambda functionJump to heading
Now you can create a new AWS Lambda function from the AWS Management Console.
- Go to the AWS Management Console andnavigate to the Lambda service.
- Click on the "Create function" button.
- Choose "Container image".
- Enter a name for the function, like "hello-world".
- Click on the "Browse images" button and select the image you pushed to ECR.
- Click on the "Create function" button.
- Wait for the function to be created.
- In the "Configuration" tab, go to the "Function URL" section and click on"Create function URL".
- Choose "NONE" for the auth type (this will make the lambda function publiclyaccessible).
- Click on the "Save" button.
Step 6: Test the Lambda functionJump to heading
You can now visit your Lambda function's URL to see the response from your Denoapp.
🦕 You have successfully deployed a Deno app to AWS Lambda using Docker. You cannow use this setup to deploy more complex Deno apps to AWS Lambda.