A deno runtime for AWS Lambda.
From the AWS console:
-
Download zip files from the releases page.
-
Create a layer and upload
deno-lambda-layer.zip
.
Note its Version ARN.
- Create a lambda function from scratch with runtime "provide your own bootstrap".
- Add a layer using the ARN above.
- Upload deno-lambda-example.zip as function code.
- "Save". "Test" (use the default event).
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.
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()
).
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
orbundle.js
(defaultts
). - Set
DENO_DIR
for storing cached assets, default.deno_dir
.
Further configuration TBD.
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.
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
.
- Hello example (
deno-lambda-example.zip
) - Web example (behind API Gateway) using dynamodb, see
/example
directory.
Many thanks to Yoshiya Hinosawa's blogpost for the initial work on this runtime.