Slides for a short presentation I gave on AWS Lambda, which "lets you run code without provisioning or managing servers". Lambda is to running code as Amazon S3 is to storing objects.
Provide objects to S3
S3 stores those objects transparently
S3
You don't know how or where objects are stored
No servers, drives, or disk space to manage
Charged only by amount stored
Provide function code to Lambda
Lambda executes code on demand
Lambda
You don't know how or where code is executed
No servers or VMs to manage
Charged only for execution time
What is a Lambda function?
Code
Dependencies
(e.g. libraries, modules, binaries)
Configuration
What about costs?
First million requests per month are free
Plus duration of execution (GB-seconds) rounded
up to nearest 100 milliseconds ($0.00001667 per GB-sec)
Charged 20¢ per million requests…
(information as of November 30, 2016)
First 400,000 GB-seconds per month are free
How can a λ be executed?
Manually in AWS Lambda console
Using AWS SDK to call Lambda API
HTTP requests via API Gateway
Events raised in AWS
(e.g. an object created in S3, new data in Kinesis stream
Ways to invoke λ functions
RequestResponse (synchronous)
Event (asynchronous)
RequestResponse Invocation
When a λ function is:
Invoked via the AWS console
Invoked using the AWS CLI
Executed via the Amazon API Gateway
Event Invocations
When an AWS event triggers a λ function:
An object is created in S3
An Amazon SNS notification
A Kinesis or DynamoDB stream
Event Models: Push vs. Pull
Push - A service (e.g. S3) publishes an
event to Lambda and invokes the function
Pull - Lambda polls streaming event
source (Kinesis, DynamoDB) and invokes
the function when new data arrives
Handler
exports.handler = (event, context, callback) => {
// code...
}
event contains input data
context contains metadata about the
executing λ function
callback (Node.Js only) returns error
or successful result to caller
Context Object
contains metadata about a λ function
version
name
ARN
(Amazon Resource Name)
memory (MB)
AWS request ID
log group
log stream
Cognito identity
client context (mobile)
remaining time (ms)
Logging
Logs are stored in AWS CloudWatch
Available via AWS console or
CloudWatch API
console.info("Event: ", JSON.stringify(event, null, 2));
Exceptions
exports.myHandler = (event, context, callback) => {
// code to compute the sum...
if (sum > 42) {
var error = new Error(
"Sum cannot be more than the ultimate answer!");
callback(error);
} else {
callback(null, result);
}
}
CloudWatch
Create via CLI
$ aws lambda create-function
--function-name simple-deploy-test
--description "deployment test function"
--handler myfunction.myHandler
--memory-size 128
--runtime nodejs4.3
--role arn:aws:iam::269911013159:role/deploy-test-role
(repeatable, easily to automate)
Versioning λ functions
Publishing a function creates a new version
$LATEST is the current, editable version
Previous versions are immutable
Publish via AWS console, CLI, or Lambda SDK
Function Aliases
Use aliases to point to the proper version
Multiple aliases for separate deployment
environments (e.g. dev, staging, production)
Refer to aliases, not specific versions
When things go wrong…
Automatic retry for stream-based event
sources (e.g. Kinesis, Dynamo)
Automatic retry for asynchronous events
from non-stream-based sources (e.g. S3)
Manual retry for synchronous invocations
Container re-use
Functions run in an isolated container (sandbox)
Lambda may or may not re-use same container
across different invocations
Be aware that files written to /tmp may still
exist from previous invocations!
https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/
Here come the frameworks…
Chalice - Python micro-framework by AWS dev
tools team
Apex - build, deploy, manage λ functions
Serverless - build web, mobile, IoT apps
powered by Lambda & API Gateway
Resource Limits
Resources are not infinite - λ imposes limits
Examples:
300 seconds (5 minutes) max execution time
Maximum 50MB deployment package
100 concurrent executions
See developer guide for details…
http://docs.aws.amazon.com/lambda/latest/dg/limits.html
It's a wrap!
Lambda is to code as S3 is to storage
No servers, no VMs, no administration
Charged only when functions run
Synchronous or async
Many ways to invoke
(events,API gateway,AWS SDK calls,AWS CLI,AWS console, etc.)
References
AWS Lambda in Action
https://www.manning.com/books/aws-lambda-in-action
Serverless Architectures on AWS
https://www.manning.com/books/serverless-architectures-on-aws
AWS Lambda
https://aws.amazon.com/lambda/
Lambda Developer Guide
http://docs.aws.amazon.com/lambda/latest/dg/