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

AWS App Runner を使う時に押さえておきたい勘所 〜 Web アプリをコンテナに最適化する 〜

はまーん
December 09, 2023

AWS App Runner を使う時に押さえておきたい勘所 〜 Web アプリをコンテナに最適化する 〜

AWS Innovate - Modern Applications Edition 2023/10/26
https://aws.amazon.com/jp/events/aws-innovate/apj/modern-apps/
「AWS App Runner を使う時に押さえておきたい勘所 〜 Web アプリをコンテナに最適化する 〜」
---
皆さんのお手元にある Ruby on Rails や Laravel といった Web アプリのコンテナイメージやソースコードを、サクッと AWS App Runner で公開しましょう!App Runner は、インフラや AWS に詳しくない方でも「手軽に」「長く」使える Web アプリ向けのホスティングサービスです。本セッションでは、App Runner で Web アプリを実行する際に、次のステップのヒントとなるプラクティスをご紹介します。

はまーん

December 09, 2023
Tweet

More Decks by はまーん

Other Decks in Technology

Transcript

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

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 濵 真⼀(はまーん)/ :track3jyo アマゾン ウェブ サービス ジャパン合同会社 シニアソリューションアーキテクト T 3 - 4 AWS App Runner を使う時に 押さえておきたい勘所 〜 Web アプリをコンテナに最適化する 〜
  2. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Shinichi Hama (@track3jyo) Sr. Solutions Architect, Startup Amazon Web Services Japan --- • ⼤阪オフィス勤務(ほぼ家にいますが) • スタートアップ企業の⽀援 • 過去のスライド: https://speakerdeck.com/track3jyo • 最近の趣味はたい焼き作り
  3. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションのゴール • お⼿元の Web アプリのコードやコンテナイメージを AWS App Runner 上で動かすために最適化できるようになる
  4. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. • App Runner がもたらすメリット • App Runner プラクティスガイド • ストレージ • SIGTERM • ロギング • 秘密情報 • 分散トレーシング アジェンダ
  5. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS は幅広い選択肢を提供
  6. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS は幅広い選択肢を提供 200 以上のサービス
  7. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ブロックのようにサービスを組み合わせて アプリケーション構築が可能
  8. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS サービスを組み合わせてインフラを構築 柔軟性が⾼い Amazon Virtual Private Cloud (Amazon VPC) [ネットワーク] AWS Fargate [コンピュート] Amazon Elastic Container Service (Amazon ECS) [オーケストレーション] Application Load Balancer (ALB) [ロードバランサー] AWS Auto Scaling [オートスケール] AWS CodeBuild [CI/CD] ⼿間がかかる
  9. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 構築済みのインフラにすぐにデプロイ AWS App Runner
  10. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner の代表的ユースケース 1. モノリスな Web アプリ AWS App Runner Amazon Aurora
  11. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner の代表的ユースケース 2. モバイルアプリやシングルページアプリケーションの バックエンド API AWS App Runner Amazon DynamoDB データを保存 ログイン ⼀覧表⽰
  12. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner の代表的ユースケース 3. 社内向け Web サイトや API お客様の VPC AWS App Runner 社内ネットワーク AWS Direct Connect VPC Endpoint (プライベートなエンドポイント) VPC Endpoint からのみアクセス可能
  13. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner がもたらすメリット • 市場投⼊までの時間を短縮 • 簡単かつ迅速に Web アプリを公開 • アプリケーション開発に集中 • アプリケーションの 実⾏時間に対して課⾦
  14. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner がもたらすメリット • 市場投⼊までの時間を短縮 • 簡単かつ迅速に Web アプリを公開 • アプリケーション開発に集中 • アプリケーションの 実⾏時間に対して課⾦
  15. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 簡単かつ迅速に Web アプリを公開可能 サンプル Web アプリを App Runner 上にデプロイして、 インターネットからアクセス可能になるまでの流れを デモしました
  16. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 簡単かつ迅速に Web アプリを公開可能 既に App Runner 上にデプロイ済みの Web アプリを 更新するときの流れをデモしました
  17. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS App Runner GitHub Repo GitHub ソースコードベースのサービス
  18. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. コンテナイメージベースのサービス AWS App Runner Amazon Elastic Container Registry (Amazon ECR) Image Repo
  19. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. コンテナイメージベースのサービス AWS App Runner コンテナイメージを取得 Amazon Elastic Container Registry (Amazon ECR) Image Repo
  20. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. コンテナイメージベースのサービス AWS App Runner コンテナインスタンス ロードバランサー AWS App Runner コンテナイメージをデプロイ Amazon Elastic Container Registry (Amazon ECR) Image Repo
  21. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner プラクティスガイド
  22. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ストレージ 消えては困るデータは DB やオブジェクトストレージなどの外部ストレージへ
  23. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner のストレージ • コンテナインスタンス終了後はアクセスできない エフェメラルなストレージ • 書き込み⾃体は可能 • 1 コンテナインスタンスあたり イメージサイズを含めて 3GB まで • 各ストレージは コンテナインスタンスに属し 他のコンテナインスタンスからは 参照できない コンテナ インスタンス AWS App Runner エフェメラル ストレージ 3GB 3GB
  24. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 消えては困るデータは外部ストレージへ • アプリケーションはステートレスな作りにする コンテナインスタンス ロードバランサー AWS App Runner Amazon Aurora Amazon Simple Storage Service (Amazon S3) ステートは外部ストレージに格納
  25. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. S3 や DynamoDB への接続⽅法 • インスタンスロール (IAM Role) でアクセスを制御する コンテナインスタンス ロードバランサー サービス A Amazon S3 コンテナインスタンス ロードバランサー サービス B インスタンスロール S3 バケットへの アクセスを許可
  26. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. VPC 内にあるサービスへの接続⽅法 • VPC コネクターを設定し、⾃⾝の VPC に接続する コンテナインスタンス ロードバランサー AWS App Runner お客様の VPC Private subnet Amazon Aurora ENI VPC コネクター プライベート接続
  27. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. SIGTERM SIGTERM をきちんとハンドリングできる アプリケーションを実装する
  28. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. コンテナインスタンスの停⽌指⽰ • App Runner は内部的にコンテナ内のアプリケーションに対して SIGTERM を送信 • e.g. オートスケーリングのスケールインの時 • その後タイムアウトののちに SIGKILL が送信される • SIGTERM でシャットダウンできないアプリケーションは強制終了される • 処理中のデータをロストするような不具合が発⽣しうる ①SIGTERM 送信 ②SIGKILL 送信 猶予期間 (この間に終了処理をおこなう)
  29. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. SIGTERM をきちんとハンドリングできる アプリケーションを実装する • SIGTERM を受け取ったら 終了処理をおこなう実装を追加する • 処理中のデータの取り扱いまで含めて 設計で考慮する • Web アプリのフレームワークなどで ⼀定程度終了処理が実装済みなことも process.on('SIGTERM', () => { console.log('Caught SIGTERM, shutting down'); // TODO: ここで何か処理する process.exit(0); }); Node.js での例
  30. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 秘密情報 秘密情報の受け渡しには、AWS Secrets Manager を使う
  31. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. コンテナのインスタンスの中に秘密情報を受け渡す⽅法 • コード内や App Runner の環境変数に秘密情報を直書きするのは 秘密情報の保護の観点でアンチパターン コンテナインスタンス AWS App Runner Amazon Aurora ... snip ... run: runtime-version: 3.7.7 env: - name: "DB_USER" value: "myappdbuser" - name: "DB_PASSWD" value: "mysupersecretpasswd" - name: "DB_HOST" value: "my-db...rds.amazonaws.com" - name: "DB_PORT" value: "3306" ... snip ...
  32. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 秘密情報の受け渡しには AWS Secrets Manager を使う コンテナインスタンス AWS App Runner Amazon Aurora AWS Secrets Manager • Secrets Managerとの統合機能を使って、透過的に秘密情報をコンテ ナインスタンスに配布する ※シークレットを更新した場合は再デプロイが必要です ... snip ... run: runtime-version: 3.7.7 secrets: - name: "DB_USER" value-from: "arn:aws:secretsmanager:...username::" - name: "DB_PASSWD" value-from: "arn:aws:secretsmanager:...password::" env: - name: "DB_HOST" value: "my-db...rds.amazonaws.com" - name: "DB_PORT" value: "3306" ... snip ...
  33. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ロギング ログはファイルではなく STDOUT/STDERR へ
  34. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ログはファイルではなく STDOUT/STDERR へ • ファイルに吐き出したログを確実に取り出すことは難しい • ログは標準出⼒ (STDOUT)・標準エラー出⼒ (STDERR) に出⼒する Twelve-Factor App はアプリケーションの出⼒ストリームの送り先やストレージについて ⼀切関知しない。 アプリケーションはログファイルに書き込んだり管理しようとするべきではない。 代わりに、それぞれの実⾏中のプロセスはイベントストリームを stdout(標準出⼒)に バッファリングせずに書きだす。 “ ” The Twelve-Factor App, ⅺ. Log ( https://12factor.net/ja/logs )
  35. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner のログ収集 ロードバランサー AWS App Runner 開発者 App Runner コンソール コンテナインスタンス STDOUT/STDERR のログを⾃動で収集
  36. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner のログ収集 ロードバランサー AWS App Runner 開発者 App Runner コンソール コンテナインスタンス App Runner コンソールから確認
  37. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner のログモニタリング
  38. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 分散トレーシング AWS X-Ray でトレーサビリティを確保する
  39. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS App Runner Amazon S3 クライアント Amazon Aurora AWS Lambda Amazon SQS
  40. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. あのとき、どこで、何が起きていた︖ AWS App Runner Amazon S3 クライアント Req. POST /something Resp. 3.0 sec. Amazon Aurora AWS Lambda Amazon SQS
  41. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. あのとき、どこで、何が起きていた︖ AWS App Runner Amazon S3 クライアント Req. POST /something Resp. 3.0 sec. Amazon Aurora AWS Lambda Amazon SQS
  42. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. あのとき、どこで、何が起きていた︖ AWS App Runner Amazon S3 クライアント Req. POST /something Resp. 3.0 sec. . . [09/07:14:16:18] ”POST /something" 201 2950ms . . . - CPU Util. 91% - Mem Util. 55% . . Amazon Aurora AWS Lambda Amazon SQS
  43. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. あのとき、どこで、何が起きていた︖ AWS App Runner Amazon S3 クライアント Req. POST /something Resp. 3.0 sec. ① ② ③ ④ ログとメトリクスだけでは 問題箇所の特定が困難 Amazon Aurora AWS Lambda Amazon SQS
  44. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. システムの分散を前提とした可視化の必要性 総処理時間 最も時間を要した処理
  45. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 分散トレーシングとは 分散トレーシングとは、分散されたシステム間でやり取りされる 処理をトレース(=追跡)するための考え⽅やそれを実現する仕組み • マイクロサービスに限った話ではない • アプリケーションで発⽣したエラーや障害の原因特定 • パフォーマンス問題の原因特定
  46. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS X-Ray による分散トレーシング AWS X-Ray はアプリケーションの トレースの分析・調査のための 分散トレーシングサービス • App Runner は X-Ray と統合可能 • App Runner 側はワンクリックで有効化 • アプリケーション側は OpenTelemetry で計装
  47. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS X-Ray による分散トレーシング
  48. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS X-Ray による分散トレーシング
  49. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS X-Ray による分散トレーシング
  50. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ • App Runner の特徴を知る § インフラや AWS についての経験がなくても、コンテナ化された Web アプリを 簡単かつ迅速に AWS 上で動かせるサービス • App Runner のプラクティスから学ぶ § App Runner で Web アプリを実⾏する際のポイントを知り、最適化に役⽴てる
  51. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. App Runner の公開ロードマップ https://github.com/aws/apprunner-roadmap
  52. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 次の⼀歩 – ワークショップにチャレンジ - AWS App Runner Workshop https://catalog.us-east- 1.prod.workshops.aws/works hops/e801f943-aad8-4d9e- aa8a-3bcc2913340f/ja-JP/
  53. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 参考リンク • P24 / App Runner のストレージの考慮事項 • https://docs.aws.amazon.com/apprunner/latest/dg/develop.html#develop.considerations • P26 / App Runenr のインスタンスロールについて • https://docs.aws.amazon.com/apprunner/latest/dg/security_iam_service-with-iam.html#security_iam_service-with-iam-roles • P27 / App Runner VPC コネクターについて • https://docs.aws.amazon.com/apprunner/latest/dg/network-vpc.html • P33 / App Runner の Secrets Manager 統合について • https://aws.amazon.com/jp/blogs/containers/aws-app-runner-now-integrates-with-aws-secrets-manager-and-aws-systems- manager-parameter-store/ • P36 / App Runner でのログモニタリングについて • https://docs.aws.amazon.com/apprunner/latest/dg/monitor-cwl.html • P47 / App Runner における分散トレーシングについて • https://docs.aws.amazon.com/apprunner/latest/dg/monitor-xray.html
  54. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ご清聴ありがとうございました 濵 真⼀(はまーん) :track3jyo