![Page 1: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/1.jpg)
© 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
![Page 2: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/2.jpg)
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
![Page 3: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/3.jpg)
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
![Page 4: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/4.jpg)
Code is all you need Event driven scaling
Never pay for idle Availability and fault tolerance built in
Benefits of AWS Lambda
![Page 5: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/5.jpg)
Customer use case:API-first migration to the cloud
![Page 6: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/6.jpg)
1. Use API Gateway to front existing backends
InternetClientAPI Gateway
Hosted service 1
Hosted service 2
AWS cloud
corporate data center
![Page 7: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/7.jpg)
2. Lift & shift service to AWS
InternetClientAPI Gateway
Hosted service 1
Service 2on EC2
AWS cloud
corporate data center
![Page 8: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/8.jpg)
3. Lift & re-architect
InternetClientAPI Gateway
Service 2on EC2
AWS cloud
Microservice 1
Microservice 2
Microservice 3
Re-architected Service 1
![Page 9: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/9.jpg)
Simplified migration:New features in API Gateway
![Page 10: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/10.jpg)
• Catch-all resource paths
• ANY http method
• PROXY integrations
Three new features in Amazon API Gateway
![Page 11: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/11.jpg)
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
![Page 12: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/12.jpg)
Catch-all resource paths – using Swagger
{ "Swagger": "2.0", … "paths": { "/{proxy+}": { … } }}
![Page 13: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/13.jpg)
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
![Page 14: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/14.jpg)
ANY HTTP method – using Swagger{ "Swagger": "2.0", … "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { … } } }}
![Page 15: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/15.jpg)
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
![Page 16: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/16.jpg)
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
![Page 17: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/17.jpg)
Proxy integrations – using Swagger{ "Swagger": "2.0", … "paths": { "/{proxy+}": { "x-amazon-apigateway-any-method": { "x-amazon-apigateway-integration": { "type": ”aws_proxy" } } } }}
![Page 18: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/18.jpg)
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
![Page 19: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/19.jpg)
Using the API Gateway console
1. Select proxy resource when creating a new resource
![Page 20: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/20.jpg)
Using the API Gateway console
1. The ANY method is created automatically2. Configure the integration
![Page 21: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/21.jpg)
Running Express applications:The aws-serverless-express library
https://github.com/awslabs/aws-serverless-express
![Page 22: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/22.jpg)
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" }};
![Page 23: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/23.jpg)
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
![Page 24: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/24.jpg)
Migrating an Express app
![Page 25: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/25.jpg)
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
![Page 26: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/26.jpg)
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)}
![Page 27: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/27.jpg)
DEMO
![Page 28: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/28.jpg)
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
![Page 29: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/29.jpg)
We demoed Express, you can do the same with any framework / supported language
• Spring boot• Jersey• Flask• …
![Page 30: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/30.jpg)
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
![Page 31: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/31.jpg)
Best practices
![Page 32: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/32.jpg)
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
![Page 33: Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway](https://reader036.vdocuments.site/reader036/viewer/2022081422/586fb3ca1a28abe57d8b6e2b/html5/thumbnails/33.jpg)
Thank you!
Stefano Buliani@sapessi
AWS serverless Express libraryhttp://bit.ly/serverless-express
Proxy resources in API Gatewayhttp://bit.ly/proxy-resources-doc