この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
いわさです。
先月のAWS SAM CLIのアップデート(v1.48.0)で、SAM CLIのInit時にX-Rayトレースを有効化してスタックを作成出来るオプションが追加され、以下の記事で紹介しました。
そして、上記記事のさいごで、Lambda関数は対応されたが併せて作成される API Gateway はX-Rayトレースが有効化されないので、手動でInit後にテンプレートを修正する必要がある点をお伝えしました。
先日のSAM CLIのアップデート(v1.51.0)で、Tracingオプション時にAPI Gatewayも有効化されるように機能強化されました。
動作確認してみていきたいと思います。
SAM CLI v1.51以降で使える
API Gatewayのトレース有効化は、SAM CLI v1.51以降で有効となるのでまずはSAM CLIのバージョンを確認し必要に応じてバージョンアップしましょう。
この記事ではmacOS環境でHomebrewを使用しています。
$ sam --version
SAM CLI, version 1.48.0
$ brew upgrade aws-sam-cli
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 7 taps (hashicorp/tap, weaveworks/tap, shopify/shopify, homebrew/core, homebrew/cask, dart-lang/dart and aws/tap).
==> New Formulae
aws-nuke glider groestlcoin mariadb@10.7 pg_partman stencil yorkie
==> Updated Formulae
Updated 591 formulae.
==> Deleted Formulae
libzt
==> New Casks
:
Removing: /usr/local/Cellar/librsvg/2.54.1... (24 files, 56MB)
==> Checking for dependents of upgraded formulae...
==> No broken dependents to reinstall!
$ sam --version
SAM CLI, version 1.51.0
Tracingオプションを使用してデプロイしてみる
この記事では、Hello World サンプルテンプレートを作成してみます。 なお、このオプション自体はカスタムテンプレートに対応していて、既存テンプレートに有効化オプションが含まれている場合も考慮されるようになっています。
TracingオプションをつかってInit
$ sam init --tracing
You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
4 - Scheduled task
5 - Standalone function
6 - Data processing
7 - Infrastructure event management
8 - Machine Learning
Template: 1
Use the most popular runtime and package type? (Python and zip) [y/N]: y
Project name [sam-app]:
:
出力されたテンプレートを確認してみましょう。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Tracing: Active
Api:
TracingEnabled: True
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
:
前回の記事でご紹介した、API GatewayのX-Rayトレーシングを有効化するためのグローバルパラメータが追加されていますね。
HelloWorldの場合はAPIが明示的に定義はされていないのですが、AWS::Serverless::Function
で作成されるAPI Gatewayに適用される動きとなっています。
デプロイ
最後に、デプロイして動作確認してみましょう。
Lambda関数に加えて、API Gatewayもデフォルトで有効化されていることが確認出来ると思います。
さいごに
本日は、SAM CLIでInit時にAPI GatewayのX-Rayトレーシングが有効化出来るようになったので動作確認兼ねて紹介しました。
他にもv1.51.0で機能強化されています、SAM CLIのリリース通知は以下で確認出来ます。
こんなクールな機能強化、一体誰が...あ!!
ぜひ使ってみてください。