Introduction
Welcome to part one of my series "Going Serverless with Dart". The point of this series is to show you how to write and deploy serverless logic (also known as cloud functions) to the most popular cloud providers: AWS and GCP.
Before we start, I want to briefly explain what serverless (computing) actually means. I really like this explanation from from spiked usage like a DDOS attack.
Amazon offers its serverless components through a service called AWS (Amazon Web Services). Serverless (aka cloud) functions on AWS are called Lambdas.
They are developed based on a technology called a custom runtime for Dart:
. You will notice one thing, the package has not been updated in the last 3 years (at the time of writing).
Thankfully, there is a . These packages are generated high-level APIs for AWS services.
With the custom runtime and all services accessible via packages, let's get to coding.
Writing your first lambda in Dart
Given all the possibilities with lambdas, I decided to show you how to write a lambda that reacts to a DynamoDB trigger. This is extremely useful for many use cases you might want to cover, especially if you are using AWS Amplify with Flutter, as it comes with DynamoDB by default.
ℹ️ You can find the full code on package by Agilord. We can create the client using the credentials available from
RuntimeContext. To make this easier to reuse, we can create an extension.
CODEextension ContextExtensions on RuntimeContext {
DynamoDB get dynamoDb => DynamoDB(
region: region,
credentials: AwsClientCredentials(
accessKey: accessKey,
secretKey: secretAccessKey,
sessionToken: sessionToken,
),
);
}
Lastly, we must register the function handler using the
invokeAwsLambdaRuntimemethod.
CODEFuture<void> main(List<String> args) async {
await invokeAwsLambdaRuntime([handleTodoCreation]);
}
We can use the AWS Console to test the function, but first, we must deploy it.
Deployment
There are many ways to deploy an AWS Lambda, but the two main ones are uploading a .zip file or Docker image or using Infrastructure-as-Code solutions like Serverless Framework, AWS CDK, or Terraform.
I'll show you how to deploy using a .zip file uploaded through the AWS Console since it also teaches you a little bit about which permissions you need to grant.
ℹ️ If you are curious about IaC deployment, I would recommend using AWS CDK. to avoid your data being compromised or leaving your endpoint vulnerable to attacks like DDOS. Common techniques include securing the Lambda with .
I've prepared a runnable which instructs docker how to build an image for AWS.
After running the script, you should have an
outputfolder withboostrapandfunction.zipfiles inside. Thisfunction.zipis what you will upload to AWS.
Create a lambda and upload .zip
- Go to Lambda Console:
CODE* Open AWS Console
* Search for "*Lambda*"
* Click "*Create function*"
- Configure basic settings:
CODE* Select "*Author from scratch*"
* Use the function name you specified in the handler (`on-create-todo` in the example)
* For Runtime, select "*Amazon Linux 2*"
* Architecture: select *x86\_64* or *arm64*, mine was arm64
* Click "*Create function*"
- Upload your zip:
CODE* In the Code tab of your function
* Click "*Upload from*" dropdown
* Select "*.zip file*"
* Upload your Dart zip file
* Click "*Save*"
- Configure the handler:
CODE* In the Runtime settings section
* Click "*Edit*"
* Set handler to `on-create-todo`
* Click "*Save*"
Create a Dynamo DB trigger
In the Configuration tab, click "Add Trigger".
Then:
Select "DynamoDB" as the trigger source
Find the
todostable and click the long link
Make sure "Activate Trigger" is checked ☑️
Configure Environment Variables
In the Configuration tab, find the "Environment variables" sub-tab and click "Edit".
Click "Add environment variable"
The key should be:
AWS_EXECUTION_ENV
The value should be:
AWS_Lambda_provided.al2
Click "Save"
This is needed for the
RuntimeContextof the lambda.
Grant permissions
Lastly, we want to grant permissions.
Click on the Configuration tab of your Lambda
Click on "Permissions"
Click on the role name listed under "Execution role"
Add the required policy:
CODE* In the IAM role page, click "*Add permissions*" → "*Create inline policy*"
* Choose **JSON** and paste this policy:
CODE```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetRecords",
"dynamodb:GetShardIterator",
"dynamodb:DescribeStream",
"dynamodb:ListStreams",
"dynamodb:UpdateItem"
],
"Resource": [
"arn:aws:dynamodb:<region>:<account-id>:table/todos/stream/*",
"arn:aws:dynamodb:<region>:<account-id>:table/todos"
]
}
]
}
```
CODEYou can find the region and the account id in the Lambda function ARN:

Click "Next"
Give it a name (e.g.,
TodosStreamReadAccess)
Click "Create policy"
Testing the lambda
Firstly, you can test the lambda using the "Test" tab of of your Lambda and by providing the JSON specified at the beginning.
If something crashes or doesn't work, the AWS console error logs should be helpful. In case they are not, modify the lambda to output more errors and redeploy until you can solve the problem.
The real test is done by going the the DynamoDB.
Click "View all tables"
Click on the
todostable
Click "Explore table items"
Click "Create item"
Create an item similar to this:
or LinkedIn.
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR