This webinar teaches you how to use Amazon API Gateway and AWS Lambda to run your existing Express.js applications with just a few lines of code. We will introduce three new features in API Gateway: proxy integrations, greedy paths, and the ANY HTTP method. Combining these features, you can configure API Gateway in a few simple clicks via the management console and express all of your logic and API definition in code.
Learning Objectives:
1. Easier migration to API Gateway and Lambda
2. New API Gateway Catch-all methods
Who Should Attend: Developers
2. Agenda
• Benefits of Amazon API Gateway
• Benefits of AWS Lambda
• API-first migration to the cloud
• New features in API Gateway
• aws-serverless-express in Github
• Demo
• Best practices
3. 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
4. Code is all you need Event driven scaling
Never pay for idle Availability and fault tolerance built in
Benefits of AWS Lambda
10. • Catch-all resource paths
• ANY http method
• PROXY integrations
Three new features in Amazon API Gateway
11. Root/
/orders
GET
POST
/{id}
GET
DELETE
/catalogue/
GET
/{id} GET
Catch-all resource paths
Define the entire API structure
• Use path variables for individual
path parts: {id}
• Use the Swagger toolkit to
generate documentation and
SDKs
Root/
/orders /{proxy+}
POST
GET
DELETE
/catalogue/ /{proxy+} GET
Quickly catch all sub-resources
• Use catch-all paths to identify
separate services
• Very quick to configure but less
well defined for doc and SDK
generation
13. Root/
/orders
GET
POST
/{id}
GET
DELETE
/catalogue/
GET
/{id} GET
ANY HTTP method
Define methods individually
• Flexible because each method
can use a different integration
• Use the Swagger toolkit to
generate documentation and
SDKs
Root/
/orders /{proxy+} ANY
/catalogue/ /{proxy+} ANY
Intercepts any HTTP verb
• Very quick to configure but less
well defined for doc and SDK
generation
• Works well with catch-all
resource paths
15. Proxy integrations
HTTP_PROXY
• No input/output mappings!
• Sends entire request to the HTTP
backend and returns the
unmodified response
AWS_PROXY
• No input/output mappings!
• Automatically generate event with
the entire request
• Well defined return interface that
is automatically transformed into
an HTTP response
18. Root/
/{proxy+} ANY Your Node.js
Express app
The simplest possible API
Simple yet very powerful:
• Automatically scale to meet demand
• Only pay for the requests your receive
19. Using the API Gateway console
1. Select proxy resource when creating a new resource
20. Using the API Gateway console
1. The ANY method is created automatically
2. Configure the integration
22. How it works
• AWS_PROXY integrations apply
a default mapping to requests
• And expect a well defined return
interface
Automatic request mapping
{
resource: “/orders/{path+}”,
path: “/orders/1423”,
httpVerb: “GET”,
headers: {
“Content-Type”: “application/json”,
“x-custom-header” : “1”
},
queryStringParameters: {
“limit”: 10
},
pathParameters: {
“path”: “1423”
},
requestContext: {
},
stageVariables: {
}
body : “”
}
Default response structure
{
statusCode: int,
body: string,
headers: {
"Content-Type": "application/json",
"x-Custom-Header" : “1"
}
};
23. How it works
Mobile apps
Start Lambda
functionAPI Gateway
Transform
request into
event JSON
aws-serverless-express
receives the event
Express
If not started – start
express server on local
socket
Transform event JSON
into HTTP request and
send it to local socket
Receive response from
local socket and
transform it into return
value
Your Express
code executes
Transform return
value into HTTP
response
25. Five simple steps
1. Pull dependencies from npm (include aws-serverless-
express)
2. Create JavaScript wrapper
3. Zip folder contents including node-modules
4. Create Lambda function
5. Create API Gateway endpoint as proxy resource
28. Express on Serverless
• Automatically scales to
handle demand
• Only pay for requests
• Unit test your Express app
locally just like you do now
• Use your code and middlewares,
no changes required
29. We demoed Express, you can do the
same with any framework /
supported language
• Spring boot
• Jersey
• Flask
• …
30. If you are starting from scratch, take a
look at some serverless-native
frameworks
• Serverless 1.0 - https://serverless.com/
• Claudia.js - http://bit.ly/claudiajs
• Chalice - http://bit.ly/aws-chalice
• Zappa - http://bit.ly/py-zappa
32. Using AWS Lambda
• 1:1 Mapping: Every API call triggers a stateless Lambda function - Each express
instance will only handle one requests at a time. Don’t worry too much about
concurrency
• Memory: CPU proportional to the memory configured - Increasing memory makes
your code execute and start faster (if CPU bound)
• Lazily load resources: The global scope helps you cache values, load them only
when you need them to avoid impacting startup time
• Offload tasks to the Gateway: Authorization and metering can be offloaded to
Amazon API Gateway. You can also use your middlewares in Express
33. Thank you!
Stefano Buliani
@sapessi
AWS serverless Express library
http://bit.ly/serverless-express
Proxy resources in API Gateway
http://bit.ly/proxy-resources-doc