migrate your existing express apps to aws lambda and amazon api gateway
TRANSCRIPT
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Stefano Buliani, Specialist Solutions Architect, Serverless@sapessi
11/02/2016
Migrate Express Apps to ServerlessUsing AWS Lambda and Amazon API Gateway
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
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
Code is all you need Event driven scaling
Never pay for idle Availability and fault tolerance built in
Benefits of AWS Lambda
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 2on EC2
AWS cloud
corporate data center
3. Lift & re-architect
InternetClientAPI Gateway
Service 2on EC2
AWS cloud
Microservice 1
Microservice 2
Microservice 3
Re-architected Service 1
Simplified migration:New features in API Gateway
• Catch-all resource paths
• ANY http method
• PROXY integrations
Three new features in Amazon API Gateway
Roo
t /
/orders
GET
POST
/{id}GET
DELETE
/catalogue/GET
/{id} GET
Catch-all resource pathsDefine the entire API structure• Use path variables for individual
path parts: {id}• Use the Swagger toolkit to
generate documentation and SDKs
Roo
t /
/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
Catch-all resource paths – using Swagger
{ "Swagger": "2.0", … "paths": { "/{proxy+}": { … } }}
Roo
t /
/orders
GET
POST
/{id}GET
DELETE
/catalogue/GET
/{id} GET
ANY HTTP methodDefine methods individually• Flexible because each method
can use a different integration• Use the Swagger toolkit to
generate documentation and SDKs
Roo
t / /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
ANY HTTP method – using Swagger{ "Swagger": "2.0", … "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { … } } }}
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
Proxy integrationsR
oot / /orders/{path+} ANY
/catalogue/{path+} ANY
Store Lambda Function
HTTP service on EC2
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: { “apiId”: “xxxxxx”, “apiKey”: “xxxxxxxxxxxxxxxxxxxx”, “identity”: { “sourceIp”: “xxx.xxx.xxx.xxx”, “cognitoIdentityId”: “xxxxxxxxxxx” } }, stageVariables: { } body : “”}
Default response structure{ statusCode: int, body: string, headers: { "Content-Type": "application/json", "x-Custom-Header" : “1" }};
Full request/response passthrough
Proxy integrations – using Swagger{ "Swagger": "2.0", … "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { "x-amazon-apigateway-integration": { "type": ”aws_proxy" } } } }}
Roo
t /
/{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
Using the API Gateway console
1. Select proxy resource when creating a new resource
Using the API Gateway console
1. The ANY method is created automatically2. Configure the integration
Running Express applications:The aws-serverless-express library
https://github.com/awslabs/aws-serverless-express
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" }};
How it works
Mobile appsStart Lambda
functionAPI GatewayTransform
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
Migrating an Express app
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
The JavaScript wrapper
'use strict’const awsServerlessExpress = require('aws-serverless-express')const app = require('./app')const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context)}
DEMO
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
We demoed Express, you can do the same with any framework / supported language
• Spring boot• Jersey• Flask• …
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
Best practices
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
Thank you!
Stefano Buliani@sapessi
AWS serverless Express libraryhttp://bit.ly/serverless-express
Proxy resources in API Gatewayhttp://bit.ly/proxy-resources-doc