Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

© 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ストリームを利用し イベント伝搬 (分散トランザクション) キューイングによる メッセージバッファー クロスアカウント、クロスリージョン

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

© 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 に載せたい?

Slide 13

Slide 13 text

© 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 の親和性 について考える

Slide 14

Slide 14 text

© 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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

© 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

Slide 17

Slide 17 text

© 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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

© 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

Slide 22

Slide 22 text

© 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

Slide 23

Slide 23 text

© 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

Slide 24

Slide 24 text

© 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 ツール

Slide 25

Slide 25 text

© 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 ツール

Slide 26

Slide 26 text

© 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 ツール

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

© 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 に ついて考える

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

© 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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

© 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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

© 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 も利用できる

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

© 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 凝集度の高いモジュール 適切な分離

Slide 38

Slide 38 text

© 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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

© 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

Slide 42

Slide 42 text

© 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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

© 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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

© 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 で利用可能

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

© 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

Slide 54

Slide 54 text

© 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