Dart on AWS Lambda
Dart on AWS Lambda
Article here: https://flatteredwithflutter.com/dart-on-aws-lambda/
Summary
The provided web content discusses how to run Dart applications on AWS Lambda using a custom runtime, deploy them with the Serverless Framework, and add HTTP endpoints for different request methods.
Abstract
The article titled "Dart on AWS Lambda" explains the process of setting up a Dart runtime environment on AWS Lambda, which is made possible by AWS's support for custom runtimes. It details the steps to write AWS Lambda functions using Dart, including the setup of the Serverless Framework and the use of the serverless-dart plugin to handle Dart-specific deployments. The article guides developers through creating a Dart project, configuring the serverless.yml file, and implementing Dart handlers for HTTP endpoints, such as a GET endpoint using helloApiGateway and a POST endpoint using postApiGateway. It also covers the registration of these handlers with the Dart runtime and the deployment of the Lambda functions to AWS using a Makefile or direct serverless commands. The article assumes familiarity with AWS services and the Serverless Framework and provides code examples and screenshots to assist developers in successfully deploying Dart applications on AWS Lambda.
Opinions
serverless-dart plugin is highlighted as a crucial tool for deploying Dart applications, suggesting that it simplifies the deployment process.serverless-dart plugin.Dart on AWS Lambda
Article here: https://flatteredwithflutter.com/dart-on-aws-lambda/
We will cover briefly:
Note: This article assumes the reader has an AWS account and knows about the serverless framework

With the introduction of custom AWS Lambda runtimes, it is possible to implement a runtime in any programming language.
AWS Lambda provides an HTTP API for custom runtimes to receive invocation events and send response data back within the Lambda execution environment.
Using the custom AWS Lambda runtime, we can also run Dart on Lambda with the recently published runtime for the Dart programming language.
aws_lambda_dart_runtime that packages the dart runtime to build lambda functions with AWS events.
In this article, we will go with the Serverless Framework approach
Pre-Requisite: You should have the aws cli configured in your machine. Check here
We will make use of the serverless framework to write lambda functions.
As per the docs,
Develop, deploy, troubleshoot and secure your serverless applications with radically less overhead and cost by using the Serverless Framework. The Serverless Framework consists of an open source CLI.
Serverless allows us to focus on the code, while it takes care of the setting up policies, and provisioning of required infrastructure onto AWS.
To install serverless, follow here. Personally, I configured using the below
npm install -g serverlessserverless has no support for dart, but there is a serverless plugin for Dart applications, called serverless-dart We will install serverless-dart usingnpm i -D serverless-dartNote: In case something goes wrong, try installing the
serverlessframework, see above.
Once the above setup is configured, it’s time to create a dart project. We make use of the below
npx serverless install \
--url https://github.com/katallaxie/serverless-aws-dart \
--name helloThis will download the source of a sample Dart application and unpack it as a service named hello in a directory called hello

.github/workflows hence, deleted the folder.pubspec.yaml we will see the package aws_lambda_dart_runtime and build_runner under dev dependencies. We give both a version number.dependencies:
aws_lambda_dart_runtime: ^1.1.0dev_dependencies:
build_runner: ^2.1.0serverless.yml which is the brain behind this application. We modify the yml file with thisservice: hello
provider:
name: aws
runtime: dart
lambdaHashingVersion: 20201221
memorySize: 128package:
individually: trueplugins:
- serverless-dartfunctions:
hello:
handler: main.hello
events:
- http:
path: /hello
method: GETservice: Name of the service (hello in our case)
functions: The functions to be exposed, along with their entry points. We currently have a function called hello Inside that, we specify the different configurations.
handler: This is the AWS registered handler and should be the same as inside our main.dart
events: The events that trigger this function. This includes http
http: REST API endpoint (API Gateway v1)
path: Path for this endpoint/hello and method being GET
Note: For detailed description: see here
We are left with one important file to inspect main.dart . We will change the contents of the file to below
// IMPORT OMITTED FOR BREVITYvoid main() async {
Handler<AwsApiGatewayEvent> helloApiGateway=(context,event) async { final resp = {
'message': 'Hello to ${context.requestId}',
'host': '${event.headers.host}',
'userAgent': '${event.headers.userAgent}',
}; final response = AwsApiGatewayResponse(
body: json.encode(resp),
isBase64Encoded: false,
statusCode: HttpStatus.ok,
headers: {
"Content-Type": "application/json",
},
); return response;
};Runtime()
..registerHandler<AwsApiGatewayEvent>("main.hello", helloApiGateway)
..invoke();
}mainfunction.aws_lambda_dart_runtime package.We define a helloApiGateway which is the handler to be invoked once our API gets hit. This is of the type AwsApiGatewayEvent and exposes two important fields: Context and AwsApiGatewayEvent
Context contains the Lambda execution context information. It comprises various parameters such as requestId etc.AwsApiGatewayEvent is one of the events supported by the package. An Event is basically an abstraction for every event that can be ingested by a handler. It comprises of implementations such as AwsCognitoEvent , AwsS3Event , AwsKinesisDataStreamEvent etcIn our case, we register the event of type AwsApiGatewayEvent Hence, we can extract the details out of the incoming request. These details include headers , body , queryStringParameters etc
AwsApiGatewayResponsebody to the sent, along with the additional parameters such as isBase64Encoded , statusCode and headersOnce the handler is written, now we need to register it to our Runtime
registerHandler which takes in two required parameters: a string name and our gatewaymain.hello (which is the same as specified inside the serverless.yml)invoke to run the Runtime in loop and digest events that are fetched from the API.Inside our serverless.yml add another endpoint for the post-service
functions:
hello:
handler: main.hello
events:
- http:
path: /hello
method: GET
postRequest:
handler: main.postRequest
events:
- http:
path: /postRequest
method: POST// POST BODY TO THE API
{
"name":"aseem"
}postApiGateway which is also of the type AwsApiGatewayEventHandler<AwsApiGatewayEvent> postApiGateway=(context,event) async { final requestBody = event.body;
final requestModel = postRequestModelFromJson(requestBody); final responseBody = ResponseModel(
message: 'Hello ${requestModel.name} from AWS Lambda!!',
); final response = AwsApiGatewayResponse(
body: responseModelToJson(responseBody),
isBase64Encoded: false,
statusCode: HttpStatus.ok,
headers: {
"Content-Type": "application/json",
},
); return response;
};event convert it into a model and extract the name parameter.ResponseModel with the message parameter.AwsApiGatewayResponseInside our main dart, we register our post handler as
Runtime()
..registerHandler<AwsApiGatewayEvent>("main.hello", helloApiGateway) ..registerHandler<AwsApiGatewayEvent>(
"main.postRequest",
PostRequest().postApiGateway,
)
..invoke();Note: We specify
main.postRequest(which is the same as specified inside theserverless.yml)
Till now, we have created our lambda function using dart. Now it’s time to deploy it.
Note: Make sure docker is running on your machine
We will create a Makefile and inside it put
.PHONY: build clean deploydeploy:
npx serverless deployremove: npx serverless remove
For deploying the lambda, we can call
make deployIf everything was successful, you should see something like this

For removing the lambda, we can call
make removeNote: In case you don’t want to create a makefile, simply run
npx serverless deploy
Other articles:

Dart Lambda Get endpointSource CodeFollow Flutter Community on Twitter: https://www.twitter.com/FlutterComm
Alexander Nguyen1-page. Well-formatted.
Andrew ZuoI recently saw this meme about async and await.
Afan KhanHere’s why Microsoft considers React a mistake for Edge.
Olenin SlavaAre you wondering why Zig programmers make so much money? Guess what? It is not what you are thinking.