サーバーレスの考え方と、サービスの紹介、アーキテクチャパターンをご紹介します
© 2023, Amazon Web Services, Inc. or its affiliates.© 2023, Amazon Web Services, Inc. or its affiliates.Serverlessを30分で理解する!〜 夏の涼を求めて 秋田へ 2023 〜Kensuke ShimokawaJ A W S - U G 東 北AWS Serverless Specialist
View Slide
MODERNIZATION JOURNEY© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark.About me…Kensuke ShimokawaAmazon Web Services JapanSnr. Serverless Specialisthttps://speakerdeck.com/_kenshhttps://qiita.com/_kensh
© 2023, Amazon Web Services, Inc. or its affiliates.© 2023, Amazon Web Services, Inc. or its affiliates."サーバーレス"とは
© 2023, Amazon Web Services, Inc. or its affiliates."サーバーレス"とは...サーバーがない?サーバーの存在を意識しないサーバーがない
© 2023, Amazon Web Services, Inc. or its affiliates.サーバーの存在を意識しない• サーバーの起動、シャットダウン、ハードウエアのモニタリング• セキュリティパッチの適用• キャパシティ管理、オートスケーリングの設計・設定例えば...以下の項目についてはお客様側での実施は不要リクエストやイベントが来ると AWS 側で自動的に起動し、不要になると終了されるメンテナンス、セキュリティパッチの適用は AWS 側で実施されるリクエストやイベント数に応じて自動的にスケールする
© 2023, Amazon Web Services, Inc. or its affiliates.アーキテクチャによる "意識" するスコープの違いオンプレミス 仮想サーバーマネージド+コンテナマネージド+ サーバーレス管理工数 (自動化=工数少)アプリケーションデータ・暗号化アプリケーションコード(PGM)サーバーサイド間通信の暗号化冗長化プラットフォーム/アプリ実行基盤OS保守 , パッチ適用ネットワーク構成コンピューティングリソース(物理NW, ストレージ)自動化に伴う制限・制約既存移行向き 追加/刷新向きお客様お客様サ"ビス提供者お客様サ"ビス提供者お客様コンテナ
© 2023, Amazon Web Services, Inc. or its affiliates.OS従来構成との比較例APP サーバー DB サーバーLBOSOSOSOSOSOSOSWeb サーバーHTTPS RESTAPI Gateway処理ロジックLambdaデータ管理DynamoDBサーバーサーバーレス各機能をサーバーリソース上に実装各機能をサービスとして組み合わせて構築サーバー/OS の準備・構成設定・開発作業+ 規模の見積もり+ 可用性設計ロードバランシング設定+ データ保全の検討設定・開発作業ü リクエストに量に応じて自動スケールü 設計済みのリトライü 複数 AZ 構成ü データ信頼性
© 2023, Amazon Web Services, Inc. or its affiliates.© 2023, Amazon Web Services, Inc. or its affiliates.QCD 観点で振り返るサーバーレスの特徴
© 2023, Amazon Web Services, Inc. or its affiliates.QCD 観点で見るサーバーレスの特徴• リクエスト数に応じたオートスケーリング• マルチ AZ による障害耐性• セキュリティパッチの自動適用可用性の高いインフラによる一定品質の担保Quality (品質)の観点リクエスト増自動的にスケールAZ - a AZ - b AZ - cマルチ AZ による障害耐性
© 2023, Amazon Web Services, Inc. or its affiliates.QCD 観点で見るサーバーレスの特徴• 高可用性なインフラが労力をかけずに利用可• 実行時間や確保する性能に応じた従量課金• アイドル時のリソース確保が不要初期投資を抑えたリスク低減コストの最適化Cost (コスト) の観点: 負荷 : 課金 (確保したリソース) : 負荷 : 課金対象 (実際に利用したリソース)サーバ確保型のコストモデル サーバーレスのコストモデル
© 2023, Amazon Web Services, Inc. or its affiliates.QCD 観点で見るサーバーレスの特徴• プロビジョニング不要な実行環境• アプリケーション開発に集中• 非機能要件にかける労力を最小化作業量、時間を短縮しリリース、ビジネスを加速Delivery (納期、スピード) の観点計画・概要設計 詳細設計 開発計画 設計 開発サーバ管理パッチ適用ソフトウェア保守サーバー増強ランタイムVer upランタイムVer up▼ Production ▲ Production市場投入時間を短縮n サイジングn 可⽤性設計n 将来予測/計画n 負荷予測n コード開発n テストn サーバー設定n 環境整備n コード開発n 機能テストn 可⽤性テスト
© 2023, Amazon Web Services, Inc. or its affiliates.どのようなワークロードがサーバーレスに向いているか• 将来の規模予測が難しい• 運用に多くの工数をかけられない、リソースがない• スモールスタートで早く動くプロダクトを開発したい• 初期投資を抑えたい• イベントをきっかけに処理を行うシステム
© 2023, Amazon Web Services, Inc. or its affiliates.© 2023, Amazon Web Services, Inc. or its affiliates.AWS の代表的なサーバーレスサービス
© 2023, Amazon Web Services, Inc. or its affiliates.AWS Lambda「 関 数 」 を 様 々 な イ ベ ン ト を ト リ ガ ー に 実 行イベントソース サービスなどAmazon API GatewayAmazon SQSAmazon S3Amazon Kinesis Data StreamsAmazon DynamoDBAmazon S3外部の API やAWS のその他サービスAWS Lambda• インフラを意識せずアプリケーション開発に集中可能なコンピューティングサービス• お客様が開発したプログラムの関数をマネージドな環境で実行• ネイティブにサポートする標準的な言語の他にカスタムランタイムも利用可能• イベントやリクエスト量に応じて自動的にスケーリング• 複数 AZ に展開され、耐障害性および回復性を備える• メンテナンスのため時間帯や定期的なダウンタイムはない• 1ミリ秒単位の課金で実行した分だけのお支払いNode.jsPythonJavaRubyGoC#PowerShell... more
© 2023, Amazon Web Services, Inc. or its affiliates.https://xxxxx.execute-api.<>.amazonaws.com/Prod/構成・設定- URL- 認証- キャッシュ- 関数紐付け※ デフォルトURLカスタムURL設定可能// DynamoDB へのアクセスconst dynamodb = require('aws-sdk/clients/dynamodb');const docClient = new dynamodb.DocumentClient();// テーブル名を環境変数から取得const tableName = process.env.SAMPLE_TABLE;exports.getAllItemsHandler = async (event) => {const { httpMethod, path } = event;if (httpMethod !== 'GET') {throw new Error( 'getAllItems: GETである必要があります’ );}// ログへの出力console.log('received:', JSON.stringify(event));// 全件取得const params = { TableName: tableName };const { Items } = await docClient.scan(params).promise();// 出力の準備const response = {statusCode: 200,body: JSON.stringify(Items),}return response;}; ※ Node.js の場合getAllItemsFunction- テーブル- データ- テーブル- データorRDSDynamoDB設定の実際
© 2023, Amazon Web Services, Inc. or its affiliates.Amazon API Gateway規 模 に 応 じ た A P I の 作 成 、 維 持 、 保 護 を 行 うクライアントバックエンドサービスなどAWS LambdaAWS のサービスをAPI として公開オンプレミスやインターネット上のHTTP APIAmazon API Gateway• インターフェースの抽象化とアクセス基盤を統合し、API エンドポイントを提供• RESTful API および WebSocket API をサポート• マネージドサービスとしての高い可用性とスケーラビリティ• アクセス制御、mTLS などの認証機能、スロットリング、キャッシュ機能、WAF 連携などを提供 (プロトコルごとに違いあり)• API キーごとにクォータやレート制限、アクセス回数の取得などが可能• API のコール数、転送データ量に対しての課金API リクエスト
© 2023, Amazon Web Services, Inc. or its affiliates.Amazon DynamoDB容 量 制 限 の な い 、 完 全 マ ネ ー ジ ド 型 N o S Q L サ ー ビ ス複数 AZ にデータを保存マルチリージョンのデータレプリケーション変更イベントのストリーミングデータを提供可能Amazon DynamoDB• 1桁ミリ秒単位の安定したパフォーマンスでアクセスが可能な NoSQL サービス• 保存時の暗号化、秒単位の自動バックアップ、リストアによりデータを保護• データは 3つの AZ に保存され保護される• グローバルテーブルによるリージョン間のデータレプリケーションをサポート• 変更イベントをストリームデータとしてリアルタイムに提供可能• データのインポート / エクスポートをサポート• 料金はデータを格納しているストレージ容量と、データの読み書きの使用量に応じて課金API(HTTP リクエスト)AWS SDKAWS CLI(マネジメントコンソール)
© 2023, Amazon Web Services, Inc. or its affiliates.© 2023, Amazon Web Services, Inc. or its affiliates.サーバーレスなWebアプリケーション
© 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.https://aws.amazon.com/jp/serverless/patterns/serverless-pattern/サーバーレスパターン
© 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.サーバーレスな静的コンテンツ管理
© 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.サーバーレスなファイル変換処理
© 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.© 2023, Amazon Web Services, Inc. or its affiliates.まとめ
© 2023, Amazon Web Services, Inc. or its affiliates.まとめ:[再掲] QCD 観点で振り返るサーバーレスの特徴• リクエスト数に応じたオートスケーリング• マルチ AZ による障害耐性• セキュリティパッチの自動適用可用性の高いインフラによる一定品質の担保• 高可用性なインフラが労力をかけずに利用可• 実行時間や確保する性能に応じた従量課金• アイドル時のリソース確保が不要初期投資を抑えたリスク低減コストの最適化• プロビジョニング不要な実行環境• アプリケーション開発に集中• 非機能要件にかける労力を最小化Quality(品質)Cost(コスト)Delivery(納期・スピード)作業量、時間を短縮しリリース、ビジネスを加速
© 2023, Amazon Web Services, Inc. or its affiliates.Thank you!© 2023, Amazon Web Services, Inc. or its affiliates.https://speakerdeck.com/_kenshhttps://qiita.com/_kensh