$30 off During Our Annual Pro Sale. View Details »

what is serverless?

kensh
July 29, 2023

what is serverless?

サーバーレスの考え方と、サービスの紹介、アーキテクチャパターンをご紹介します

kensh

July 29, 2023
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    Serverlessを30分で理解する!
    〜 夏の涼を求めて 秋田へ 2023 〜
    Kensuke Shimokawa
    J A W S - U G 東 北
    AWS Serverless Specialist

    View Slide

  2. 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 Shimokawa
    Amazon Web Services Japan
    Snr. Serverless Specialist
    https://speakerdeck.com/_kensh
    https://qiita.com/_kensh

    View Slide

  3. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    "サーバーレス"とは

    View Slide

  4. © 2023, Amazon Web Services, Inc. or its affiliates.
    "サーバーレス"とは...
    サーバーがない?
    サーバーの存在を意識しない
    サーバーがない

    View Slide

  5. © 2023, Amazon Web Services, Inc. or its affiliates.
    サーバーの存在を意識しない
    • サーバーの起動、シャットダウン、ハードウエアのモニタリング
    • セキュリティパッチの適用
    • キャパシティ管理、オートスケーリングの設計・設定
    例えば...以下の項目についてはお客様側での実施は不要
    リクエストやイベントが来ると AWS 側で自動的に起動し、不要になると終了される
    メンテナンス、セキュリティパッチの適用は AWS 側で実施される
    リクエストやイベント数に応じて自動的にスケールする

    View Slide

  6. © 2023, Amazon Web Services, Inc. or its affiliates.
    アーキテクチャによる "意識" するスコープの違い
    オンプレミス 仮想サーバー
    マネージド
    +コンテナ
    マネージド
    + サーバーレス
    管理工数 (自動化=工数少)
    アプリケーションデータ・暗号化
    アプリケーションコード(PGM)
    サーバーサイド間通信の暗号化
    冗長化
    プラットフォーム/アプリ実行基盤
    OS保守 , パッチ適用
    ネットワーク構成
    コンピューティングリソース
    (物理NW, ストレージ)
    自動化に伴う制限・制約
    既存移行向き 追加/刷新向き







    "









    "












    View Slide

  7. © 2023, Amazon Web Services, Inc. or its affiliates.
    OS
    従来構成との比較例
    APP サーバー DB サーバー
    LB
    OS
    OS
    OS
    OS
    OS
    OS
    OS
    Web サーバー
    HTTPS REST
    API Gateway
    処理ロジック
    Lambda
    データ管理
    DynamoDB
    サーバー
    サーバーレス
    各機能をサーバーリソース上に実装
    各機能をサービスとして組み合わせて構築
    サーバー/OS の準備・構成
    設定・開発作業
    + 規模の見積もり
    + 可用性設計
    ロードバランシング設定
    + データ保全の検討
    設定・開発作業
    ü リクエストに量に応じて
    自動スケール
    ü 設計済みのリトライ
    ü 複数 AZ 構成
    ü データ信頼性

    View Slide

  8. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    QCD 観点で振り返る
    サーバーレスの特徴

    View Slide

  9. © 2023, Amazon Web Services, Inc. or its affiliates.
    QCD 観点で見るサーバーレスの特徴
    • リクエスト数に応じたオートスケーリング
    • マルチ AZ による障害耐性
    • セキュリティパッチの自動適用
    可用性の高いインフラによる
    一定品質の担保
    Quality (品質)の観点
    リクエスト増
    自動的に
    スケール
    AZ - a AZ - b AZ - c
    マルチ AZ による障害耐性

    View Slide

  10. © 2023, Amazon Web Services, Inc. or its affiliates.
    QCD 観点で見るサーバーレスの特徴
    • 高可用性なインフラが労力をかけずに利用可
    • 実行時間や確保する性能に応じた従量課金
    • アイドル時のリソース確保が不要
    初期投資を抑えたリスク低減
    コストの最適化
    Cost (コスト) の観点
    : 負荷 : 課金 (確保したリソース) : 負荷 : 課金対象 (実際に利用したリソース)
    サーバ確保型のコストモデル サーバーレスのコストモデル

    View Slide

  11. © 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 可⽤性テスト

    View Slide

  12. © 2023, Amazon Web Services, Inc. or its affiliates.
    どのようなワークロードがサーバーレスに向いているか
    • 将来の規模予測が難しい
    • 運用に多くの工数をかけられない、リソースがない
    • スモールスタートで早く動くプロダクトを開発したい
    • 初期投資を抑えたい
    • イベントをきっかけに処理を行うシステム

    View Slide

  13. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    AWS の代表的な
    サーバーレスサービス

    View Slide

  14. © 2023, Amazon Web Services, Inc. or its affiliates.
    AWS Lambda
    「 関 数 」 を 様 々 な イ ベ ン ト を ト リ ガ ー に 実 行
    イベントソース サービスなど
    Amazon API Gateway
    Amazon SQS
    Amazon S3
    Amazon Kinesis Data Streams
    Amazon DynamoDB
    Amazon S3
    外部の API や
    AWS のその他サービス
    AWS Lambda
    • インフラを意識せずアプリケーション開発に
    集中可能なコンピューティングサービス
    • お客様が開発したプログラムの関数をマネー
    ジドな環境で実行
    • ネイティブにサポートする標準的な言語の他
    にカスタムランタイムも利用可能
    • イベントやリクエスト量に応じて自動的にス
    ケーリング
    • 複数 AZ に展開され、耐障害性および回復性
    を備える
    • メンテナンスのため時間帯や定期的なダウン
    タイムはない
    • 1ミリ秒単位の課金で実行した分だけのお支
    払い
    Node.js
    Python
    Java
    Ruby
    Go
    C#
    PowerShell
    ... more

    View Slide

  15. © 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
    - テーブル
    - データ
    - テーブル
    - データ
    or
    RDS
    DynamoDB
    設定の実際

    View Slide

  16. © 2023, Amazon Web Services, Inc. or its affiliates.
    Amazon API Gateway
    規 模 に 応 じ た A P I の 作 成 、 維 持 、 保 護 を 行 う
    クライアント
    バックエンドサービスなど
    AWS Lambda
    AWS のサービスを
    API として公開
    オンプレミスやインターネット上の
    HTTP API
    Amazon API Gateway
    • インターフェースの抽象化とアクセス基盤を統
    合し、API エンドポイントを提供
    • RESTful API および WebSocket API をサポート
    • マネージドサービスとしての高い可用性と
    スケーラビリティ
    • アクセス制御、mTLS などの認証機能、スロッ
    トリング、キャッシュ機能、WAF 連携などを
    提供 (プロトコルごとに違いあり)
    • API キーごとにクォータやレート制限、アクセ
    ス回数の取得などが可能
    • API のコール数、転送データ量に対しての課金
    API リクエスト

    View Slide

  17. © 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 SDK
    AWS CLI
    (マネジメントコンソール)

    View Slide

  18. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    サーバーレスな
    Webアプリケーション

    View Slide

  19. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    https://aws.amazon.com/jp/serverless/patterns/serverless-pattern/
    サーバーレスパターン

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. © 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  30. © 2023, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    サーバーレスな
    静的コンテンツ管理

    View Slide

  31. © 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
    静的コンテンツ

    View Slide

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

    View Slide

  33. © 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

    View Slide

  34. © 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

    View Slide

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

    View Slide

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

    View Slide

  37. © 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

    View Slide

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

    View Slide

  39. © 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

    View Slide

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

    View Slide

  41. © 2023, Amazon Web Services, Inc. or its affiliates.
    © 2023, Amazon Web Services, Inc. or its affiliates.
    まとめ

    View Slide

  42. © 2023, Amazon Web Services, Inc. or its affiliates.
    まとめ:[再掲] QCD 観点で振り返るサーバーレスの特徴
    • リクエスト数に応じたオートスケーリング
    • マルチ AZ による障害耐性
    • セキュリティパッチの自動適用
    可用性の高いインフラによる
    一定品質の担保
    • 高可用性なインフラが労力をかけずに利用可
    • 実行時間や確保する性能に応じた従量課金
    • アイドル時のリソース確保が不要
    初期投資を抑えたリスク低減
    コストの最適化
    • プロビジョニング不要な実行環境
    • アプリケーション開発に集中
    • 非機能要件にかける労力を最小化
    Quality
    (品質)
    Cost
    (コスト)
    Delivery
    (納期・スピード)
    作業量、時間を短縮し
    リリース、ビジネスを加速

    View Slide

  43. © 2023, Amazon Web Services, Inc. or its affiliates.
    Thank you!
    © 2023, Amazon Web Services, Inc. or its affiliates.
    https://speakerdeck.com/_kensh
    https://qiita.com/_kensh

    View Slide