building serverless backends with aws lambda and amazon api gateway
TRANSCRIPT
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Stefano Buliani, Specialist solutions architect, Serverless
October 24, 2016
Building Serverless BackendsUsing AWS Lambda and Amazon API Gateway
Agenda
• Why serverless backends
• Introduction to AWS Lambda
• Introduction to Amazon API Gateway
• Demo: Hello world API
• Demo: Running Express apps in AWS Lambda
• Best practices
Everybody knows this 3-tier web app diagram
Presentation Tier Logic Tier Data Tier
Mobile App Web Server Database
Here is what that translates to in real life
http://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf
The server-less stack
InternetMobile appsAWS Lambda
functions
AWS
API GatewayOther AWS
services
Managed
The server-less stack – zoom in
InternetMobile appsAWS Lambda
functions
AWS
API Gateway
cache
Endpoints on
Amazon EC2
Any other publicly
accessible endpoint
Amazon
CloudWatch
Amazon
CloudFrontAPI
Gateway
API GatewayOther AWS
services
AWS Lambda
functions
Abstraction in AWS Compute offerings
VM App Function
Service EC2 ECS Lambda
H/W OS Runtime
Unit of scale
Level of
abstraction
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
Applications Components for Serverless apps
EVENT SOURCE FUNCTION SERVICES (ANYTHING)
Changes in
data state
Requests to
endpoints
Changes in
resource state
Node
Python
Java
… more coming soon
Amazon
S3
Amazon
DynamoDBAmazon
Kinesis
AWS
CloudFormation
AWS
CloudTrail
Amazon
CloudWatch
Amazon
SNS
Amazon
SES
Amazon
API GatewayAmazon
Cognito
AWS
IoT
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
Benefits of 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
API Gateway integrations
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
Any other
AWS service
Customer use case:
API-first migration to the cloud
1. Use API Gateway to front existing backends
InternetClientAPI Gateway
Hosted service 1
Hosted service 2
AWS cloud
corporate data center
2. Lift & shift service to AWS
InternetClientAPI Gateway
Hosted service 1
Service 2
on EC2
AWS cloud
corporate data center
3. Lift & re-architect
InternetClientAPI Gateway
Service 2
on EC2
AWS cloud
Microservice 1
Microservice 2
Microservice 3
Re-architected Service 1
Demo: Hello World API
Using API Gateway Input/Output Transforms
Filter output results
• Remove private/unnecessary data
• Filter dataset size to improve API
performance
Translate between client-backend
• Convert GET query string
parameters to body for POST
• Talk XML to API user-interface but
JSON to Lambda
3 new features in API Gateway
• Catch-all resource paths
• ANY http method
• PROXY integrations
Ro
ot
/ /store/{path+}
* (ANY)
OPTIONS
/catalogue/{path+} * (ANY)
Store Lambda
Function
Mock integration
for CORS
support
HTTP service on
EC2
Automatic request mapping{
resource: “/store/{path+}”,
path: “/store/items/1423”,
httpVerb: “GET”,
headers: {
“Content-Type”:
“application/json”,
“x-custom-header” : “1”
},
queryStringParameters: {
},
pathParameters: {
petId: “1”
},
requestContext: {
},
stageVariables: {
}
body : “”
}
Default response structure{
statusCode: int,
body: string,
headers: {
"Content-Type":
"application/json",
"x-Custom-Header" : “1"
}
};
Full request/response passthrough
Demo: Running an Node.js
Express app in AWS Lambda
Best practices
Attaching Lambda functions to RESTful HTTP Endpoints
• 1:1 Mapping: Every API call triggers a stateless Lambda function
• Add caching to API calls to return a cached response instead for duplicate requests
• API Gateway concepts: An API is defined as a set of resources and methods
• Resource: A logical entity that can be accessed within an API
• Method: The combination of a resource path and an HTTP verb such as GET/POST
• Automatic Scaling: Both API Gateway and Lambda scale automatically with calls
• Safety throttle of 100 concurrent Lambda functions, can be increased by AWS Support Center
• User defined standard-rate limit and a burst-rate limit per second for each API method
Best practices for creating Lambda functions
• Memory: CPU proportional to the memory configured
• Increasing memory makes your code execute faster (if CPU bound)
• Timeout: Increasing timeout allows for longer functions, but more wait in case of errors
• Retries: For API Gateway, Lambda doesn’t retry the function execution, but the
Gateway generated SDKs retry throttled requests
• Permission model: API Gateway synchronously triggers Lambda, so assign API
Gateway a resource policy to invoke Lambda.
What are stage variables
• Stage variables act like environment variables
• Use stage variables to store configuration values
• Stage variables are available in the $context object
• Values are accessible from most fields in API Gateway
• Lambda function ARN
• HTTP endpoint
• Custom authorizer function name
• Parameter mappings
Stage variables and Lambda alias for stages
Using Stage Variables in API Gateway together with
Lambda function Aliases helps you manage a single API
configuration and Lambda function for multiple stages
myLambdaFunction
1
2
3 = prod
4
5
6 = beta
7
8 = dev
My First API
Stage variable = lambdaAlias
Prod
lambdaAlias = prod
Beta
lambdaAlias = beta
Dev
lambdaAlias = dev
Manage Multiple Versions and Stages of your APIs
Works like a source repository – clone your API to create a new
version
API 1
(v1)Stage (dev)
Stage (prod)
API 2
(v2)Stage (dev)
Custom Domain Names
Use custom domain names to put 2 APIs under the same domain
• Custom domain names can point to an API or a Stage
• A custom domain name can include a base path
• Use v1 as your base path in the custom domain name
• Pointing to an API you have access to all Stages
• Beta (e.g. yourapi.com/v1/beta)
• Prod (e.g. yourapi.com/v1/prod)
• Pointing directly to your “prod” Stage
• Prod (e.g. yourapi.com/v1)
Three Next Steps
1. Check out http://squirrelbin.com/
2. Try out the “Serverless Web-App Reference Architecture” at https://github.com/awslabs/lambda-refarch-webapp/
3. Check out some serverless frameworks:
1. https://github.com/awslabs/chalice (Python)
2. https://github.com/awslabs/aws-serverless-express (Express)
3. https://serverless.com/ (Serverless framework)
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
All attendees will receive a special giveaway gift!
Please join us for the
AWS DevDay Networking Reception
5:00 - 6:30 PM
JW Grand Foyer
Thank You!
@sapessi