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.
8. 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
9. 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
10. What is a Lambda function?
Code
Dependencies
(e.g. libraries, modules, binaries)
ConïŹguration
29. 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
31. 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
32. Ways to invoke λ functions
RequestResponse (synchronous)
Event (asynchronous)
33. RequestResponse Invocation
When a λ function is:
Invoked via the AWS console
Invoked using the AWS CLI
Executed via the Amazon API Gateway
34. Event Invocations
When an AWS event triggers a λ function:
An object is created in S3
An Amazon SNS notiïŹcation
A Kinesis or DynamoDB stream
35. 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
37. The following programming model
examples are for Node.jsâŠ
The Python 2.7 and Java8 models are
similar
38. 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
40. 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)
41. Logging
Logs are stored in AWS CloudWatch
Available via AWS console or
CloudWatch API
console.info("Event: ", JSON.stringify(event, null, 2));
42. 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
52. 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)
53. 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
58. 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 speciïŹc versions
60. 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
61. 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 ïŹles written to /tmp may still
exist from previous invocations!
https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/
63. 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
64. Resource Limits
Resources are not inïŹnite - λ 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
65. 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.)
66. 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/