Deploy Deno to AWS Lambda
Video descriptionJump to heading
Show how to deploy Deno applications to AWS Lambda (using a community runtimefor Lambda).
Transcript and codeJump to heading
Run Deno on AWS LambdaJump to heading
Running Deno on AWS Lambda? Sure, you can do that. With AWS lambda theserverless pricing can be cheaper than a VPS and can be easier to maintainbecause it can auto scale behind the scenes.
To make that work, we’re going to use the aws-lambda-adapter project to makesure that ourDeno.serve
function runs as we expect it to. This is a popularapproach to deploying to AWS lambda due to control, flexibility, andconsistency.
There’s a nice article on this on the blog if you want to learn more about theseconsiderations.
Let’s take a look at the Dockerfile that we can use to make this work:
# Set up the base imageFROM public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 AS aws-lambda-adapterFROM denoland/deno:bin-2.0.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 -A main.ts || [ $? -eq 124 ] || exit 1CMD ["deno", "-A", "main.ts"]
Then we’ll build the Docker image.
docker build-t my-deno-project.
Now we need to start interfacing with AWS. If this is your first time workingwith AWS, you can create an account:https://aws.amazon.com
And if you haven’t installed the AWS CLI, you can do that too. You know if it’sinstalled by typingaws
into your Terminal or Command Prompt. If that returnsan error you can install with homebrew or follow the instructions through thewebsite:https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
brew install awscli
Then you’ll want to make sure that you’re set up withaws configure
.Everything that it is looking for is in theSecurity Credentials section of theAWS Console.
Use the CLI to create an ECRJump to heading
The ECR is a registry service where we can push our docker container
aws ecr create-repository --repository-name my-deno-project --region us-east-1 | grep repositoryUri
This outputs a URI for the repo: `"repositoryUri":"<<myuserid>>.dkr.ecr.us-west-1.amazonaws.com/my-deno-project",`
Then log in using the URI that comes back
aws ecr get-login-password--region us-east-1|docker login--username AWS --password-stdin<username>.dkr.ecr.us-east-1.amazonaws.com/my-deno-project
Tag the image
docker tag my-deno-project:latest<myProject>.dkr.ecr.us-east-1.amazonaws.com/my-deno-project:latest
Then Push the image to ECR
docker push<myproject>.dkr.ecr.us-west-1.amazonaws.com/my-deno-project:latest
Now we need to create a function that will host our app:
- https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/begin
- Think of a function as being a place where the app is going to run
- Select Create a Function
- Select Container Image Radio Button
- Call the function
tree-app
- Select the app from the Browse Containers button
- Halfway down the page select “Configuration”
- Select
Function URL
- Create a URL
- Select None so the endpoint is public
- Select Save
- Check the app in the browser
One thing to keep in mind with Lambda functions is cold start performance. Coldstarts happen when AWS needs to initialize your function, and it can causeslight delays. There’s a pretty coolblog here that goes through Deno vs. othertools.
Using Deno with AWS Lambda functions is a great way to stand up your app quicklyin a familiar environment.