Slide 1

Slide 1 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 2020/11/3 Amazon Web Services Japan K.K. Solutions Architect Yoshinori Fujiwara AWS Lambda のちょっと便利な ツールのご紹介 サーバーレスしてますか! @twingo_b 〜 合同勉強会 in 大都会岡山 - 2020 Winter Online - 〜

Slide 2

Slide 2 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート

Slide 3

Slide 3 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート

Slide 4

Slide 4 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート

Slide 5

Slide 5 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート Amazon API Gateway を使用して HTTP REST API を提供

Slide 6

Slide 6 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート AWS Lambda は役割 および HTTP Method 毎に関数を分離

Slide 7

Slide 7 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート バックエンドにも (外部) HTTP API が 存在

Slide 8

Slide 8 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート Amazon SQS キュー を使った非同期処理

Slide 9

Slide 9 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート Amazon DynamoDB と DynamoDB Streams に よるストリーム処理

Slide 10

Slide 10 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート • Amazon API Gateway を使用して HTTP REST API を提供 • バックエンドにも (外部) HTTP API が存在 • AWS Lambda は役割および HTTP Method 毎に関数を分離 • Amazon SQS キューを使った非同期処理 • Amazon DynamoDB と DynamoDB Streams によるストリーム処理

Slide 11

Slide 11 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスアプリケーションの よくある運用の悩み

Slide 12

Slide 12 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート 各 Lambda 関数のエラ ーをどうやって監視し たら良いの?

Slide 13

Slide 13 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート バックエンドの HTTP API はどの Lambda 関数から 呼ばれているの?

Slide 14

Slide 14 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート ボトルネックはどこにあ るの?

Slide 15

Slide 15 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスショッピングカート 非同期処理やストリー ミング処理は正常稼働 しているの?

Slide 16

Slide 16 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレスアプリケーションのよくある運用の悩み • 各 Lambda 関数のエラーをどうやって監視したら良いの? • バックエンドの HTTP API はどの Lambda 関数から呼ばれているの? • ボトルネックはどこにあるの? • 非同期処理やストリーミング処理は正常稼働しているの?

Slide 17

Slide 17 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. サーバーレス は分散アーキテクチャ 静的コンテンツ 動的データ 記事/試合状況 フォロー状況管理 認可 (フェデレーション) ログの保存 ETL処理 ログ収集 データ分析 クローリング 記事に対する処理 (タグ付けなど) 変更通知 画像に対する処理 (顔座標の検出) Amazon CloudWatch AWS X-Ray 静的コンテンツ 動的データ 記事/試合状況 フォロー状況管理 ログの保存 ETL処理 ログ収集 データ分析 クローリング 記事に対する処理 (タグ付けなど) 変更通知 画像に対する処理 (顔座標の検出) 静的コンテンツ 動的データ 記事/試合状況 フォロー状況管理 ログの保存 ETL処理 ログ収集 データ分析 クローリング 記事に対する処理 (タグ付けなど) 変更通知 画像に対する処理 (顔座標の検出) 静的コンテンツ 動的データ 記事/試合状況 クローリング 記事に対する処理 (タグ付けなど) 変更通知 画像に対する処理 (顔座標の検出)

Slide 18

Slide 18 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools

Slide 19

Slide 19 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools https://awslabs.github.io/aws-lambda-powertools-python/

Slide 20

Slide 20 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools ログ メトリ クス トレース Amazon CloudWatch AWS X-Ray

Slide 21

Slide 21 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools 生成 CloudWatch Logs Insights

Slide 22

Slide 22 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. CloudWatch Logs Insights (Cold Start)

Slide 23

Slide 23 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools 生成 CloudWatch サービスマップ

Slide 24

Slide 24 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools 生成 CloudWatch トレース

Slide 25

Slide 25 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools 生成 CloudWatch カスタムメトリクス ビジネスメトリクスの可視化に利用可能

Slide 26

Slide 26 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools 対応言語 aws-samples / aws-lambda-powertools-python aws-samples / aws-lambda-powertools-java

Slide 27

Slide 27 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ロギング

Slide 28

Slide 28 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 構造化ログ (Structured Logging) を推奨 [INFO] Raw Semi-structured Structured [INFO] ... {“message”: } {“level”: “INFO”, “message”: } Canonical [timestamp] meta=data timestamp | operation | key=value |… Sample Type

Slide 29

Slide 29 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これまでの Lambda ログ [INFO] 2020-01-31T22:.. 1c8df7d3… Hello world Result [課題] 構造化されていないので検索性が よくない

Slide 30

Slide 30 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 自作の半構造化ログ [INFO] … {“message”: “Hello world”} Result [課題] 構造化オブジェクトを自作する手間 Log Level も構造化したい

Slide 31

Slide 31 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 自作の構造化ログ { "asctime": "2020-07-12...", "levelname": "INFO", "name": "root", "message": "Aha!" } Result [課題] 構造化されたが、コードの 可読性が損なわれる

Slide 32

Slide 32 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools の構造化ログ { "timestamp": "2020-07-14...", "level": "INFO", "location": "collect.handler:5", "service": "payment", "sampling_rate": 0.5, "message": ”Aha!” } Result [解消] 構造化されており、記述も楽!

Slide 33

Slide 33 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools の構造化ログ { … "lambda_function_name":.., "lambda_function_memory_size": 128, "lambda_function_arn": ”…", "lambda_request_id": "52fd..", "message": ”Aha", "cold_start": true } Result [解消] デコレータで関数Context情報も 出力可能

Slide 34

Slide 34 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. トレーシング

Slide 35

Slide 35 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. トレーシング の考え方 トレーシング セグメント サブセグメント API Gateway Lambda Fargate DynamoDB API Gateway S3 bucket Type Key Value Namespace Annotation UserId a1c8k... Metadata Cart {….} Checkout 凡例 Trace metadata AWS X-Ray

Slide 36

Slide 36 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS SAM で Lambda のトレーシングを有効化

Slide 37

Slide 37 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これまでのトレーシング / サービスマップ [課題] パッチを当てる手間がある

Slide 38

Slide 38 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これまでのトレーシング / サービスマップ [課題] Subsegment を個別に宣言する必要あり 冗長な記述

Slide 39

Slide 39 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools のトレーシング / サービスマップ [解消] デコレータでサブセグメントも定義

Slide 40

Slide 40 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. メトリクス

Slide 41

Slide 41 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. メトリクス の概念

Slide 42

Slide 42 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これまでのメトリクス出力 Synchronous [課題] Metric のフォーマットに 従うのが大変 冗長! 同期出力…

Slide 43

Slide 43 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. これまでのメトリクス出力 Synchronous [課題感] Metricのフォーマットに 従うのが大変 複数出力はさらに冗長! 同期出力…

Slide 44

Slide 44 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools メトリクス出力 Asynchronous [解消] Metric のフォーマット を意識しない 非同期出力!

Slide 45

Slide 45 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools メトリクス出力 Asynchronous [解消] Metric のフォーマット 複数出力でも意識しない 非同期出力!

Slide 46

Slide 46 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools メトリクス出力 Asynchronous Result Embedded Metric Format を使用している

Slide 47

Slide 47 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools はオールインワン Environment variable Value POWERTOOLS_SERVICE_NAME shopping-cart POWERTOOLS_METRICS_NAMESPACE e-commerce-app LOG_LEVEL INFO 環境変数で名前空間を管理

Slide 48

Slide 48 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ユーティリティ

Slide 49

Slide 49 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools

Slide 50

Slide 50 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Systems Manager Parameter Store, AWS Secrets Manager, Amazon DynamoDB • 指定された時間(デフォルト は5秒)のパラメーター値を キャッシュ • JSONまたはbase64でエンコ ードされた文字列からパラメ ータ値を変換

Slide 51

Slide 51 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SQS バッチプロセス • 正常処理メッセージがSQSに返却されるのを防止 • バッチメッセージを個別に処理するためのシンプル なインターフェイス

Slide 52

Slide 52 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Slide 53

Slide 53 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Powertools を活用しましょう! aws-samples / aws-lambda-powertools-python aws-samples / aws-lambda-powertools-java

Slide 54

Slide 54 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools 活用デモアプリ aws-samples / aws-serverless-airline aws-samples / aws-serverless-shopping-cart aws-samples / serverless-ecommerce-platform