$30 off During Our Annual Pro Sale. View Details »

サーバーレスのプラクティスをシンプルに実現する / AWS Lambda Powertools

kensh
October 07, 2020

サーバーレスのプラクティスをシンプルに実現する / AWS Lambda Powertools

サーバーレス のプラクティスを実践するために多くのコーディングをしていませんか?

ビジネスロジックに集中するために、プラクティスを素早く実現するLambda Powertoolsをご紹介します!

想定される聴講者
AWSサーバーレス をご利用されている方
よりプラクティカルなAWS Lambdaの運用を考えている方
サーバーレスサービス構築・運用に興味がある方

ゴール
AWS Lambda Powertools の使い所を理解していただく

kensh

October 07, 2020
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

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

    View Slide

  2. © 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. © 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/

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. © 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
    65;7
    >*
    C
    D
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    Amazon
    CloudWatch
    AWS X-Ray
    A9
    (9
    65;7
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    A9
    (9
    65;7
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    A9
    (9

    C"D
    ,1?:
    8$.%7
    CB/43&D

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. © 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

    View Slide

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

    View Slide

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

    View Slide

  25. © 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
    [課題感]
    構造化されたが、コードの
    可読性が損なわれる

    View Slide

  26. © 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
    [解消]
    構造化されており、記述も楽!

    View Slide

  27. © 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情報も
    出力可能

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. © 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  48. © 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
    環境変数で名前空間を管理

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  54. © 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
    65;7
    >*
    C
    D
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    Amazon
    CloudWatch
    AWS X-Ray
    A9
    (9
    65;7
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    A9
    (9
    65;7
    #-
    ETL%7
    )@ '2

    C"D
    ,1?:
    8$.%7
    CB/43&D
    A9
    (9

    C"D
    ,1?:
    8$.%7
    CB/43&D

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  58. © 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

    View Slide