以aws lambda與amazon api gateway打造無伺服器後端
TRANSCRIPT
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
AWS Cloud Kata for Start-Ups and Developers
How to build application backends?
Back-end logic Database Mobile
AWS Cloud Kata for Start-Ups and Developers
The familiar 3-Tier Architecture
AWS Cloud Kata for Start-Ups and Developers
Dr. Werner Vogels CTO - Amazon
AWS Cloud Kata for Start-Ups and Developers
No server is easier to manage…
AWS Cloud Kata for Start-Ups and Developers
No server is easier to manage…
…than no server.
AWS Cloud Kata for Start-Ups and Developers
No server is easier to manage…
…than no server.
AWS Lambda
AWS Cloud Kata for Start-Ups and Developers
Utilization Window
Weeks Minutes Seconds Milliseconds
On-Premise Amazon EC2 Containers AWS Lambda
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
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.
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
AWS Cloud Kata for Start-Ups and Developers
The Other Challenge..
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
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
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
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
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
AWS Cloud Kata for Start-Ups and Developers
AWS Lambda
Hello World API
Amazon API Gateway
Python (Chalice)
Postman
DEMO
Test
Build / Deploy
AWS Cloud Kata for Start-Ups and Developers
Web App
AWS Cloud Kata for Start-Ups and Developers
Real World Architecture
AWS Cloud Kata for Start-Ups and Developers
Real World “Serverless” Architecture
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
AWS Cloud Kata for Start-Ups and Developers
Swagger
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
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
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
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
AWS Cloud Kata for Start-Ups and Developers
Best Practices
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
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
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
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
AWS Cloud Kata for Start-Ups and Developers
Thank you!
Olivier Klein Senior Solutions Architect
AWS