Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
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:

main.ts
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-adapterproject 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.

  1. Go to the AWS Management Console andnavigate to the Lambda service.
  2. Click on the "Create function" button.
  3. Choose "Container image".
  4. Enter a name for the function, like "hello-world".
  5. Click on the "Browse images" button and select the image you pushed to ECR.
  6. Click on the "Create function" button.
  7. Wait for the function to be created.
  8. In the "Configuration" tab, go to the "Function URL" section and click on"Create function URL".
  9. Choose "NONE" for the auth type (this will make the lambda function publiclyaccessible).
  10. 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.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp