building serverless backends with aws lambda and amazon api gateway

31
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Stefano Buliani, Specialist solutions architect, Serverless October 24, 2016 Building Serverless Backends Using AWS Lambda and Amazon API Gateway

Upload: amazon-web-services

Post on 06-Jan-2017

309 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Building Serverless Backends with AWS Lambda and Amazon API Gateway

© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Stefano Buliani, Specialist solutions architect, Serverless

October 24, 2016

Building Serverless BackendsUsing AWS Lambda and Amazon API Gateway

Page 2: Building Serverless Backends with AWS Lambda and Amazon API Gateway
Page 3: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 4: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Everybody knows this 3-tier web app diagram

Presentation Tier Logic Tier Data Tier

Mobile App Web Server Database

Page 5: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Here is what that translates to in real life

http://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf

Page 6: Building Serverless Backends with AWS Lambda and Amazon API Gateway

The server-less stack

InternetMobile appsAWS Lambda

functions

AWS

API GatewayOther AWS

services

Page 7: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Managed

The server-less stack – zoom in

InternetMobile appsAWS Lambda

functions

AWS

API Gateway

cache

Endpoints on

Amazon EC2

Any other publicly

accessible endpoint

Amazon

CloudWatch

Amazon

CloudFrontAPI

Gateway

API GatewayOther AWS

services

AWS Lambda

functions

Page 8: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Abstraction in AWS Compute offerings

VM App Function

Service EC2 ECS Lambda

H/W OS Runtime

Unit of scale

Level of

abstraction

Page 9: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 10: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 11: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Amazon

S3

Amazon

DynamoDBAmazon

Kinesis

AWS

CloudFormation

AWS

CloudTrail

Amazon

CloudWatch

Amazon

SNS

Amazon

SES

Amazon

API GatewayAmazon

Cognito

AWS

IoT

Amazon

Alexa

Cron events

DATA STORES ENDPOINTS

REPOSITORIES EVENT/MESSAGE SERVICES

Event Sources that integrate with AWS Lambda

… and the list will continue to grow!

Amazon RDS

Aurora

Page 12: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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 13: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 14: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Customer use case:

API-first migration to the cloud

Page 15: Building Serverless Backends with AWS Lambda and Amazon API Gateway

1. Use API Gateway to front existing backends

InternetClientAPI Gateway

Hosted service 1

Hosted service 2

AWS cloud

corporate data center

Page 16: Building Serverless Backends with AWS Lambda and Amazon API Gateway

2. Lift & shift service to AWS

InternetClientAPI Gateway

Hosted service 1

Service 2

on EC2

AWS cloud

corporate data center

Page 17: Building Serverless Backends with AWS Lambda and Amazon API Gateway

3. Lift & re-architect

InternetClientAPI Gateway

Service 2

on EC2

AWS cloud

Microservice 1

Microservice 2

Microservice 3

Re-architected Service 1

Page 18: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Demo: Hello World API

Page 19: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 20: Building Serverless Backends with AWS Lambda and Amazon API Gateway

3 new features in API Gateway

• Catch-all resource paths

• ANY http method

• PROXY integrations

Ro

ot

/ /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

Page 21: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Demo: Running an Node.js

Express app in AWS Lambda

Page 22: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Best practices

Page 23: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 24: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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.

Page 25: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 26: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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

Page 27: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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)

Page 28: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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)

Page 29: Building Serverless Backends with AWS Lambda and Amazon API Gateway

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)

Page 30: Building Serverless Backends with AWS Lambda and Amazon API Gateway

© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

All attendees will receive a special giveaway gift!

Please join us for the

AWS DevDay Networking Reception

5:00 - 6:30 PM

JW Grand Foyer

Page 31: Building Serverless Backends with AWS Lambda and Amazon API Gateway

Thank You!

@sapessi