Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Webアプリを Lambdaで動かすまでに考えること / How to implement m...

kensh
April 12, 2025

Webアプリを Lambdaで動かすまでに考えること / How to implement monolithic Lambda Web Application

みなさんは Spring Bootや Nodeのexpress、Flaskなどの Web Frameworkを使って構築された Webアプリを Lambdaに載せたことはありますか? また載せたいと思ったことはありますか?

そんな時に考えるポイントをご紹介します。

kensh

April 12, 2025
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 1 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . Webアプリを Lambdaで動かす までに考えること Kensuke Shimokawa J A W S - U G 北 陸 新 幹 線 Snr. Serverless Specialist Amazon Web Services Japan G.K.
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 2 アジェンダ • サーバーレスについて考える • WEB フレームワークと Lambda の親和性 について考える • ルーティングについて考える • AWS Managed Domain について考える • 最適化について考える
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 3 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . サーバーレスについて考える
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 4 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . みなさん、ご出身はどちらですか?
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 5 みなさん、ご出身はどちらですか? Serverless Native On-prem Native OR
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 6 よく教科書に出てくる アーキテクチャ 6 Amazon API Gateway AWS Lambda Client Amazon DynamoDB
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 7 サーバーレスって チョロくない?
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 8 現実世界のアーキテクチャ Contracts API Property images S3 bucket Agent Properties Web API Search function Approval function Publication evaluation event handler Customer Properties table Agent Unicorn.Contracts Unicorn.Web Contract status table Property publishing approval workflow Contract status changed event handler Contract status checker Property approvals sync Unicorn.Properties Wait for contract approval SendTaskSuccess Ingest queue Properties event bus Contracts function Contracts table Contract Pipes Contracts event bus Properties Web event bus Ingest queue Unicorn Contracts schema Registry Unicorn Properties schema registry Unicorn Web Schema Registry Parameter Store Parameter Store Parameter Store Shared infrastructure DynamoDBストリームを利用し イベント伝搬 (分散トランザクション) キューイングによる メッセージバッファー クロスアカウント、クロスリージョン
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 9 ああサーバーレスって、 分散トランザクションだ..
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 10 アーキテクチャ 非同期パターン AWS サービス全般に 関する深い知見 サーバーレス 独自のツール知識 や開発経験 高度な分散化 可観測性 DevOps 自動化 テスト サーバーレス 技術者に求められる知識は広い
  11. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 11 今日のフォーカスはこちらの皆様 Serverless Native On-prem Native
  12. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 12 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . 既存の Web アプリをそのまま Lambda に載せたい?
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 13 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . WEB フレームワークと Lambda の親和性 について考える
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 14 Web アプリはフレームワークに支えられている Ruby on Rails PHP - Laravel Python - Flask Java – Spring Boot Go - Gin
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 15 なぜフレームワークを Lambda で使いたいのか? • 慣れ親しんだ開発体験を継続したい • 開発経験者の確保が容易 (e.g. Ruby on Rails / Laravel ) • ポータビリティのため • Amazon ECS などのコンテナサービスや Amazon EC2 上のコンテナ環境との Artifact ポータビリティの確保 • 同じコンテナイメージを各種サービスで動かしたい
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 16 API Gateway Routing Endpoint /api/ GET POST /books/ /{id} GET PUT Lambda function Lambda function Lambda function Lambda function $ curl https://domain/api/books/12 API Gateway Routing
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 17 API Gateway Routing Endpoint /api/ GET POST /books/ /{id} GET PUT Lambda function Lambda function Lambda function Lambda function $ curl https://domain/api/books/12 API Gateway Routing Routing + Web Framework
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 18 WEB フレームワークと Lambda の親和性 • Lambda はリクエストごとに独立した実行環境が与えられ、強固 なisolation を持っている • WEB フレームワーク はマルチプロセスやマルチスレッド、ある いはイベントループによってリクエストを同時に受け付ける仕組 みを持っている • WEB フレームワークの多くは Lambda の実行モデルを考慮したデ ザインになっているわけでは無いことに注意
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 19 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . ルーティングについて考える
  20. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 20 API Gateway でどのようにルーティングするのか • ANY + Proxy • 認証やWAFなどは統合されて coupling に • method resource 単位の細かい統合制御はやり難い • method 単位の throttle など API Gateway ならではの機能は使い難い
  21. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 21 API Gateway Routing / 分散系 Endpoint /api/ GET POST /books/ /{id} GET PUT Lambda function Lambda function Lambda function Lambda function $ curl https://domain/api/books/12 API Gateway Routing Routing + Web Framework
  22. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 22 API Gateway Routing / Monolithic Endpoint /{proxy+} ANY Lambda function $ curl https://domain/api/books/12 API Gateway Routing Monolithic Routing
  23. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 23 Web Framework Routing / Monolithic dispatch event from mangum import Mangum app = FastAPI() handler = Mangum(app) handler @app.get(‘/api/books') def books(): return [book1, boo2, boo3, … bookN] @app.post(‘/api/books') def books(book): put_item(book) @app.put("/api/books/{id}") def books(book): put_item(book) @app.get("/api/books/{id}") def get_book(id): book = get_item(id) return book Routing $ curl https://domain/api/books/12
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 24 AWS Lambda Web Adapter Lambda ランタイム API の汎用アダプター 関数本体コードの依存関係はありません あらゆる Web フレームワークをサポート あらゆるプログラミング言語をサポート 既存のツールを使用して ローカルで開発 ローカルでテスト Rust で構築され、安全で効率的 https://github.com/awslabs/aws-lambda-web-adapter event HTTP AWS Lambda Web Adapter Lambda で Web アプリを実行するための OSS ツール
  25. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 25 AWS Lambda Web Adapter OS Packages Runtime Interface Client Web App http- adapter REST API Endpoint HTTP API Endpoint Application Load Balancer Lambda function invoke Rest API Event HTTP API v2 Event ALB Event 統合先として Lambda Function URL API Gateway REST API HTTP API Application Load Balancer をサポート コンテナと Zip パッケージを サポート aws-lambda-web-adapter Function URL Lambda で Web アプリを実行するための OSS ツール
  26. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 26 AWS Lambda Web Adapter FROM amazoncorretto:21.0.6-al2023 COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 /lambda-adapter /opt/extensions/lambda-adapter ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","/app.jar"] Dockerfile 既存の Dockerfile に一行追加するだけ (Zip artifactの場合は、Layerを追加) Lambda で Web アプリを実行するための OSS ツール
  27. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 27 AWS Lambda Web Adapter • 開発者が Adapter を意識しない • Adapter がサイドカーとして存在するため、 Artifact へのインストールが不要 • 開発者が開発するために、Adapter の理解が不要 • Adapter はフレームワーク機能は関心事から分離、完全なフレームワーク機能 • 言語 / Runtime ごとに異なる Adapter を用意する必要がない • ポータビリティ • Adapter が ロジックの外にあり、コンテナサービスとのポータビリティを維持 • Rust 言語での実装で、Latency も抑えられている • 任意の Lambda Runtime type で動作 (Hetero runtime)
  28. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 28 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . AWS Managed Domain に ついて考える
  29. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 29 表示したいページ
  30. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 30 Server Side Rendering するケース Amazon API Gateway AWS Lambda Amazon Aurora Users API HTML, JS, CSS, image https://4pckf8fve3.execute-api.ap-northeast-1.amazonaws.com/prod/{proxy+} AWS Managed Domain Stage
  31. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 31 Server Side Rendering するケース
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 32 Server Side Rendering するケース 既存のコンテナイメージの 静的ファイル上の base path のままではリソース取得できない https://4pckf8fve3.execute-api.ap-northeast-1.amazonaws.com/prod/resources/images/spring-logo.svg
  33. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 33 AWS Managed Domain の Stageの除去 • HTTP API の $default stage ( “Stage なし” )を利用
  34. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 34 AWS Managed Domain の Stageの除去 • Cloud Front を利用する • Origin Path で吸収する • Path の rewrite を行う • CloudFront Functions • Lambda@Edge • Cloud Front + Function URL も利用できる
  35. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 35 そもそも、Custom Domain を使う
  36. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 36 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . 最適化について考える
  37. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 37 アーキテクチャの進化 Time 1 7 初期開発では モノリシックを許容 1 Modular Monolith 小さな Monolith 3 Modular Monolith 3 新たな 小さな Monolith 凝集度で 分割 凝集度で分割しつつ 小さなモノリスは許容 5 凝集度の高いモジュール 適切な分離
  38. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 38 Snap Start / Zip Lambda (Python, .NET, Java) Cold Start 抑制のためのメモリキャプチャとレジューム Invoke API warm start ? Is snapshot available? Create a snapshot Resume the snapshot Invoke handler Complete Invocation No No Yes
  39. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 39 Provisioned Concurrency Concurrency time ColdStart ColdStart ColdStart 初期処理が重たいRuntimeは立ち上がりにColdStartを誘発
  40. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 40 Provisioned Concurrency Concurrency time Provisioned Concurrency WarmStart WarmStart WarmStart ProvisionedConcurrencyを使えば、ColdStartは抑制される
  41. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 41 Provisioned Concurrency(リクエストの波がある) Concurrency time Provisioned Concurrency > 0.7 = 70% > 0.7 = 70% > 0.7 = 70% Amazon Application Auto Scalingを使って ProvisionedConcurrencyUtilizationに応じてスケール Amazon Application Auto Scaling
  42. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 42 AWS CDK での Auto Scaling 設定例 // create an alias for the version. const alias = new lambda.Alias(this, 'alias', { aliasName: aliasName, version: version, }); // Create a scalable capacity const autoScaling = alias.addAutoScaling({ minCapacity: 1, maxCapacity: 100, }); // Scaling on 70% utilization autoScaling.scaleOnUtilization({ utilizationTarget: 0.7 }); https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_applicationautoscaling-readme.html
  43. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 43 暖機したいAPI Provisioned Concurrency割り当て可能: 900 Endpoint Lambda function 900 provisioned
  44. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 44 暖機したいAPIが複数ある場合 Provisioned Concurrency割り当て可能: 900 要件: 両APIとも600 rpsを満たしたい Endpoint Lambda function 300 Endpoint Lambda function 600 Duration (Warm) provisioned 500 ms(P99) 1000 ms(P99)
  45. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 45 暖機したいAPIが多数ある場合 Provisioned Concurrency割り当て可能: 900 Provisioned Concurrency利用なら Dispatch Patternなど(後ほど説明)
  46. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 46 暖機したいAPIが多数ある場合 Provisioned Concurrency割り当て可能: 900 (重要) システム全体アーキテクチャとして暖機APIが多数の場合 アーキテクチャデザインを見直すことも視野に入れる • 基本的な関数コードのチューニング • 非同期にできるか検討 • リージョン、アカウント分離 • Service Quota 緩和 • AWS Fargate などのコンテナ化検討 Provisioned Concurrencyに頼らない
  47. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 47 Dispatch Pattern - 複数関数を共通Quota消費で暖機 Provisioned Concurrency割り当て可能: 900 Dispatch Pattern で実装として対応 900 provisioned 言語関数実装 def func1 def func3 def func2 def funcN dispatcher
  48. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 48 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . まとめ
  49. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 49 まとめ • サーバーレスについて考える • サーバーレスは、分散トランザクション • WEB フレームワークと Lambda の親和性 について考える • フレームワークは Lambda の実行モデルを考慮していない • ルーティングについて考える • AWS Lambda Web Adapter でフレームワークのルーティングを利用 • AWS Managed Domain について考える • Custom Domain にするか、Cloud Front を入れるのが楽 • 最適化について考える • Provisioned Concurrency: どんな言語でも利用できる • Snap Start: Python, .NET, Java で利用可能
  50. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 50 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. . Appendix
  51. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 51 Cold Startの抑制 (リクエストの波がある) Concurrency time Provisioned Concurrency WarmStart WarmStart WarmStart ProvisionedConcurrencyを使えば、ColdStartは抑制される
  52. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 52 Cold Startの抑制 (リクエストの波がある) Concurrency time Provisioned Concurrency WarmStart WarmStart WarmStart ProvisionedConcurrencyを使えば、ColdStartは抑制される 余分なConcurrency = コスト増大
  53. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 53 Client Side Rendering するケース Amazon CloudFront AWS Lambda Amazon Aurora Users Signing AWS Lambda Function URLs Lambda@Edge API + SigV4
  54. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. . 54 Client Side Rendering するケース Amazon CloudFront Amazon Simple Storage Service (Amazon S3) Amazon API Gateway AWS Lambda Amazon Aurora Users HTML, JS, CSS, image API