Slide 1

Slide 1 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 7th Oct 2020 Kensuke Shimokawa AWS Lambda Powertools サーバーレスのプラクティスをシンプルに実現する @_kensh

Slide 2

Slide 2 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Who am I? Name Kensuke Shimokawa Company Amazon Web Services Japan K.K. Role Serverless Specialist Solutions Architect @_kensh

Slide 3

Slide 3 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 本セッションは 想定される聴講者 • AWSサーバーレス をご利用されている方 • よりプラクティカルなAWS Lambdaの運用を考えている方 • サーバーレスサービス構築・運用に興味がある方 ゴール • AWS Lambda Powertools の使い所を理解していただく

Slide 4

Slide 4 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Table of contents • Serverless Lens について • AWS Lambda Powertools について • Logging • Trace • Metrics

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Well-Architected Framework(W-A)とは? 2015年 AWS re:Invent にて発表

Slide 7

Slide 7 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Well-Architected Frameworkホワイトペーパー 設計原則と(質問と回答形式)のベストプラクティス集 コストの 最適化 セキュリティ 信頼性 パフォーマンス 効率 運⽤の 優秀性

Slide 8

Slide 8 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 毎年アップデート Serverless Lens : 2017年 AWS re:Invent にて発表

Slide 9

Slide 9 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kindle版も無料でダウンロードできます

Slide 10

Slide 10 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Well-Architected Tool https://aws.amazon.com/jp/blogs/news/new-serverless-lens-in-aws-well-architected-tool/

Slide 11

Slide 11 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Serverless Application Lens

Slide 12

Slide 12 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Serverless Application Lens

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 運用上の優秀性 Log Metrics Trace Amazon CloudWatch AWS X-Ray

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools パイプラインに必要なツールリソースを生成 seminar-app アプリケーションに必要なリソースを生成 上位Stack 下位Stack AWS SAM CloudFormation AWS Lambda Powertools 運用上の優秀性をサポート

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Powertools Log Metrics Trace Amazon CloudWatch AWS X-Ray

Slide 20

Slide 20 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Runtime aws-samples / aws-lambda-powertools-python aws-samples / aws-lambda-powertools-java

Slide 21

Slide 21 text

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

Slide 22

Slide 22 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 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace の考え方 Trace Segment Sub-segment 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 32

Slide 32 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS SAMでLambdaのTraceをアクティベイト

Slide 33

Slide 33 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ Result

Slide 34

Slide 34 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ - Before [課題感] パッチを当てる手間

Slide 35

Slide 35 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ - Before Result

Slide 36

Slide 36 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ - Before [課題感] Subsegmentを宣言する必要 冗長な記述

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ - After Result

Slide 39

Slide 39 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Trace / サービスマップ - After Result

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metricsの概念

Slide 42

Slide 42 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metricsの出力 - Before Synchronous [課題感] Metricのフォーマットに 従うのが大変 冗長! 同期出力…

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metricsの出力 - After Asynchronous Result Embedded Metric Format を使用している

Slide 47

Slide 47 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metricsの出力 - After Result ビジネスKPIを可視化!

Slide 48

Slide 48 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. All In One Environment variable Value POWERTOOLS_SERVICE_NAME shopping-cart POWERTOOLS_METRICS_NAMESPACE e-commerce-app LOG_LEVEL INFO 環境変数で名前空間を管理

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. まとめ • Well-Architected Framework(WA)を使って、アーキテクチャのプラクティス の確認を行いましょう。 • AWS Lambda Powertoolsを使って、手間がかかるWAの実践を便利にしてみ ましょう!

Slide 56

Slide 56 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Thank you ! @_kensh

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Demo examples aws-samples / aws-serverless-airline aws-samples / aws-serverless-shopping-cart aws-samples / serverless-ecommerce-platform