以aws lambda與amazon api gateway打造無伺服器後端

33
AWS Cloud Kata for Start-Ups and Developers Taiwan Olivier Klein 䅎䰐㳓, Senior Solutions Architect, AWS Building Serverless Backends Using AWS Lambda and Amazon API Gateway

Upload: amazon-web-services

Post on 06-Jan-2017

481 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Taiwan

Olivier Klein , Senior Solutions Architect, AWS

Building Serverless Backends Using AWS Lambda and Amazon API Gateway

Page 2: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

How to build application backends?

Back-end logic Database Mobile

Page 3: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

The familiar 3-Tier Architecture

Page 4: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Dr. Werner Vogels CTO - Amazon

Page 5: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

No server is easier to manage…

Page 6: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

No server is easier to manage…

…than no server.

Page 7: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

No server is easier to manage…

…than no server.

AWS Lambda

Page 8: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Utilization Window

Weeks Minutes Seconds Milliseconds

On-Premise Amazon EC2 Containers AWS Lambda

Page 9: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

AWS Lambda: Run code in response to events

Lambda functions: Stateless, trigger-based code execution

Triggered by events: •  Direct and Asynchronous API calls •  AWS Service integrations •  3rd party triggers

Makes it easy to: •  Perform event-driven actions •  Build back-end services that perform at scale

Page 10: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Amazon S3 Amazon DynamoDB

Amazon Kinesis

AWS CloudFormation

AWS CloudTrail Amazon CloudWatch

Amazon SNS AmazonSES

AmazonAPI Gateway

Amazon Cognito

AWSIoT

AmazonAlexa

Cron events

DATA STORES ENDPOINTS

REPOSITORIES EVENT/MESSAGE SERVICES

Event Sources that integrate with AWS Lambda

… and the list continues to grow.

Page 11: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Multiple Application Types using AWS Lambda

  Analytics •  Operational management •  Live Dashboards

  Data workflows •  Content management •  ETL workflows

  Interactive Backends •  Bots •  Webhooks

  Autonomous IT •  Policy engines •  Infrastructure management

Page 12: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

The Other Challenge..

Page 13: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Managing APIs is Hard!

Managing multiple versions and stages of an API is difficult

Monitoring 3rd party developers’ access is time consuming

Access authorization is a challenge

Traffic spikes create operational burden

Page 14: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

API Gateway to manage your REST endpoints

  Build, Deploy and Manage APIs

  Throttling rules per HTTP method

  Cache with customizable keys

  Multiple API version and stages

  Generates client SDKs

  Dashboard for visual monitoring

  Flexible authorization model

  Usage plans for consumption

Page 15: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

API Gateway + Lambda = Frontend + Backend

Endpoints on Amazon EC2

Any other publicly accessible endpoint

AWS Lambda functions

API Gateway Cache

Internet

Mobile Apps

Websites

Services

Amazon CloudFront Amazon

API Gateway

Amazon CloudWatch Monitoring

Page 16: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

DDoS and Network Protection

Internet

Mobile Apps

Websites

Services

AWS Lambda functions

Endpoints on Amazon EC2

Amazon CloudFront Amazon

API Gateway

Layer 7 and layer 3 DDoS protection

Request throttling for backend services

Page 17: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

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 - API defined as a set of resources and methods •  Resource: A logical entity that can be accessed within an API

•  Method: Resource path and an HTTP command such as GET/POST

  Automatic Scaling – API Gateway and Lambda scale automatically •  Safety throttle of 100 concurrent Lambda functions, can be increased by AWS Support

•  User defined standard-rate limit and a burst-rate limit per second for each API method

Page 18: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

AWS Lambda

Hello World API

Amazon API Gateway

Python (Chalice)

Postman

DEMO

Test

Build / Deploy

Page 19: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Web App

Page 20: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Real World Architecture

Page 21: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Real World “Serverless” Architecture

Page 22: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Storage and Delivery of the App

Amazon S3

Amazon CloudFront

  Amazon S3 •  Highly available object storage •  Designed for 99.999999999% durability •  Offers HTTP / HTTPS endpoint to objects

  Amazon CloudFront •  Content Delivery Network with 59 edge

locations across the world •  Caches content on edge locations for low

latency

Page 23: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Swagger

Page 24: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Swagger!

  Swagger •  Goal is to define a language-agnostic

interface to REST APIs •  Ecosystem of tools to explore, visualize,

document and test APIs

SwaggerHub •  Collaborative platform to define and manage

APIs •  Integration with Amazon API Gateway

www.swagger.io

Page 25: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Swagger – Build an API in minutes!

Amazon API Gateway

Swagger

DEMO

AWS Lambda Function

AWS Lambda Function

Page 26: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

NLTK & Lambda

  NLTK is a toolkit written in Python to facilitate Natural Language Processing (NLP)   Provides easy-to-use interfaces for classification, tokenization, stemming, tagging, parsing, and semantic reasoning   VADER - Rule-based Model for Sentiment Analysis of Social Media Text www.nltk.org

Page 27: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Amazon S3

Sentiment Analysis Web App

Amazon API Gateway

Amazon Route 53

HTML / JS / CSS

Swagger

www.aws.klein.asia api.aws.klein.asia

DEMO

Page 28: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Best Practices

Page 29: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Best Practices for Lambda functions

  Memory •  CPU proportional to memory •  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 if failed •  Gateway generated SDKs retry

throttled requests

  Permission Model •  API Gateway synchronously triggers

Lambda, so assign API Gateway a resource policy to invoke Lambda

Page 30: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Using API Gateway Input/Output Transforms

  Filter Output Results •  Remove private/unnecessary data •  Filter dataset size to improve API

performance

  Translate between client and backend

•  Convert GET query strong parameters to body for POST

•  Talk XML to API user-interface but JSON to Lambda

Page 31: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Managing different versions of your API stack

  Operate multiple API versions and multiple stages for each version simultaneously

  Clone an existing API to start working on the next major version

  Set up custom domain names to point directly to a stage

  Version Lambda functions, and associate API stage with Lambda function version alias

Page 32: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Final Thoughts

  Plug-in API Gateway early. It allows to split your API between legacy EC2 and new Lambda functions   Create many version and stages – no additional charges for APIs, allows for backwards compatibility   Document your API from Day 1   Test your API calls consistently and automatically   Always use serverless interfaces when possible – HTML5 and JS frameworks like React or Angular are useful

Page 33: 以AWS Lambda與Amazon API Gateway打造無伺服器後端

AWS Cloud Kata for Start-Ups and Developers

Thank you!

Olivier Klein Senior Solutions Architect

AWS