Serverless Frameworkを本番環境に投入するために
Serverless Frameworkを本番環境に投⼊するために
View Slide
.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)
DreeVee (動画広告プラットフォーム) 等
サーバーサイドエンジニア
ServerlessArchitectureFaaS ServerlessFrameworkCaseStudy
サーバーレスアーキテクチャとは?
サーバー (レスアーキテクチャ) とは?
Client
常駐ProcessClientEvent
常駐ProcessClient狀態Event
常駐ProcessClient狀態Client Client Client ClientClient ClientClient Client Client Client
常駐ProcessClient狀態Client Client Client ClientClient ClientClient Client Client Client常駐Process常駐Process狀態 狀態
常駐ProcessClient狀態Client Client Client ClientClient ClientClient Client Client Client常駐Process常駐Process狀態 狀態管理
管理すべき事↓Eventの振り分け (Load balancing)処理能⼒の割り當て (Scale up)Process數の増減 (Scale out)死活監視Ver.更新
管理すべき事↓Eventの振り分け (Load balancing)処理能⼒の割り當て (Scale up)Process數の増減 (Scale out)死活監視Ver.更新(╯°□°)╯︵ ┻━┻
Client Client Client Client ClientClient ClientClient Client Client Client狀態サーバーレスアーキテクチャ管理FunFunFunFunFunFun
サーバーレスアーキテクチャ↓BaaS (Backend as a Service)FaaS (Function as a Service)S3, Cognito, Pusher, Auth0, Algolia, …
サーバーレスアーキテクチャ↓Functionを第⼀級市⺠として扱ふインフラ設計。Application serverの、出來る事を殆ど減らさず管理costを無くせる。
Awesome↓https://github.com/anaibol/awesome-serverless
Tips:「serverless」でググってはいけない(☍﹏⁰)(Serverless Framework (後述) の事しか出て來ない)
FaaS ?
FaaS ?↓AWS LambdaGoogle Cloud FunctionsAzure FunctionsIBM OpenWhisk
FaaS (Function as a Service) ?
FaaS (函數 as a Service) ?
Function (函數, 働き)
P Qf
Function↓函數: ⼊⼒と出⼒の對働き: 狀態遷移(⾮形式的に) ⾔葉のイメージとしては、Function=函數∪働き
Function↓1つの⼊⼒と1つの出⼒を持ち、狀態を變更する
Eventsource狀態Function變更結果Event
Eventsource狀態Function變更結果EventFunction
狀態Function變更FunctionEventsourceEventsourceEventsource
Eventsource狀態FunctionEventsourceEventsourceFunctionFunctionFunctionFunctionFunctionFunctionEvent
Eventsource狀態FunctionEventsourceEventsourceFunctionFunctionFunctionFunctionFunctionFunctionEventEventとFunctionが1對1
FaaS↓Functionを實⾏するインフラを提供する
FaaS↓「 1つの⼊⼒と1つの出⼒を持ち、狀態を變更する」ものを實⾏するインフラを提供する
管理すべきだった事↓Eventの振り分け (Load balancing)処理能⼒の割り當て (Scale up)Process數の増減 (Scale out)死活監視Ver.更新
FaaSで管理すべき事↓Eventの振り分け (Load balancing)処理能⼒の割り當て (Scale up)→設定するProcess數の増減 (Scale out)死活監視→Log監視Ver.更新→⾃然なBlue-Green
FaaSで管理すべき事↓Eventの振り分け (Load balancing)処理能⼒の割り當て (Scale up)→設定するProcess數の増減 (Scale out)死活監視→Log監視Ver.更新→⾃然なBlue-Green♪(*>ω<)o∀*∀o(>ω<*)♪カンパーイ
Tips:「FaaS」でググってはいけない(☍﹏⁰)(Fraud as a Service: 詐欺代⾏named by RSA Security@2008の事しか出て來ない)
https://serverless.com/
Serverless Framework↓FaaSを中⼼として、サーバーレスアーキテクチャでserviceを構築する爲のframework
(〃l _ l)<でもFaaSは樂だし、framework要らなくない?
(まぁFaaSは樂だよ。FaaSは)Oo。.(l _ l〃)
Eventsource狀態Function變更結果EventFunctionEvent sourceって具體的には何?
Evet source↓Storageの更新/cron/HTTP access/MQTT access等例へばAWS Lambdaに對するAPI Gateway
API Gateway↓何かに對するREST interfaceを作成するどうやって?
Terraformで剥き出しのAWS resourceを管理する例API GatewayとLambdaの組み合はせでリリースするバージョンを制御する - c4se記:さっちゃんですよ☆http://c4se.hatenablog.com/entry/2016/07/26/122508
Terraformで剥き出しのAWS resourceを管理する例API GatewayとLambdaの組み合はせでリリースするバージョンを制御する - c4se記:さっちゃんですよ☆ http://c4se.hatenablog.com/entry/2016/07/26/122508管理するもの↓aws_lambda_function/aws_lambda_alias/aws_lambda_alias/aws_lambda_permission/aws_lambda_permission/aws_api_gateway_rest_api/aws_api_gateway_deployment/aws_api_gateway_deployment/aws_api_gateway_resource/aws_api_gateway_resource/aws_api_gateway_method/aws_api_gateway_method/aws_api_gateway_integration/aws_api_gateway_integration/aws_api_gateway_method_response/aws_api_gateway_method_response/aws_api_gateway_method_response/aws_api_gateway_method_response/aws_api_gateway_method_response/aws_api_gateway_method_response/aws_api_gateway_integration_response/aws_api_gateway_integration_response/aws_api_gateway_integration_response/aws_api_gateway_integration_response/aws_api_gateway_integration_response/aws_api_gateway_integration_response
Terraformで剥き出しのAWS resourceを管理する例API GatewayとLambdaの組み合はせでリリースするバージョンを制御する - c4se記:さっちゃんですよ☆ http://c4se.hatenablog.com/entry/2016/07/26/122508deploy⼿順↓1. aws_lambda_alias.prodのfunction_versionを"$LATEST" にしておく。Lambda函數作成前で、publish_versionもしてゐずバージョンが未だ無い爲。2. LambdaとAPI Gatewayをデプロイ。3. API Gatewayをstagingステージにデプロイ。4. stagingで検証。5. Lambdaをpublish_versionして、Versionをメモする。6. aws_lambda_alias.prodのfunction_versionを 上でメモしたバージョンに書き換へる。7. Lambdaをデプロイ。8. API Gatewayをprodステージにデプロイ。
Terraformで剥き出しのAWS resourceを管理する例API GatewayとLambdaの組み合はせでリリースするバージョンを制御する - c4se記:さっちゃんですよ☆ http://c4se.hatenablog.com/entry/2016/07/26/122508deploy⼿順↓1. aws_lambda_alias.prodのfunction_versionを"$LATEST" にしておく。Lambda函數作成前で、publish_versionもしてゐずバージョンが未だ無い爲。2. LambdaとAPI Gatewayをデプロイ。3. API Gatewayをstagingステージにデプロイ。4. stagingで検証。5. Lambdaをpublish_versionして、Versionをメモする。6. aws_lambda_alias.prodのfunction_versionを 上でメモしたバージョンに書き換へる。7. Lambdaをデプロイ。8. API Gatewayをprodステージにデプロイ。(╯°□°)╯︵ ┻━┻
Terraformで剥き出しのAWS resourceを管理する例API GatewayとLambdaの組み合はせでリリースするバージョンを制御する - c4se記:さっちゃんですよ☆ http://c4se.hatenablog.com/entry/2016/07/26/122508deploy⼿順↓1. aws_lambda_alias.prodのfunction_versionを"$LATEST" にしておく。Lambda函數作成前で、publish_versionもしてゐずバージョンが未だ無い爲。2. LambdaとAPI Gatewayをデプロイ。3. API Gatewayをstagingステージにデプロイ。4. stagingで検証。5. Lambdaをpublish_versionして、Versionをメモする。6. aws_lambda_alias.prodのfunction_versionを 上でメモしたバージョンに書き換へる。7. Lambdaをデプロイ。8. API Gatewayをprodステージにデプロイ。Event sourceの管理とEvent sourceとFunctionの對應の管理には、温かみの在る⾮⼈間的な作業が⼀杯!
Serverless Frameworkで管理する例↓provider:stage: stagingfunctions:example:handler: handler.handlerevents:- http:path: "example/example”method: getintegration: lambda- http:path: "example/example”method: putintegration: lambda
Serverless Frameworkで管理する例↓`sls deploy ‒s prod`
Serverless Frameworkで管理する例↓`sls deploy ‒s prod`♪(*>ω<)o∀*∀o(>ω<*)♪カンパーイ
Serverless Frameworkとは↓FaaSに對して、Function/Event source/狀態を保持するserviceをdeployするtool
AWSに對するServerless Frameworkとは↓Cloud Formation + LambdaSuuuuuuuuuuuuuuuuper simple!
Tips:「Serverless Framework」でググってもよいです(〃l _ l)
⼩さなHTTP serviceを作った話Serverless Frameworkをv0.5→v1.0に更新した話RDSのslow queryを監視する話
RDSが1時間毎に作るslowquerylogRDSが1時間毎に作るslowquerylogRDSが1時間毎に作るslowquerylogCloudWatchScheduleLambdaChatroom1回/1 hour取得投稿
service: xxx-export-slow-queryprovider:name: awsregion: ap-northeast-1stage: prodruntime: python2.7iamRoleStatements:- Effect: AllowAction:- rds:DescribeDBLogFiles- rds:DownloadDBLogFilePortionResource:- arn:aws:rds:*package:exclude:- tasks.py- testsfunctions:exportToChat:handler: handler.export_to_chatmemorySize: 512timeout: 6events:- schedule: rate(1 hour)
⼀番難しかったところ↓slowquery.logを取得する最⼩のIAM roleを確かめるところ
問題は簡單↓ユーザーの⾏動に依って増減する或るもの個數を管理せよ増減履歴も保存する半年間増減しなければ0個にする
解決は難しい↓ユーザー數がそこそこ多い増減囘數もわりと多い參照囘數はかなり多い増減履歴は凄く多いAccess數が突然増える個數がおかしくなったらコ◯スよ既存のsystemがかなり複雜
既存のsystemが複雜だね↓でもmicro serviceに出來るねインフラが難しいね↓でもAPI Gateway + Lambda + DynamoDBなら簡單だね!
APIGatewayLambda取得/増減 DynamoDB(個數)DynamoDB(履歴)GET/PUT追記GET/PUT
⼀番難しかったところ↓DynamoDBでtransactionっぽい事が出來るのを確かめるところ(+ 途中迄Terraformで組んでたので、Terraformが難しかった)
前記の⼩さなHTTP serviceはServerless Framework v0.5で作ってゐた↓v1.0がreleaseされたので、更新した
v0.5/s-project.jsonfunctions/fun-a/s-function.jsonaʼs codefun-b/s-function.jsonbʼs codev1.0/serverless.ymlfun-a&fun-bʼs code
v0.5→v1.0↓設定が抽象化された (AWSに依存した記法が殆ど無くなった)設定が短くなったFunction毎にcodeを分けなくなったAWSに作るresourceの命名法が變はった
v0.5→v1.0↓AWSに作るresourceの命名法が變はった事を利⽤して、無停⽌でupgradeした
舊LambdaAPIGatewaycustomedomain舊APIGatewayresource舊LambdaAPIGatewaycustomedomain舊APIGatewayresource新Lambda新APIGatewayresourceAPIGatewaycustomedomain新Lambda新APIGatewayresource
⼀番難しかったところ↓檢證して、インフラを變更する恐怖を乘り越えろ
纏め
What?/Why?サーバーレスアーキテクチャ→Functionを第⼀級市⺠として扱ふインフラ設計Function→1つの⼊⼒と1つの出⼒を持ち、狀態を變更するServerless Framework→FaaSと周邊serviceをdeployするtool
What?/Why?サーバーレスアーキテクチャ→インフラ管理costが激減するFaaS→Applicationとインフラ管理を分離出來るServerless Framework→FaaSへのdeployが凄く簡單になる