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

AWS Lambda の上でいろんなWEB フレームワークを動かそう! / Web Frameworks on Lambda

kensh
November 10, 2022

AWS Lambda の上でいろんなWEB フレームワークを動かそう! / Web Frameworks on Lambda

AWS Lambda はサーバーレスサービスの中で、compute処理を担うサービスになります。その役割はシンプルにロジックを実行することにあります。そして、その他の非機能要件についてはどうするかというと、他のAWSサービスに任せたりします。たとえばログは Amazon CloudWatch Logs、トレースは AWS X-Ray、認証認可は Amazon Cognito などなど。

その中でよく質問にあがるのが、オンプレミスやEC2で使っていたプログラミング言語用のフレームワークをそのまま Lambda関数で使いたい!どうすればよいですか? というものです。

このセッションでは、そういう質問があがる背景から入り、AWS Lambda の機能によるうまいやり方を探っていきます。

kensh

November 10, 2022
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

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

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda の上でいろんな WEB フレームワークを動かそう! Kensuke Shimokawa E - 3 Snr. Serverless Specialist Amazon Web Services Japan Slides https://speakerdeck.com/_kensh/ Qiita https://qiita.com/_kensh
  2. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. About Me… Kensuke Shimokawa Amazon Web Services Japan Snr. Serverless Specialist Slides https://speakerdeck.com/_kensh/ Qiita https://qiita.com/_kensh
  3. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 話すこと、話さないこと • 話すこと § AWS Lambda にWEBフレームワークを載せるというのはどういうことなのか? – スケール、ポータビリティ § 技術的な実現方法とその解説 – AWS Lambda と WEB フレームワークの親和性を高める • 話さないこと § 個別の WEB フレームワークの AWS Lambda への載せ方の詳細 – e.g. Rails on Lambda / Laravel on Lambda / Spring Boot on Lambda
  4. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. このセッションのモチベーション • そもそも Lambda に WEB フレームワークは必要か? § AWS Lambda のスケール / 実行モデル について理解を深める • AWS Lambda に WEB フレームワークを載せる技術パターンを理解 § 現在(2022時点で)おすすめの手法とは?
  5. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Agenda • 結論から先に • AWS Lambda 実装のプラクティス • AWS Lambda に WEBフレームワークを載せる (・・・ための挑戦の歴史) • AWS Lambda の実行モデル • Tenets / フレームワークを載せるために • AWS Lambda でのサイドカーパターン • AWS Lambda Web Adapter について
  6. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. tl;dr - 結論から先に
  7. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージを 異種プラットフォームで実行 Application Load Balancer Lambda Function Target Group Fargate Task Target Group EC2 Instances Target Group Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container
  8. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda Web Adapter https://github.com/awslabs/aws-lambda-web-adapter Lambda で Web アプリを実行するための OSS ツール
  9. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Example – express.js app を Lambda 上で実行 AWS Lambda Web Adapter をインストールするには1行追加するだけ install Lambda Web Adapter
  10. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 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 で Web アプリを実行するための OSS ツール aws-lambda-web-adapter
  11. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 実装のプラクティス
  12. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. NOTE ! このセッションは Lambda 上に WEB フレームワークを載せること を推奨するものではありません。
  13. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda 実装のプラクティス • Lambda 関数実装ではできるだけシンプルに作る § 既存の WEB フレームワーク を用いると – 前段の Amazon API Gateway で ANY method / proxy resource を使ったモノリスになりがち • 最小権限の原則の破壊 – 不要なコンポーネントが付随 • Artifact size の肥大で Cold Start Latency の増加 • Lambda 関数のインスタンスライフサイクルが短いため、起動時間にかけたコストに見合わない • フレームワークが持つ非機能コンポーネントを AWS にオフロード – 認証認可 : Amazon Cognito – Layer7攻撃対応: AWS WAF
  14. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 機能横断的要件(あるいは非機能要件) Spring Boot Actuator Spring Security FAT jar Log Authentication Layer7 Security Password Policy Metrics Trace
  15. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 機能横断的要件(あるいは非機能要件) Skinny jar AWS X-Ray AWS WAF AWS Lambda Amazon CloudWatch Amazon Cognito Amazon API Gateway Business Logic Log Authentication Layer7 Security Password Policy Metrics Trace
  16. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. YAGNI You aren't gonna need it. Martin Fowler 今の時点で必要ない余計な機能 (及び非機能) は付けるな
  17. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda Amazon CloudWatch Amazon API Gateway Business Logic YAGNI で始めやすい Log Metrics 今の時点で必要ない余計な機能 (及び非機能) は付けるな
  18. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda Powertools E-4: コードとデモで理解する!これは便利! AWS Lambdaの実装を加速するAWS Lambda Powertoolsを使いこなそう! https://awslabs.github.io/aws-lambda-powertools-python WEB フレームワークを使わない シンプルな Lambda でアプリレイヤー の構造化、非機能用件に対応するには? AWS Lambda Powertools • トレース • 構造化ログ • カスタムメトリクス • 冪等性 • バッチ処理 etc. ベストプラクティスの適用を容易にする Lambda 関数 ユーティリティスイート • Python • TypeScript • Java • .NET
  19. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda に WEBフレームワークを載せる (・・・ための挑戦の歴史)
  20. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. WEB フレームワーク Ruby on Rails PHP - Laravel Python - Flask Java – Spring Boot Go - Gin
  21. © 2022, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. これまでどんな方法が取られていたか • サーバーレス専用フレームワーク (模倣体験の提供) § AWS Chalice – AWS 製の Python フレームワーク、Flask に似た開発体験を得られる、独自 CLI が存在する § Kotless – JetBrains 製の Kotlin フレームワーク、Spring Bootの構文(spring-boot-lang) が利用できる § Ruby on Jets – BoltOps 製の Ruby フレームワーク、Rails に似た開発体験を得られる、独自 CLI が存在する • Runtime 埋め込み系 Adapter を利用する § 埋め込みの Adapter を言語ごとに提供し、実物のフレームワークを稼働 • Ahead-of-time Compilation による Native イメージ化 https://aws.github.io/chalice https://site.kotless.io https://rubyonjets.com
  23. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 様々な Runtime 埋め込み系 Adapter 1/2 • Python § Mangum – AWS Lambda で ASGI アプリケーションを実行し、Function URL、API Gateway、ALB、 Lambda@Edge イベントを処理するための Adapter § aws-wsgi (AWSGI) – AWSGI を使用すると、Flask や Django などの WSGI 互換のミドルウェアとフレームワーク を API Gateway / Lambda プロキシ統合で使用可能 • Node.js § Serverless Express – AWS Lambda 、API Gateway で、既存の Node.js フレームワーク (Express、Koa、Hapi、 Sails など) を使用して、Web アプリケーションを実行可能 https://mangum.io https://github.com/slank/awsgi https://github.com/vendia/serverless-express
  24. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 様々な Runtime 埋め込み系 Adapter 2/2 • Java § Serverless Java Container – Spring、Spring Boot、Apache Struts、Jersey、Spark などのフレームワークで作成された Java アプリケーションを AWS Lambda で実行可能 • PHP § bref – 他の PHP ホスティング プラットフォームと同様に、PHP アプリケーション ( Laravel や Symfony )を AWS Lambda で実行可能 • .NET Core § AspNetCoreServer – ASP.NET Core Web API を、Lambda 関数として実行可能 https://github.com/awslabs/aws-serverless-java-container https://bref.sh https://github.com/aws/aws-lambda- dotnet/tree/master/Libraries/src/Amazon.Lambda.AspNetCoreServer
  25. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda Invoke Invoke API Client Service API payload Lambda Service 実行環境 Runtime API Lambda Function Instance API payload def lambda_handler(event, context): # do something return { … } handler event object event
  26. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda Invoke def lambda_handler(event, context): # do something return { … } handler Invoke API Client Service 実行環境 Runtime API API payload Lambda Service Lambda Function Instance API payload event object event frame work GAP
  27. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Handler に Adapter を混入 / GAP の緩和 def lambda_handler(event, context): handler Invoke API Client Service 実行環境 Runtime API Lambda Service Lambda Function Instance API payload http object API payload http event frame work adapter
  28. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. WEB フレームワークを Lambda に導入 1. イベント ハンドラを実装 2. コードと依存関係をパッケージ 例) Python Flask アプリ adapter library Flask App event handler
  29. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Runtime 埋め込み系 Adapter の欠点 • 開発者が Adapter を意識 § Handler 内に Adapter が存在するため、 Artifact へのインストールが必要 § 開発者が開発するために、Adapter の理解が必要 § Adapter がサポートしている形式以外の完全なフレームワーク機能が使えない § 言語 / Runtime ごとに異なる Adapter を用意する必要がある • ポータビリティがない § Adapter が Artifact に内在し、コンテナサービスとのポータビリティはない
  30. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 埋め込み系 Adapter は、そのままECSで動くか? HTTPS / REST Client ALB API payload Container Service 実行環境 API payload Function Handler on Container def lambda_handler(event, context): handler http object http event frame work adapter インピーダンスミスマッチ GAP NO ! ポータビリティがない
  31. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Ahead-of-time Compilation (AOT) GraalVM は JVM ベースのコードを Native Platform で実行可能な Nativeファイルに変換する事前 (AOT) コンパイル 機能を持つ • Spring Native § Spring Native は、GraalVM を使用して Spring アプリケーションをネイティブ 実行可能ファイルにコンパイルするためのサポートを提供 • MICRONAUT § マイクロサービスおよびサーバーレス アプリケーションを構築するための JVM (AOT by GraalVM) フルスタックフレームワーク • QUARKUS § Java (AOT by GraalVM) を サーバーレスの主要な言語にすると同時に、開発者 に分散アーキテクチャに対応するためのフレームワークを提供 https://www.graalvm.org/22.3/reference-manual/java/compiler/#ahead-of-time-compilation https://docs.spring.io/spring-native/docs/current/reference/htmlsingle https://micronaut.io/ https://quarkus.io/
  32. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GraalVM x MICRONAUT を取り込む Source Build Test Deploy Lambda Life Cycle Warm Start Cold Start コンテナ 生成 パッケージ ロード パッケージ 展開 ランタイム 起動 初期化 関数・メソッド 起動 compile-time DI / AOP x MICRONAUT Lambda Life Cycle MICRONAUT は、 compile-time に、 reflection-free な DI と AOP を実現
  33. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AOT ベースフレームワークの欠点 • 使い慣れた WEB フレームワーク ではなく、AOT 向けカスタマイズ されたフレームワークである • GraalVM の知識、経験が多少なりとも必要 • 基本的に JVM User 向けのアプローチ Java の起動速度にご興味がある方はこちらもぜひ!
  34. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda の実行モデル
  35. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. NOTE ! このセッションは Lambda 上に WEB フレームワークを載せること を推奨するものではありません。 (大事なことなので二回言いました)
  36. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. “使い慣れた” 以外のメリットはあるのか? • Lambda の Concurrency (同時実行) の実行単位は完全に独立分離 § 複数リクエスト同時に単一の Lambda 関数リソースを共有利用することはない § セキュリティやプログラミングモデルの簡素化に加え、C10K への対抗策にも • Cold Start 時のリソース確保サイズの最適化 § Cold Start のメカニズムを理解して適切に Artifact を作ることで確保量を抑え システムとしての Latency を抑制 • マネージドなスケーリング § Auto scaling の仕組みを自分で組む必要がない • Pay as you go! § 使った分の課金 (1ms単位の実行時間課金)
  37. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda の実行モデルについて考えてみる https://qiita.com/_kensh/items/dc99e9a0238a1b4238a5
  38. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda の実行環境分離 Firecracker MicroVM MicroVM Kernel Lambda Sandbox Execution Environment KVM on Bare Metal EC2 FunctionB Code FunctionB /tmp Extension Code FunctionB Runtime Extension Runtime FunctionB vCPU FunctionB Memory Firecracker MicroVM MicroVM Kernel Lambda Sandbox Execution Environment FunctionA Code FunctionA /tmp Extension Code FunctionA Runtime Extension Runtime FunctionA vCPU FunctionA Memory Shared resources within execution environment Unshared resources within execution environment https://docs.aws.amazon.com/lambda/latest/ operatorguide/execution-environment.html Managed by AWS Lambda Service FunctionA Network FunctionB Network
  39. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Isolation / 独立環境 function instance f(x)=>{ } f(x) deployment g(x) request to f(x) handling cold start
  40. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Isolation / 独立環境 function instance f(x)=>{ } f(x) deployment g(x) response from f(x)
  41. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Isolation / 独立環境 function instance f(x)=>{ } f(x) deployment g(x) request to f(x) handling warm start request to f(x) function instance f(x)=>{ } handling cold start request to g(x) function instance g(x)=>{ } handling cold start リクエスト単位の isolation を持つ AWS Lambda の実行モデルは WEB フレームワーク の高機能な並列性やリクエストルーティングは、その真価を発揮できないことも
  42. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Cold Start における頻度の比較 Functions Containers Virtual Machines 小さな Cold Start を頻度高く 大きな Cold Start を頻度低く Cold Start は FaaS だけの問題ではなく、Utilizationを指標としたリソース逐次確保速度を超える スパイクアクセスが発生すると、そこには必ず Cold Start の可能性が出てくる
  43. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Cold Start におけるリソース確保の比較 スケールに必要なリソース 現状のリソース Functions Containers Virtual Machines
  44. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 本番環境の Lambda ワークロード分析 “本番環境の Lambda ワークロードの分析によると Cold Start は 呼び出しの 1% 未満で発生します。Cold Start の Latency は、100 ms 未満から 1s 以上まで様々です。 Lambda サービスは Warmup された実行環境 を後続の呼び出しに再利用するため、Cold Start は通常、本番環境のワークロードよりも開発およびテスト 機能でより見られます。これは、開発中の関数があまり高頻度に呼び出されないためです。” https://aws.amazon.com/jp/blogs/compute/operating-lambda-performance-optimization-part-1
  45. © 2022, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Tenets / フレームワークを載せるために 原則または信念、特に哲学的な主な原則の 1 つ
  47. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Tenets / WEB フレームワークを載せるために 1. Main Function の runtime type によらず利用可能であること 2. レイテンシーへの影響を最小限にすること 3. Lambda のイベント形式に対するアダプターであること 4. コンテナイメージは 他のサービスへのポータビリティを持つこと 5. コンテナイメージ / Zip ともに利用可能であること
  48. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Tenets / WEB フレームワークを載せるために 1. Main Function の runtime type によらず利用可能であること § Ruby, Python, Java, PHP,…のどの runtime でも利用可能 2. レイテンシーへの影響を最小限にすること 3. Lambda のイベント形式に対するアダプターであること § Event → HTTP request への変換 4. コンテナイメージは 他のサービスへのポータビリティを持つこと § Amazon ECS など他のコンテナサービスへのポータビリティを持つ 5. コンテナイメージ / Zip ともに利用可能であること
  49. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Tenets / WEB フレームワークを載せるために 実装を Rust 言語にすることで実現できそうなこと § Main Function の runtime type によらず利用可能であること § レイテンシーへの影響を最小限にすること サイドカーパターンで実現できそうなこと § Lambda のイベント形式に対するアダプターであること § コンテナイメージは 他のサービスへのポータビリティを持つこと § コンテナイメージ / Zip ともに利用可能であること
  50. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda でのサイドカーパターン
  51. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Design patterns for container-based distributed systems https://research.google/pubs/pub45406.pdf The 8th Usenix Workshop on Hot Topics in Cloud Computing (HotCloud '16) (2016) で 発表された論文 コンテナを使った分散システムのデザイン パターンを述べている その中で、サイドカーパターンについても 解説されている
  52. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda でサイドカーパターンは実現可能なの ? https://aws.amazon.com/jp/builders-flash/202202/new-lambda-container-development-7
  53. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. サイドカーパターン でロギング main Side Car Log サイドカーは非機能要件を扱うのに 適しているという説明によく登場す るのが、アプリケーションログ用の サイドカーの利用 HTTP request Client Execution Environment Observability SaaS / S3 など
  54. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. サイドカーパターン で reverse proxy HTTP request Client proxy main Side Car Execution Environment GAP サイドカーで HTTP Proxy を配備 することを考える main で受付たい形式とギャップ がある場合どうするか? e.g. 通信プロトコルの差異 / payload形式の差異 etc.
  55. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. サイドカーパターン で reverse adapter proxy HTTP request Client proxy main 形式2 形式1 adapter Side Car Execution Environment 単純な HTTP Proxy ではなく、形 式を main の入力形式に変換する ような adapter 機能も必要となる
  56. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda でのサイドカーの実現方法 Lambda Service API Endpoints Processes External Extensions Runtime + Function Logs Extension Extension API Runtime API Logs API Execution Environment Lambda Extension を使用して、Lambda 関数を拡張可能 • たとえば、好みのオブザーバビリティ、セキュリティ、ガバナンス ツールと統合 • AWS Lambda パートナー提供 Extension から選択するか、独自の Extension を作成 https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html
  57. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda Invoke Invoke API Client Service Lambda Service Extension GET event Lambda Function Instance event Extension はサイドカーとして、proxy 兼、 event 形式から HTTP request への変換を行う http adapter main web framework HTTP request
  58. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Web Adapter について
  59. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda Web Adapter https://github.com/awslabs/aws-lambda-web-adapter public.ecr.aws/awsguru/lambda-http-ric コードを変更せずに、Lambda と ECS の両方で Web アプリ コンテナを実行 AWS Lambda 用の Rust Extensionで構築
  60. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Initialize Execution Environment Runtime API aws-lambda-web-adapter OS Packages Runtime Interface Client Web App http- adapter Readiness check the web application spawn
  61. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Invoke - request Execution Environment Runtime API aws-lambda-web-adapter OS Packages Runtime Interface Client Web App http- adapter Get next event from Runtime API Invoke adapter Send http request Incoming Invoke
  62. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Invoke - response Execution Environment Runtime API aws-lambda-web-adapter OS Packages Runtime Interface Client Web App http- adapter Send result to runtime API Convert http response Receive http response Send response
  63. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Example – express.js app を Lambda 上で実行 Dockerfile で aws-lambda-web-adapter を使用するための 3 つの手順: 1. aws-lambda-web-adapter を ECR パブリック レジストリからコピー 2. Docker コンテナの ENTRYPOINT として使用 3. CMD を使用して Web アプリケーションを実行 (デフォルト ポート 8080) Step 1: adapterをコピー Step 2: ENTRYPOINTとして使用 Step 3: CMD を使用して実行
  64. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 4つの HTTP エンドポイントをサポート 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 * HTTP API v1 イベント ペイロード形式はサポートされていません Lambda 固有のイベントを標準の HTTP リクエストに変換 aws-lambda-web-adapter Function URL
  65. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イメージを変更せずに ECS で実行可能か? Application Load Balancer ENI Fargate Task Container OS Packages aws-lambda-web-adapter Web App health check spawn HTTP Requests YES ! ポータブル
  66. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージを 異種プラットフォームで実行 Application Load Balancer Lambda Function Target Group Fargate Task Target Group EC2 Instances Target Group Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container Web App Container
  67. © 2022, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Key takeaways
  69. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Key takeaways • Lambda 関数実装は WEB フレームワークを使わなくても良い粒度で シンプルに構築する § 最小権限の原則を遵守 § モノリス / FAT な実装にならないように § AWS Lambda Powertools がシンプル実装に対する非機能要件に対応 • それでも WEB フレームワークを利用したい場合は § Lambda のスケールや実行モデルを理解する § Lambda Extension が登場し、メイン関数実装に関与することなくサイドカー パターンによる Proxy / Adapter が可能になった – その実装の一つが、AWS Lambda Web Adapter
  70. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Thank you! © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Kensuke Shimokawa Slides https://speakerdeck.com/_kensh/ Qiita https://qiita.com/_kensh
  71. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Please complete the session survey © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  72. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Appendix
  73. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda の Cold Start 初期化処理 Cold Start Warm Start ハンドラー処理 ② ① ③
  74. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Little’s Law (リトルの法則) Concurrency = rps x duration (同時実⾏数) (リクエストレート) (実⾏時間)
  75. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda の Concurrency (同時実行) Client Server Concurrency = 1 Concurrency = 0
  76. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Concurrency 増加 : リクエストレート ( rps ) 増加 Client Server Concurrency = 3
  77. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Concurrency 増加 : 実行時間 ( duration ) 増加 Client Server Concurrency = 3
  78. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. AWS Lambda のスケール Concurrency time アカウント同時実行数 制限緩和 1500 (Tokyo) 1000 1 request で 1 token 消費 Burst でスケール 500/分で Burst Quota 回復 Token Bucket algorithm AWS Lambda はビルドインでスケールの仕組みを提供 1000 Burst Quota
  79. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda 実行環境 Execution Environment Runtime API Lambda Service Language Runtime OS Packages Runtime Interface Client Initialization & Handler Code Invoke handler Get next event from Runtime API
  80. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Zip / Jar File Execution Environment Runtime API Lambda Service Language Runtime OS Packages Runtime Interface Client You provide AWS provides You deploy Initialization & Handler Code Zip / Jar File
  81. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージ (AWS Lambda Base Image) Execution Environment Runtime API Lambda Service Language Runtime OS Packages Runtime Interface Client You provide AWS provides You deploy Initialization & Handler Code Container Image
  82. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージ (Custom Base Image) Execution Environment Runtime API Lambda Service Language Runtime OS Packages Runtime Interface Client You provide AWS provides You deploy Initialization & Handler Code Container Image
  83. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージ (Custom Base Image & RIC) Execution Environment Runtime API Lambda Service Language Runtime OS Packages Runtime Interface Client You provide AWS provides You deploy Initialization & Handler Code Container Image
  84. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda 内で Proxy サーバーを実行? lambda-http-adapter を使うと可能 Execution Environment Runtime API Lambda Service Lambda-http- adapter OS Packages You provide AWS provides You deploy Web App Container Image
  85. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda のコンテナイメージサポート • コードをコンテナイメージとしてパッ ケージ化し、Lambda にデプロイ可能に • 画像サイズ 10GBまで • イベント駆動型プログラミング モデル
  86. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Understanding the Lambda execution environment Firecracker MicroVM MicroVM Kernel Lambda Sandbox Execution Environment KVM on Bare Metal EC2 FunctionB Code FunctionB /tmp Extension Code FunctionB Runtime Extension Runtime FunctionB vCPU FunctionB Memory Firecracker MicroVM MicroVM Kernel Lambda Sandbox Execution Environment FunctionA Code FunctionA /tmp Extension Code FunctionA Runtime Extension Runtime FunctionA vCPU FunctionA Memory Shared resources within execution environment Unshared resources within execution environment https://docs.aws.amazon.com/lambda/latest/ operatorguide/execution-environment.html Managed by AWS Lambda Service FunctionA Network FunctionB Network
  87. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Lambda External Extensions An external extension runs as an independent process in the execution environment and continues to run after the function invocation is fully processed.
  88. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon EC2 AWS Fargate AWS Lambda AWS は 多くのコンピューティング プラットフォームを提供 まずは、Lambda から始めるのがお勧め AWS での Compute 選択どうすればよいですか?
  89. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 使い慣れたプログラミング モデルとツール 高速な開発体験 AWS Lambda Amazon ECS / Fargate Amazon EC2 間の移植性 なぜフレームワークを Lambda で使いたいのか?
  90. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 需要に応じた スケール 瞬時にバースト 耐障害性 高可用性 デフォルトで マルチ AZ 管理する サーバーはなし ホストのプロビ ジョニングや管理 は不要 パッチを適用する OS パッケージや ランタイムはなし セキュリティ上の 懸念の最小化 ネットワークアド レスを意識しない (IAM で保護された 呼び出し) 関数単位の マイクロ VM 実行環境 利用した分の 支払い ミリ秒単位の課金 AWSサービスとの 高レベルのインテ グレーション ストリームと キューのマネージ ドポーリング AWS Lambda の良さを振り返る
  91. © 2022, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 1. アプリケーションを Lambda 関数としてパッケージ化 2. Amazon API Gateway を介して HTTP エンドポイントを作成 3. RESTリクエストに応じてアプリケーションをトリガー Lambda function Amazon API Gateway Amazon RDS Lambda で Web アプリケーションを実行?