Slide 1

Slide 1 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda@Edge - JAWS-UG ⼤阪 第 21 回勉強会 - アマゾン ウェブ サービス ジャパン 株式会社 ソリューション アーキテクト 藤原 吉規 2017.08.09

Slide 2

Slide 2 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介 藤原 吉規(ふじわら よしのり) ⻄⽇本担当 ソリューション アーキテクト • AWS ⼤阪オフィスにいます • 関⻄のスタートアップ企業で 6 年間 AWS を活⽤ • AWS サムライ 2013 • 好きな AWS サービス: AWS サポート

Slide 3

Slide 3 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 4

Slide 4 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. https://aws.amazon.com/jp/blogs/news/lambdaedge-intelligent-processing-of-http-requests-at-the-edge/ Lambda@Edge の⼀般提供を 2017 年 7 ⽉に 開始

Slide 5

Slide 5 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Agenda • AWS Lambda@Edge 概要 • AWS Lambda@Edge サービスの仕組み • AWS Lambda@Edge デモ • AWS Lambda@Edge まとめ

Slide 6

Slide 6 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Agenda • AWS Lambda@Edge 概要 • AWS Lambda@Edge サービスの仕組み • AWS Lambda@Edge デモ • AWS Lambda@Edge まとめ

Slide 7

Slide 7 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge の利点 グローバル エンドユーザーに近い AWS エッ ジロケーションからコードを実⾏ 継続的スケーリング 各トリガーに応答してコードを並 列で実⾏することにより、アプリ ケーションがワークロードのサイ ズに合わせて正確にスケール オリジンの負荷を軽減 サーバーのプロビジョニングや管 理は不要、 Node.js コードを記述 して AWS Lambda にアップロー ドするだけで、エンドユーザーに 近い AWS エッジロケーションか ら⾃動的にコードを実⾏ ミリ秒単位の課⾦ コードの実⾏時間 50 ミリ秒ごとに、 トリガーされた回数に応じて課⾦ https://aws.amazon.com/jp/lambda/edge/

Slide 8

Slide 8 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge のユースケース • ⾼度にパーソナライズされたウェブサイト • ビューワーリクエストに応じたレスポンス⽣成 • URL の書き換え • エッジロケーションでのアクセスコントロール • リモートネットワークの呼び出し • A/B テスト

Slide 9

Slide 9 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Agenda • AWS Lambda@Edge 概要 • AWS Lambda@Edge サービスの仕組み • AWS Lambda@Edge デモ • AWS Lambda@Edge まとめ

Slide 10

Slide 10 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. 対象となる CloudFront イベント http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

Slide 11

Slide 11 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. 対象となる CloudFront イベント • リクエストが最初に閲覧者から届いたとき (Viewer request) • Lambda 関数でオリジンへのリクエストを転送するとき (Origin request) • Lambda 関数でオリジンからの応答を受けるとき (Origin response) • Lambda 関数が閲覧者に応答する前 (Viewer response) ※Origin response が HTTP 400 以上の場合はトリガーされない http://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/eligible-cloudfront-events.html

Slide 12

Slide 12 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge ⽤ Lambda Function の作成 http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html#lambda-edge-how-it-works AWS Region US-East-1 Author at Lambda console Master Lambda function CloudFront distribution trigger AWS Region Lambda function replica AWS Region Lambda function replica CREATE ASSOCIATE REPLICATE REPLICATE CloudWatch CloudWatch ・ ・ ・

Slide 13

Slide 13 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge 実⾏環境と制限 http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html Origin facing Viewer facing ランタイム Node.js 6.10 ← メモリ 128 MB ← 最大実行時間 3 seconds 1 second デプロイパッケージサイズ (zip/jar圧縮前) 1 MB ← ネットワーク呼び出し Yes No レスポンスサイズ (request events) 256 KB 40 KB 同時実行数 (Region毎) 1,000 ← /tmp, 環境変数, DLQ, VPC, X-Ray 使⽤不可 ←

Slide 14

Slide 14 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge イベントの構造/制限 Request Event distributionId read-only uri read / write method (HTTP method) read-only clientIp read-only headers read / write Response Event distributionId read-only status read-only statusDescription read-only headers read / write http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-header-restrictions.html

Slide 15

Slide 15 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. HTTP レスポンスの⽣成 制限 Origin request events Viewer request events status required ← headers ホワイトリスト / ブラックリスト ← body 256 KB 40 KB http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses.html http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples- static-web-server • ユースケース例 • HTTPリダイレクト • 静的Webサーバー

Slide 16

Slide 16 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. CloudWatch Logs へのログ記録 • console.log() • console.error() • console.warn() • console.info() { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com” }, "Action": "sts:AssumeRole” }, { "Effect": "Allow", "Principal": { "Service": "edgelambda.amazonaws.com” }, "Action": "sts:AssumeRole” } ] } Lambda⽤ロールへの信頼関係を追加 http://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/logging.html

Slide 17

Slide 17 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Agenda • AWS Lambda@Edge 概要 • AWS Lambda@Edge サービスの仕組み • AWS Lambda@Edge デモ • AWS Lambda@Edge まとめ

Slide 18

Slide 18 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda@Edge デモ

Slide 19

Slide 19 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Agenda • AWS Lambda@Edge 概要 • AWS Lambda@Edge サービスの仕組み • AWS Lambda@Edge デモ • AWS Lambda@Edge まとめ

Slide 20

Slide 20 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge の料⾦ 1. リクエスト • リクエスト 100 万件あたり 0.60 USD (リクエスト 1 件あたり 0.0000006 USD) 2. 時間 • 128 MB-秒の使⽤につき 0.00000625125 USD (Lambda@Edge 関数でコード実⾏ 1 件あたりに利⽤でき るメモリは 128 MB) • Lambda@Edge 関数は 50 ミリ秒単位で計測 https://aws.amazon.com/jp/lambda/pricing/#edge

Slide 21

Slide 21 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda@Edge まとめ • CloudFront イベントに応じて AWS エッジロケーショ ンで Lambda 関数を実⾏ • パーソナライズ、レスポンス⽣成、URL書き換え、アク セスコントロール、リモートネットワーク呼び出し、 A/Bテスト等、多様なユースケース • 従量課⾦で使った分だけ⽀払い

Slide 22

Slide 22 text

© 2017, Amazon Web Services, Inc. or its affiliates. All rights reserved.