Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway

3,001 views

Published on

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

Published in: Technology

Migrate your Existing Express Apps to AWS Lambda and Amazon API Gateway

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

×