More Related Content Similar to Getting Started with AWS Lambda and Serverless Computing (20) More from Amazon Web Services (20) Getting Started with AWS Lambda and Serverless Computing1. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Getting Started with AWS Lambda and Serverless Computing
George Mao,
Serverless Specialist
2. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Agenda
• What does it mean to be Serverless?
• What is AWS Lambda?
• What is Amazon API Gateway
• Use cases
• Best practices
3. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
What does it mean to be
Serverless?
4. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Servers
How will the application
handle server hardware failure?
How can I control
access from my servers?
When should I decide to
scale out my servers?
When should I decide to
scale up my servers?
What size servers are
right for my budget?
How much remaining
capacity do my servers have?
(AAHHHHHHHHH!!)
5. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Operations and management Scaling
Provisioning and utilization Availability and fault tolerance
Owning servers means dealing with ...
6. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Everybody knows this 3-tier app diagram
Presentation Tier Logic Tier Data Tier
Mobile App App Server Database
7. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Here is what that translates to in real life
http://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf
8. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
The Serverless stack
InternetMobile apps
AWS Lambda
functions
AWS
API Gateway
Other AWS
services
9. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda
Amazon DynamoDB
Amazon SNS
Amazon API Gateway
Amazon SQS
Amazon KinesisAmazon S3
Compute and API Proxy
AWS X-Ray
AWS Step Functions
Services for Building Serverless Applications
Database, Storage, Orchestration, Analytics, Interprocess Messaging
Developer Tools
AWS CodeBuild
AWS CodePipelineAWS SAM
Third-Party Tools
Open Source
10. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
What is AWS Lambda?
Compute service that lets you run code without managing servers:
Scale, monitor, and, trigger on your behalf
11. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Introduction to AWS Lambda: Run code in response to events
FUNCTION SERVICES (ANYTHING)
Changes in
data state
Requests to
endpoints
Changes in
resource state
Node
Python
Java
C#
EVENT SOURCE
12. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Cost-effective and
efficient
No Infrastructure
to manage
Pay only for what you use
Bring Your
Own Code
Productivity focused compute platform to build powerful, dynamic, modular
applications in the cloud
Run code in standard
languages
Focus on business logic
Benefits of AWS Lambda
1 2 3
13. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
How it works
14. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Using AWS Lambda
Bring your own code
• Node.js, Java, Python, C#
• Bring your own libraries
(even native ones)
Simple resource model
• Select power rating from
128 MB to 1.5 GB
• CPU and network
allocated proportionately
Flexible use
• Synchronous or
asynchronous
• Integrated with other
AWS services
Flexible authorization
• Securely grant access to
resources and VPCs
• Fine-grained control for
invoking your functions
15. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Using AWS Lambda
Authoring functions
• WYSIWYG editor or
upload packaged .zip
• Third-party plugins
(Eclipse, Visual Studio,
SAM)
Monitoring and logging
• Metrics for requests,
errors, and throttles
• Built-in logs to Amazon
CloudWatch Logs
Programming model
• Use processes, threads,
/tmp, sockets normally
• AWS SDK built in
(Python and Node.js)
Stateless
• Persist data using
external storage
• No affinity or access to
underlying infrastructure
16. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
3 Easy Steps!
1. Write your code
1. Package/Upload to AWS Lambda
2. Define your Compute Requirements
17. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Amazon
S3
Amazon
DynamoDB
Amazon
Kinesis
AWS
CloudFormation
AWS
CloudTrail
Amazon
CloudWatch
Amazon
SNS
Amazon
SES
Amazon
API Gateway
Amazon
Cognito
Amazon
Alexa
Cron events
DATA STORES ENDPOINTS
REPOSITORIES EVENT/MESSAGE SERVICES
Event Sources that integrate with AWS Lambda
… and the list will continue to grow!
Amazon RDS
Aurora
New
AWS
CodeCommit
AWS Step
Functions
AWS IoT
18. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda Pricing
• Buy compute time in
100 ms increments
• Low request charge
• No hourly, daily, or
monthly minimums
• No per-device fees
Never pay for idle!
Free Tier
1 million requests and 400,000 GB-secs of
compute every month, every customer
19. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
What is API Gateway?
Fully managed service that makes it easy for developers to:
create, publish, maintain, monitor, and secure
APIs at any scale.
20. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Introduction to Amazon API Gateway
Create a unified
API frontend for
multiple micro-
services
Authenticate and
authorize
requests to a
backend
DDoS protection
and throttling for
your backend
Throttle, meter,
and monetize API
usage by 3rd
party developers
21. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Amazon API Gateway: Serverless APIs
Internet
Mobile Apps
Websites
Services
AWS Lambda
functions
AWS
API Gateway
Cache
Endpoints on
Amazon EC2
All publicly
accessible
endpoints
Amazon
CloudWatch
Monitoring
Amazon
CloudFront
Amazon
API Gateway
Any other
AWS service
22. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lets dive a little deeper
api-name/
/store/products
GET
OPTIONS
/store/{cart} POST
Store Lambda
Function
Mock
integration for
CORS support
AWS
Service
Proxy
https://my-api-id.execute-api.region-id.amazonaws.com/stage/api-name
23. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
What are Step Functions?
Makes it simple to:
Coordinate and run multistep applications
24. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
“I want to sequence functions”
“I want to select functions based on data”
I want to run functions in parallel
“I want to retry functions”
“I want to try/catch/finally”
“I want to run code for hours”
Orchestration for serverless apps
AWS Step Functions
25. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Benefits of Step Functions orchestration
Change and add steps
without writing code to
evolve applications and
innovate faster
Coordinate and visualize
Lambda functions as a series
of steps to quickly create
serverless apps
Automatically trigger and track
each step at scale
and handle errors with
built-in retry and fallback
Productivity AgilityResilience
26. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Step Functions
Easily coordinate multiple Lambda functions using visual workflows
Visualize in the
Console
Define in JSON Monitor
Executions
27. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Visual Workflows and Orchestration
28. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Visual Workflows and Orchestration
Sequential Steps Branching Steps Parallel Steps
29. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
JSON Based Templates
• Defined in JSON format
– Pass
– Task
– Choice
– Parallel
{
"Comment": "A Hello World example",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Pass",
"Result": "Hello World!",
"End": true
}
}
}
30. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Serverless Use cases
31. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Common use cases
Web
Applications
• Static websites
• Complex web
apps
• Packages for
Flask and
Express
Data
Processing
• Real time
• MapReduce
• Batch
Chatbots
• Powering
chatbot logic
Backends
• Apps & services
• Mobile
• IoT
</></>
Amazon
Alexa
• Powering voice-
enabled apps
• Alexa Skills Kit
IT
Automation
• Policy engines
• Extending AWS
services
• Infrastructure
management
32. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Web Applications and Backends
App requests
over HTTPS
CRUD
operations
33. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Backend Order Processing
CloudWatch event
every 5 minutes
Lambda runs
for 5 minutesSQS queue
34. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
What to expect from the session
15-20 minutes of processing now in seconds
2x order of magnitude for cost savings
https://www.youtube.com/watch?v=TXmkj2a0fRE
Nordstrom Recommendations
35. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda use cases – Image Thumbnail Creation
36. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda use cases – Data processing
37. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Best practices
38. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda best practices
Limit your function/code size
Node – remember execution is asynchronous – event loop!
500 MB /tmp directory provided to each function
Don’t assume function will reuse underlying infrastructure
But take advantage of it when it does occur
You own the logs
Include details from service-provided context
Create custom metrics
Operations-centric vs. business-centric
Use environment variables
Parameterize code and change parameters independent of code updates
Use for securing credentials and keeping them out of code
39. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Additional best practices
Externalize authorization to IAM roles whenever possible
Least privilege and separate IAM roles
Externalize configuration
DynamoDB is great for this
Take advantage of Dead Letter Queues
Use to handle failed invocations
Make sure your downstream setup “keeps up” with Lambda scaling
Limit concurrency when talking to relational databases
Be aware of service throttling
Engage AWS Support to increase your limits
Contact AWS Support before known large scaling events
40. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Best practice: Use versions and aliases
• Versions = immutable copies of code + properties
• Aliases = mutable pointers to versions
Rollbacks
Staged
promotions
“Lock” behavior
for client
41. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS CloudFormation brings:
• Infrastructure as code
• Easy to provision and manage a collection of related AWS resources
• Input .yaml file and output provisioned AWS resources
• Optimized for infrastructure
AWS SAM:
• CloudFormation extension optimized for serverless
• New serverless resources: functions, APIs, and tables
• Supports anything CloudFormation supports
• Open specification (Apache 2.0)
AWS Serverless Application Model (AWS SAM)
42. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWSTemplateFormatVersion: '2010 -09-09'
Resources:
GetHtmlFunctionGetHtmlPermissionProd:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
Principal: apigateway.amazonaws.com
FunctionName:
Ref: GetHtmlFunction
SourceArn:
Fn::Sub: arn:aws:execute -api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/*
ServerlessRestApiProdStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId:
Ref: ServerlessRestApiDeployment
RestApiId:
Ref: ServerlessRestApi
StageName: Prod
ListTable:
Type: AWS::DynamoDB::Table
Properties:
ProvisionedThroughput:
WriteCapacityUnits: 5
ReadCapacityUnits: 5
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- KeyType: HASH
AttributeName: id
GetHtmlFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.gethtml
Code:
S3Bucket: flourish -demo-bucket
S3Key: todo_list.zip
Role:
Fn::GetAtt:
- GetHtmlFunctionRole
- Arn
Runtime: nodejs4.3
GetHtmlFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess
- arn:aws:iam::aws:policy/service -role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: '2012 -10-17'
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
ServerlessRestApiDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId:
Ref: ServerlessRestApi
Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d'
StageName: Stage
GetHtmlFunctionGetHtmlPermissionTest:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
Principal: apigateway.amazonaws.com
FunctionName:
Ref: GetHtmlFunction
SourceArn:
Fn::Sub: arn:aws:execute -api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/*
ServerlessRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Body:
info:
version: '1.0'
title:
Ref: AWS::StackName
paths:
"/{proxy+}":
x-amazon-apigateway-any-method:
x-amazon-apigateway-integration:
httpMethod: ANY
type: aws_proxy
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015 -03-
31/functions/${GetHtmlFunction.Arn}/invocations
responses: {}
swagger: '2.0'
CF template example – API triggering Lambda
AWSTemplateFormatVersion: '2010-09-09’
Transform: AWS::Serverless-2016-10-31
Resources:
GetHtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://flourish-demo-bucket/todo_list.zip
Handler: index.gethtml
Runtime: nodejs4.3
Policies: AmazonDynamoDBReadOnlyAccess
Events:
GetHtml:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
ListTable:
Type: AWS::Serverless::SimpleTable
AWS SAM example – API triggering Lambda
AWS SAM: Less complexity, more power
43. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Q & A
44. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved
aws.amazon.com/activate
Everything and Anything Startups
Need to Get Started on AWS