Great performance < 10ms on event processing and < 50MB memory consumption
No need to ship the Dart runtime
Multiple event handlers
Typed events
Custom events
this package requires Dart >= 2.6
currently dart2native only supports building for the platform it is run on, so you must either build on a Linux machine or use docker
🚀 Introduction
Dart is an unsupported AWS Lambda runtime language. However, with a custom runtime you can support virtually every programming language.
There are two ways in which you could use Dart. You could bundle the Dart Runtime in a Lambda layer and use JIT compilation within the lambda execution to run a Dart program. The other is to compile a shippable binary of the Dart program.
Dart >= 2.6 introduced dart2native. The tool uses AOT (ahead-of-time) to compile a Dart program to native x64 machine code. This standalone executable is native machine code that's compiled from the specific Dart file and its dependencies, plus a small Dart runtime that handles type checking and garbage collection.
We decided to use the latter approach rather then the just-in-time compilation of Dart files. The main reason for this decision is that we wanted to avoid having to ship and maintain a standalone Dart runtime version. We would eventually have to deprecate versions, or always update the version when moving forward. Furthermore, shipping a binary has the advantage of having an always runnable version of your function in addition to performance benefits.
Every serverless workflow command should work out of the box. The template also includes an example GitHub actionsconfiguration file which can unlock a virtuous cycle of continuous integration and deployment
( i.e all tests are run on prs and every push to master results in a deployment).
Custom deployment
The deployment is a manual task right now. We have a example/build.sh script which makes the process a bit easier. There are three steps to get your code ready to be shipped.
Compile your Dart program with dart2native main.dart -o bootstrap
Create a .zip file with zip lambda.zip bootstrap
Upload the lambda.zip to a S3 bucket or use the AWS CLI to upload it
again, you have to build this on Linux, because dart2native does not support cross-compiling
When you created your function and upload it via the the console. Please, replace arn:aws:iam::xxx:xxx with the role you created for your lambda.
The example in main.dart show how the package is intended to be used. Because dart2native does not support cross-platform compilation, you can use the google/dart (:warning: if you are on Linux you can ignore this) container to build the project. The build.sh script automates the build process in the container.
# will build the binary in the containercd example; docker run -v $PWD:/app --entrypoint app/build.sh google/dart && zip lambda.zip bootstrap && rm bootstrap
You will see the lambda.zip which you can upload manually, or use the client of your choice.
What you see in the example is an example of the interface to the Dart Runtime that we created.
You will have to make aws_lambda_dart_runtime a dependency of your project.
...dependencies:aws_lambda_dart_runtime:...
We support using multiple handlers in one executable. The following example shows to register one handler.
import'package:aws_lambda_dart_runtime/aws_lambda_dart_runtime.dart';
voidmain() async {
/// This demo's handling an API Gateway request.finalHandler<AwsApiGatewayEvent> helloApiGateway = (context, event) async {
final response = {"message": "hello ${context.requestId}"};
/// it returns an encoded response to the gatewayreturnInvocationResult(
context.requestId, AwsApiGatewayResponse.fromJson(response));
};
/// The Runtime is a singleton. You can define the handlers as you wish.Runtime()
..registerHandler<AwsApiGatewayEvent>("hello.apigateway", helloApiGateway)
..invoke();
}
This example registers the hello.apigateway handler with the function to execute for this handler. The handler function is typed to receive a Amazon API Gateway Event and it returns a response to the invoking gateway. We support many other events. Handler functions get a Context injected with the needed information about the invocation. You can also register your own custom events via Runtime.registerEvent<T>(Handler<T>) (see events).
The data folder contains examples of the used events. We use this to run our tests, but you can also use them to implement new features. If you want to request the processing of a new event, you may provide a payload here.