サーバーレスは利用した分の支払いになっていることや、自動でスケールしてくれることなど、これから構築しようとしているビジネスを“試す“環境としてよく利用されます。それではサーバーレスの代表的なアーキテクチャはどのような姿をしているのでしょうか? 今回はその辺りを発表いたします。
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスサービスの特徴とアプリケーションの考え⽅Amazon Web Services Japan G.K.Snr. Serverless SpecialistKensuke Shimokawa
View Slide
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Kensuke ShimokawaAmazon Web Services JapanSnr. Serverless Specialist
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Agenda• サーバーレスサービスの紹介• サーバーレスな Webアプリケーション• サーバーレスな ファイル変換処理• サーバーレスな バッチ処理
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.これからお話すること• サーバーレスサービスの特徴を⽤いたアプリケーションの考え⽅• ビジネスロジックにフォーカスするアーキテクチャの導⼊• イベント駆動に処理をすることの⼤切さ
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスサービスの紹介
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.AWS Lambda の特徴• サーバーのプロビジョニング/管理なしでプログラムを実⾏できるサービス• コードの実⾏やスケーリングに必要なことは、Lambda 側で実施するので、開発者の⽅はコードを書くことにより集中できる• リクエストベースの料⾦体系実⾏回数(無料枠あり)実⾏時間(単価は確保したメモリによる)(無料枠あり)+https://aws.amazon.com/jp/lambda/pricing
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.AWS Lambda におけるコーディング• 対応⾔語• Node.js、Python 、Java、Go、Ruby、C# 、PowerShell• サポートされていない⾔語は、カスタムランタイムを実装することで利⽤可能• ハンドラーで呼び出す関数を指定する• デフォルトでは lambda_function.lambda_handler
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.AWS Lambda で設定できる項⽬ (抜粋)• 確保するメモリの量• 128MB 〜 10,240MB (1MBごと)• CPU 能⼒は確保するメモリの量に⽐例• タイムアウト値• 最⼤で 900秒• 実⾏ IAM ロールLambda function Amazon DynamoDBIAM RoleDynamoDB にPUTする権限が必要
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Web API を開発するときに考えるべきことAPI⾃体の開発インフラの管理⾼可⽤性/スケーラビリティ設計デプロイ管理APIキーの管理・・・開発者はここに注力したい!
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Amazon API Gateway の特徴• サーバーをプロビジョニング/管理することなく、APIを作成・管理• 可⽤性の担保、スケーリング、および API キー管理といった API 開発で必要なことを、API Gateway に任せることで、開発者はビジネスの差別化に繋がる作業に集中できる• REST API、WebSocket API、HTTP API に対応• リクエストベースの料⾦体系(REST API の場合)実⾏回数(AWSサインアップ⽇から12ヶ⽉間、無料枠あり)( + キャッシュメモリ量) + データ転送料金参考) BlackBelt シリーズ: Amazon API Gatewayhttps://d1.awsstatic.com/webinars/jp/pdf/services/20190514_AWS-Blackbelt_APIGateway.pdf
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.豊富なバックエンド統合その他AWSサービス外部リソースAmazon API GatewayクライアントAmazon EC2AWS Lambda
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.AWS のデータベースサービス (抜粋)Amazon RDS Amazon Aurora Amazon DynamoDBAmazon Redshift Amazon NeptuneAmazon ElastiCache
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Amazon DynamoDB の特徴• フルマネージド型の NoSQL データベースサービス• 3つの Availability Zone に保存されるので信頼性が⾼い• 性能要件に応じて、テーブルごとにスループットキャパシティを定義するキャパシティの Auto Scaling、オンデマンドキャパシティの設定も可能• ストレージの容量制限がない• 料⾦体系(キャパシティを定義する場合)設定したRead キャパシティユニットWrite キャパシティユニット(無料枠あり)+ ストレージ利⽤料 ( + オプション機能料⾦ )https://aws.amazon.com/jp/dynamodb/pricing/
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスでよく使われるサービスAmazonAPI GatewayAmazonDynamoDBAmazon SimpleQueue Service(SQS)AWS StepFunctionsAmazon SimpleStorage Service(S3)AmazonCloudWatchAWS X-RayAWS Lambda
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスなWebアプリケーション
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.よくある実装クライアントマイクロサービスマイクロサービスマイクロサービスDBDBDBRESTRESTRESTRESTクライアントは、RESTでマイクロサービスと直接会話するとクライアントが知るべきマイクロサービス がどんどん増えて⾏く
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.課題• マイクロサービス に分割するのは成功したが、サービス が増えて⾏くにつれて、クライアントがサービス エンドポイントに直接アクセスするのがつらい• 連携先のプロトコルが千差万別• クライアント側がバックエンドの能⼒を個々に把握する必要がある• 可⽤性、回復性、スループットなど• サービス 側のREST契約が破棄されると直接クライアント影響が出る• 既存資産(モノリス)があり、全てをマイクロサービス に置き換えることはできない• モノリスとマイクロサービス を共存させたい
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.API ゲートウェイパターンを導⼊クライアントマイクロサービスマイクロサービスマイクロサービスDBDBDBRESTRESTRESTREST>API ゲートウェイパターンゲートウェイ層によって、さまざまなバックエンドサービスの呼び出しを統⼀された APIで統合AmazonAPI Gateway
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.API ゲートウェイパターンを導⼊クライアントDBDBDB>API ゲートウェイパターン連携先のプロトコルが多種に渡っても、クライアントからはRESTでアクセス可能とするAmazonAPI GatewayAmazon SQS AWS LambdaAWS LambdaALB AWS FargateREST
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.モノリスを分割クライアントDBDB>API ゲートウェイパターン既存のモノリスの前段に API Gateway を配置して、クライアントとモノリスやマイクロサービス との繋がりを疎結合にすることができるAmazonAPI GatewayAWS Lambdaモノリス
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.モノリスを分割クライアントDBDB>処理が複雑になってきても、Lambda関数実⾏を直列にするのはバッドプラクティスAmazonAPI GatewayAWS Lambdaモノリス
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.モノリスを分割クライアントDBDB>⼀⽅で、Lambda関数⾃体を モノリシック にデザインするのも、、、AmazonAPI GatewayAWS Lambdaモノリス
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.モノリスを分割クライアントDBDB>サーバーレスなワークフローを利⽤するのは︖AmazonAPI GatewayモノリスAWS Step Functions
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレス ワークフローStep Functions Workflow Studio によるワークフローの構築※ Express Workflow では、実⾏開始レートは毎秒100,000 以上 をサポートAWS Step Functions
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.改善の方向性• マイクロサービス に分割するのは成功したが、サービス が増えて⾏くにつれて、クライアントがサービス エンドポイントに直接アクセスするのがつらい• 連携先のプロトコルが千差万別• API Gateway は多種多様なサービスと統合• クライアント側がバックエンドの能⼒を個々に把握する必要がある• 可⽤性、回復性、スループットなど• 連携先の能⼒に応じて、API Gateway のスループットを調整できる• サービス 側のREST契約が破棄されると直接クライアント影響が出る• API Gateway 層で(ある程度)影響を吸収できる(e.g. template利⽤)• 既存資産(モノリス)があり、全てをマイクロサービス に置き換えることはできない• モノリスとマイクロサービス を共存させたい• API Gateway でバックエンドを抽象化する• クライアントからはモノリスを意識させない
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスなWebアプリケーション静的コンテンツ管理
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.よくある実装クライアントDBDBDB>Compute 層にロジックだけでなく、静的ファイルの⽣成も依頼AmazonAPI GatewayAmazon SQS AWS LambdaAWS LambdaALB AWS Fargatecss, js, html, image静的コンテンツ
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.課題• 静的コンテンツアクセスが API Gateway エンドポイントに集中し、トラフィックの⼤部分を閉めるようになった。• マイクロサービスはロジックに専念し、API Gateway エンドポイントの負荷を下げたい• ラウンドトリップのレイテンシーも改善したい
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.オブジェクトストレージと CDN を導⼊クライアントDBDBDB>CDNとして Amazon CloudFront を利⽤エッジロケーションを介した世界規模のネットワークを通じてコンテンツを配信AmazonAPI GatewayAmazon SQS AWS LambdaAWS LambdaALB AWS Fargatecss, js, html, image静的コンテンツAmazon CloudFront Amazon S3
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.改善の⽅向性• 静的コンテンツアクセスが API Gateway エンドポイントに集中し、トラフィックの⼤部分を閉めるようになった。• マイクロサービス はロジックに専念し、API Gatewayエンドポイントの負荷を下げたい• 静的コンテンツトラフィックを Cloud Front や S3 へオフロード• ラウンドトリップのレイテンシーも改善したい• 静的コンテンツGETのトラフィックに対しAmazon CloudFrontキャッシュ利⽤• CloudFront と AWSオリジンサービス 間の通信費は無料• S3へのアクセスを CloudFront 経由のみに絞れるhttps://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスなファイル変換処理
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.よくある実装BucketAWSLambdaAmazonAPI Gatewayクライアントリクエスト毎にファイル変換raw dataresize: 300x300png → jpg*.zipGET
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.課題• リクエストごとに処理した結果を返すと処理時間や処理回数が増える• 多数の変換バリエーションがあり、今後増える可能性もある
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.改善の⽅向性BucketAWS LambdaクライアントPUTクライアントBucketAmazon SNSresize: 300x300png → jpg*.zipObject KeyGETPUTGET
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.改善の⽅向性• ファイルが⽣成されたタイミングで処理する• 多数のバリエーションは ファン・アウト で処理する
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.サーバーレスなバッチ処理
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.よくある実装AWSLambdaAmazonAPI GatewayクライアントEvent(time-based)AmazonEventBridgeサーバーオンライン処理バッチ処理ReadWritedatabase
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.課題• バッチタスクが増えているのに、⼈員も増えずITリソースも増えない• 決まったバッチウィンドウ時間の中で、確実に処理を終わらせる
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.バッチ処理の気持ち決まった時間の中で処理を完了させたい無理なら処理を減らそう処理が硬直化
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.バッチ処理からイベント駆動処理へtimeprocess01:00 05:00バッチ処理TimeWindow処理イベント駆動処理
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.時間内にバッチ処理を終わらせたいデータ量増加でも時間内で終了させたい処理の並列化チューニングイベント駆動処理の検討
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.イベントソーシングパターン• データストアを直接更新する代わりに、ビジネスロジックに重要なイベントを耐久性のあるイベントログに追加• イベントレコードは個別に保存されるため、すべての更新はアトミック (分割不可かつ削減不可)• 保存されたイベントを再度処理するだけで、アプリケーションのいかなる時点の状態でも再構築可能
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.イベントソーシングパターンイベントソースイベント状態プロセスプロセスプロセスイベントログ
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Kinesis Data Streams (or SQS FIFO) による実装例イベントソースAmazon KinesisData StreamsAWS LambdaイベントメッセージAmazon SQSFIFOorAmazonDynamoDB
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.イベントソーシングの注意点• “現在の状態”の制御を考える必要性• 状態を取りたい場合、必ず結果整合になる
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.HTTP リクエストをインベントソース化AmazonAPI GatewayクライアントAmazon KinesisData StreamsAWS LambdaAmazonDynamoDBAPI Gateway と Kinesis Data Streams や、Amazon SQS は直接統合可能Amazon SQSFIFOOR
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.改善の⽅向性• 軽量なイベント駆動の⼿法で、すぐ使え、いつでも破棄可能な環境で試⾏錯誤• セキュリティや可⽤性、スケーラビリティは担保• バッチ専任チームだけでなく、アプリチームが軽量バッチ処理を実践
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.まとめ
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.まとめ• サーバーレスの特徴理解し、マネージドなアプリケーションの構築を考える• ビジネスロジックにフォーカスするアーキテクチャの導⼊• イベント駆動に処理をすることの⼤切さを理解し、イベント発⽣をトリガーに処理できるように構築
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Thank you!Amazon Web Services Japan G.K.Snr. Serverless SpecialistKensuke Shimokawa