Serverless service / Function as a service allowing to run code without having to worry about underlying hardware and OS.
Event driven: the lambda is triggered by an event.
Pay only for what you use: per request and based on the duration of the code execution.
Use cases
Data transformation (Kinesis Data Stream as input)
File processing (when uploaded to S3 bucket)
Website backend microservice
Scheduled tasks
Bad use cases
Long running processes (timeout after 15mn)
Constant workload (no scalability and high cost)
Large code base (needed to be loaded at startup)
State management (lambda are stateless)
Anti-patterns
Monolithic function
increase package size
hard to enforce least privilege permissions
hard to upgrade, maintain and test
Recursion
endless loop
Orchestration
avoid complex workflow logic
ties lambda with other systems
instead consider AWS Step Functions or EventBridge
Chaining (synchronously invoke another lambda)
instead use EventBridge or QueueService
Waiting (synchronously call services or databases)
instead use asynchronous calls
Runtime
OS
Libraries
Programming language (.NET, Node.js, Python, Go, Ruby, Java)
Environnement variables
DOTNET_STARTUP_HOOKS
ex: path to an assembly to inject logging
Wrapper scripts
Execute the wrapper on top of the runtime and the lambda function.
run shell commands and binaries
Use AWS_LAMBDA_EXEC_WRAPPER to point to your wrapper script.
Using the C# 9's top-level statements feature, you generate an executable assembly which will be run by the Lambda. You provide Lambda only with the name of the executable assembly to run.
varhandler = async (string argument1, ILambdaContext context) => { };
// bootstrap the Lambda runtime and pass it the handler methodawait LambdaBootstrapBuilder.Create(handler, newDefaultLambdaJsonSerializer()).Build().RunAsync();
# install the VS project template
dotnet new install Amazon.Lambda.Templates
# install the command line tools
dotnet tool install -g Amazon.Lambda.Tools
On VS, if the extension AWS Toolkit is installed you have the AWS .NET Mock Lambda Test Tool available.
A debug configuration is created for you lambda.
Properties\launchSettings.json
{"profiles":{"Mock Lambda Test Tool":{"commandName":"Executable","commandLineArgs":"--port 5050","executablePath":"<home-directory>\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe","workingDirectory":".\\bin\\Debug\\net6.0"}}}