Skip to content

A deno runtime for AWS Lambda. See example to see how to use with serverless.

License

Notifications You must be signed in to change notification settings

brianleroux/deno-lambda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deno on AWS Lambda

A deno runtime for AWS Lambda.

ci status

Quick Start

From the AWS console:

  1. Download zip files from the releases page.

  2. Create a layer and upload deno-lambda-layer.zip.

Create layer

Layer created

Note its Version ARN.

  1. Create a lambda function from scratch with runtime "provide your own bootstrap".

Create function

Function created

  1. Add a layer using the ARN above.

Add layer to function

  1. Upload deno-lambda-example.zip as function code.

Upload function code

  1. "Save". "Test" (use the default event).

Execution successful


AWS Lambda calls the hello.handler function:

// hello.ts

import { Context, Event } from "https://deno.land/x/lambda/mod.ts";

export async function handler(event: Event, context: Context) {
  return {
    statusCode: 200,
    body: `Welcome to deno ${Deno.version.deno} 🦕`
  };
}

The default is hello.handler but this can be configured by the Handler setting.

Warning

The way the lambda platform works means that promises not awaited by the handler may never be completed. This is because the underlying container can be paused between invocations and will sometimes be shutdown.

export async function badHandler(event: Event, context: Context) {
  somethingAsync(); // not awaited
  return;
}

If you need to return immediately but want to invoke a longer running process you can async-invoke another lambda function (which does the await somethingAsync()).


Configuration

Lambda functions using the deno-lambda-layer:

  • Support Handler i.e. setting the handler file and function.
  • Use HANDLER_EXT to set supported extension e.g. js or bundle.js (default ts).
  • Set DENO_DIR for storing cached assets, default .deno_dir.

Further configuration TBD.

Function code

Create a zip file which contains:

  • an entry point which exports an async function (e.g. hello.ts)
  • any other files needed to run the entry file
  • (optional) .deno_dir directory*

*You can use a different directory by passing it as the DENO_DIR environment variable.

DENO_DIR remapping

Zip the source files and DENO_DIR. In order for compile artifacts to be recovered (avoiding runtime compilation) you need to do the following directory remapping:

# Compile the handler (and fetch dependencies into DENO_DIR).
DENO_DIR=.deno_dir deno fetch hello.ts

# This is the "remapping" step:
cp -R .deno_dir/gen/file/$PWD/ .deno_dir/LAMBDA_TASK_ROOT
# Note: We do the inverse of this operation in bootstrap.

zip lambda.zip -x '.deno_dir/gen/file/*' -r .deno_dir hello.ts  # other source files

In a serverless.yml this can be automatically prior to each upload using the serverless-scriptable-plugin:

plugins:
  - serverless-scriptable-plugin

custom:
  scriptHooks:
    before:package:createDeploymentArtifacts: DENO_DIR=.deno_dir deno fetch api/candidate.ts && cp -R .deno_dir/gen/file/$PWD/ .deno_dir/LAMBDA_TASK_ROOT

See example/serverless.yml.

Examples


Many thanks to Yoshiya Hinosawa's blogpost for the initial work on this runtime.

About

A deno runtime for AWS Lambda. See example to see how to use with serverless.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 64.1%
  • Shell 29.3%
  • Dockerfile 5.9%
  • JavaScript 0.7%