Slide 1

Slide 1 text

© 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 Specialist Kensuke Shimokawa

Slide 2

Slide 2 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kensuke Shimokawa Amazon Web Services Japan Snr. Serverless Specialist

Slide 3

Slide 3 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Agenda • サーバーレスサービスの紹介 • サーバーレスな Webアプリケーション • サーバーレスな ファイル変換処理 • サーバーレスな バッチ処理

Slide 4

Slide 4 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これからお話すること • サーバーレスサービスの特徴を⽤いたアプリケーションの考え⽅ • ビジネスロジックにフォーカスするアーキテクチャの導⼊ • イベント駆動に処理をすることの⼤切さ

Slide 5

Slide 5 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスサービスの紹介

Slide 6

Slide 6 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda の特徴 • サーバーのプロビジョニング/管理なしでプログラムを実⾏できるサービス • コードの実⾏やスケーリングに必要なことは、Lambda 側で実施するので、 開発者の⽅はコードを書くことにより集中できる • リクエストベースの料⾦体系 実⾏回数 (無料枠あり) 実⾏時間 (単価は確保したメモリによる) (無料枠あり) + https://aws.amazon.com/jp/lambda/pricing

Slide 7

Slide 7 text

© 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

Slide 8

Slide 8 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda で設定できる項⽬ (抜粋) • 確保するメモリの量 • 128MB 〜 10,240MB (1MBごと) • CPU 能⼒は確保するメモリの量に⽐例 • タイムアウト値 • 最⼤で 900秒 • 実⾏ IAM ロール Lambda function Amazon DynamoDB IAM Role DynamoDB に PUTする権限が必要

Slide 9

Slide 9 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Web API を開発するときに考えるべきこと API⾃体 の開発 インフラの管理 ⾼可⽤性/ スケーラビリティ設計 デプロイ管理 APIキーの管理 ・・・ 開発者はここに注力したい!

Slide 10

Slide 10 text

© 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 Gateway https://d1.awsstatic.com/webinars/jp/pdf/services/20190514_AWS-Blackbelt_APIGateway.pdf

Slide 11

Slide 11 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 豊富なバックエンド統合 その他AWSサービス 外部リソース Amazon API Gateway クライアント Amazon EC2 AWS Lambda

Slide 12

Slide 12 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS のデータベースサービス (抜粋) Amazon RDS Amazon Aurora Amazon DynamoDB Amazon Redshift Amazon Neptune Amazon ElastiCache

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスでよく使われるサービス Amazon API Gateway Amazon DynamoDB Amazon Simple Queue Service (SQS) AWS Step Functions Amazon Simple Storage Service (S3) Amazon CloudWatch AWS X-Ray AWS Lambda

Slide 15

Slide 15 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスな Webアプリケーション

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. よくある実装 クライアント マイクロサービス マイクロサービス マイクロサービス DB DB DB REST REST REST REST クライアントは、RESTでマイクロサービス と直接会話すると クライアントが知るべきマイクロサービス が どんどん増えて⾏く

Slide 18

Slide 18 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 課題 • マイクロサービス に分割するのは成功したが、サービス が増えて⾏くにつれて、クライアントが サービス エンドポイントに直接アクセスするのがつらい • 連携先のプロトコルが千差万別 • クライアント側がバックエンドの能⼒を個々に把握する必要がある • 可⽤性、回復性、スループットなど • サービス 側のREST契約が破棄されると直接クライアント影響が出る • 既存資産(モノリス)があり、全てをマイクロサービス に置き換えることはできない • モノリスとマイクロサービス を共存させたい

Slide 19

Slide 19 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. API ゲートウェイパターンを導⼊ クライアント マイクロサービス マイクロサービス マイクロサービス DB DB DB REST REST REST REST > API ゲートウェイ パターン ゲートウェイ層によって、さまざまなバックエンド サービスの呼び出しを統⼀された APIで統合 Amazon API Gateway

Slide 20

Slide 20 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. API ゲートウェイパターンを導⼊ クライアント DB DB DB > API ゲートウェイ パターン 連携先のプロトコルが多種に渡っても、 クライアントからはRESTでアクセス可能とする Amazon API Gateway Amazon SQS AWS Lambda AWS Lambda ALB AWS Fargate REST

Slide 21

Slide 21 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. モノリスを分割 クライアント DB DB > API ゲートウェイ パターン 既存のモノリスの前段に API Gateway を配置して、クライアントと モノリスやマイクロサービス との繋がりを疎結合にすることができる Amazon API Gateway AWS Lambda モノリス

Slide 22

Slide 22 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. モノリスを分割 クライアント DB DB > 処理が複雑になってきても、Lambda関数実⾏を直列にするのは バッドプラクティス Amazon API Gateway AWS Lambda モノリス

Slide 23

Slide 23 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. モノリスを分割 クライアント DB DB > ⼀⽅で、Lambda関数⾃体を モノリシック にデザインするのも、、、 Amazon API Gateway AWS Lambda モノリス

Slide 24

Slide 24 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. モノリスを分割 クライアント DB DB > サーバーレスなワークフローを利⽤するのは︖ Amazon API Gateway モノリス AWS Step Functions

Slide 25

Slide 25 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレス ワークフロー Step Functions Workflow Studio に よるワークフローの構築 ※ Express Workflow では、実⾏開始レートは毎秒 100,000 以上 をサポート AWS Step Functions

Slide 26

Slide 26 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 改善の方向性 • マイクロサービス に分割するのは成功したが、サービス が増えて⾏くにつれて、クライアントが サービス エンドポイントに直接アクセスするのがつらい • 連携先のプロトコルが千差万別 • API Gateway は多種多様なサービスと統合 • クライアント側がバックエンドの能⼒を個々に把握する必要がある • 可⽤性、回復性、スループットなど • 連携先の能⼒に応じて、API Gateway のスループットを調整できる • サービス 側のREST契約が破棄されると直接クライアント影響が出る • API Gateway 層で(ある程度)影響を吸収できる(e.g. template利⽤) • 既存資産(モノリス)があり、全てをマイクロサービス に置き換えることはできない • モノリスとマイクロサービス を共存させたい • API Gateway でバックエンドを抽象化する • クライアントからはモノリスを意識させない

Slide 27

Slide 27 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスな Webアプリケーション 静的コンテンツ管理

Slide 28

Slide 28 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. よくある実装 クライアント DB DB DB > Compute 層にロジックだけでなく、静的ファイルの⽣成も依頼 Amazon API Gateway Amazon SQS AWS Lambda AWS Lambda ALB AWS Fargate css, js, html, image 静的コンテンツ

Slide 29

Slide 29 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 課題 • 静的コンテンツアクセスが API Gateway エンドポイントに集中し、トラ フィックの⼤部分を閉めるようになった。 • マイクロサービスはロジックに専念し、API Gateway エンドポイント の負荷を下げたい • ラウンドトリップのレイテンシーも改善したい

Slide 30

Slide 30 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. オブジェクトストレージと CDN を導⼊ クライアント DB DB DB > CDNとして Amazon CloudFront を利⽤ エッジロケーションを介した世界規模のネットワークを通じてコンテンツを配信 Amazon API Gateway Amazon SQS AWS Lambda AWS Lambda ALB AWS Fargate css, js, html, image 静的コンテンツ Amazon CloudFront Amazon S3

Slide 31

Slide 31 text

© 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

Slide 32

Slide 32 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスな ファイル変換処理

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. よくある実装 Bucket AWS Lambda Amazon API Gateway クライアント リクエスト毎に ファイル変換 raw data resize: 300x300 png → jpg *.zip GET

Slide 35

Slide 35 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 課題 • リクエストごとに処理した結果を返すと処理時間や処理回数が増える • 多数の変換バリエーションがあり、今後増える可能性もある

Slide 36

Slide 36 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 改善の⽅向性 Bucket AWS Lambda クライアント PUT クライアント Bucket Amazon SNS resize: 300x300 png → jpg *.zip Object Key GET PUT GET

Slide 37

Slide 37 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 改善の⽅向性 • ファイルが⽣成されたタイミングで処理する • 多数のバリエーションは ファン・アウト で処理する

Slide 38

Slide 38 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスなバッチ処理

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. よくある実装 AWS Lambda Amazon API Gateway クライアント Event (time-based) Amazon EventBridge サーバー オンライン処理 バッチ処理 Read Write database

Slide 41

Slide 41 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 課題 • バッチタスクが増えているのに、⼈員も増えずITリソースも増えない • 決まったバッチウィンドウ時間の中で、確実に処理を終わらせる

Slide 42

Slide 42 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. バッチ処理の気持ち 決まった時間 の中で処理を 完了させたい 無理なら処理 を減らそう 処理が硬直化

Slide 43

Slide 43 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. バッチ処理からイベント駆動処理へ time process 01:00 05:00 バッチ処理 TimeWindow処理 イベント駆動処理

Slide 44

Slide 44 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 時間内にバッチ処理を終わらせたい データ量増加 でも時間内で 終了させたい 処理の並列化 チューニング イベント駆動 処理の検討

Slide 45

Slide 45 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. イベントソーシングパターン • データストアを直接更新する代わりに、ビジネスロジックに重要なイベントを 耐久性のあるイベントログに追加 • イベントレコードは個別に保存されるため、すべての更新は アトミック (分割不可かつ削減不可) • 保存されたイベントを再度処理するだけで、アプリケーションのいかなる時点 の状態でも再構築可能

Slide 46

Slide 46 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. イベントソーシングパターン イベントソース イベント 状態 プロセス プロセス プロセス イベントログ

Slide 47

Slide 47 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kinesis Data Streams (or SQS FIFO) による実装例 イベントソース Amazon Kinesis Data Streams AWS Lambda イベント メッセージ Amazon SQS FIFO or Amazon DynamoDB

Slide 48

Slide 48 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. イベントソーシングの注意点 • “現在の状態”の制御を考える必要性 • 状態を取りたい場合、必ず結果整合になる

Slide 49

Slide 49 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. HTTP リクエストをインベントソース化 Amazon API Gateway クライアント Amazon Kinesis Data Streams AWS Lambda Amazon DynamoDB API Gateway と Kinesis Data Streams や、Amazon SQS は 直接統合可能 Amazon SQS FIFO OR

Slide 50

Slide 50 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 改善の⽅向性 • 軽量なイベント駆動の⼿法で、すぐ使え、いつでも破棄可能な環境で 試⾏錯誤 • セキュリティや可⽤性、スケーラビリティは担保 • バッチ専任チームだけでなく、アプリチームが軽量バッチ処理を実践

Slide 51

Slide 51 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. まとめ

Slide 52

Slide 52 text

© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved. まとめ • サーバーレスの特徴理解し、マネージドなアプリケーションの構築 を考える • ビジネスロジックにフォーカスするアーキテクチャの導⼊ • イベント駆動に処理をすることの⼤切さを理解し、イベント発⽣を トリガーに処理できるように構築

Slide 53

Slide 53 text

© 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 Specialist Kensuke Shimokawa