AWS Lambda is a compute service that runs your code without provisioning or managing servers. Amazon API Gateway is a fully managed service that makes it easy for developers to publish, maintain, monitor, and secure APIs at any scale.
This session will familiarize you with the basics of AWS Lambda and Amazon API Gateway and demonstrate how to build web, mobile, and IoT backends using these services. You will learn how to setup API endpoints that trigger AWS Lambda functions to handle mobile, web, IoT, and 3rd party API requests. You will also learn how to use Lambda to read and write to Amazon DynamoDB. We will run through a demo of setting up a simple serverless blogging web application that allows user authentication and the ability to create posts and comments.
3. 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
4. Everybody knows this 3-tier web app diagram
Presentation Tier Logic Tier Data Tier
Mobile App Web Server Database
5. Here is what that translates to in real life
http://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf
7. Managed
The server-less stack – zoom in
InternetMobile apps
AWS Lambda
functions
AWS
API Gateway
cache
Endpoints on
Amazon EC2
Any other publicly
accessible endpoint
Amazon
CloudWatch
Amazon
CloudFront
API
Gateway
API Gateway
Other AWS
services
AWS Lambda
functions
8. Abstraction in AWS Compute offerings
VM App Function
Service EC2 ECS Lambda
H/W OS Runtime
Unit of scale
Level of
abstraction
9. 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
10. 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
12. 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
13. 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
19. 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
20. 3 new features in API Gateway
• Catch-all resource paths
• ANY http method
• PROXY integrations
Root/
/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
23. 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
24. 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.
25. 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
26. 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
27. 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)
28. 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)
29. 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)