いわさです。
本日 SAM CLI の v1.72.0 がリリースされました。
いくつか新機能がリリースされているのですが、リリース通知のタイトルにもなっているようにsam list
という新しいコマンドが追加されています。
これまでの使い方が大きく変わるような機能ではないのですが、SAM で開発を行う際に少し便利になる機能です。
また、What'new でもsam list
コマンドについては取り上げられていたということもあり、このコマンドがどういうものなのか少し使ってみたので紹介したいと思います。
sam list
では次の 3 つのサブコマンドが提供されています。
% sam list
Usage: sam list [OPTIONS] COMMAND [ARGS]...
Get local and deployed state of serverless application.
Options:
-h, --help Show this message and exit.
Commands:
endpoints Get a summary of the cloud endpoints in the stack.
resources Get a list of resources that will be deployed to...
stack-outputs Get the stack outputs as defined in the...
sam list endpoints
こちらは開発中テンプレートを解析して、呼び出し可能な API Gateway エンドポイントと関数 URL を表示するものです。
デプロイ前の場合はメソッドのみ、デプロイ後は--stack-name
オプションを指定することでクラウド上からエンドポイント情報や Physical ID も取得することが出来ます。
% sam list endpoints
The --stack-name options was not provided, displaying only local template data. To see data about deployed resources, provide the corresponding stack name.
2023-02-04 06:40:12 Loading policies from IAM...
2023-02-04 06:40:14 Finished loading policies from IAM.
Endpoints
-------------------------------------------------------------------------------------------------------------
Resource ID Physical ID Cloud Endpoints Methods
-------------------------------------------------------------------------------------------------------------
NetCodeWebAPIServerless - - -
ServerlessHttpApi - - /{proxy+}['x-amazon-
apigateway-any-method'];
/['x-amazon-apigateway-
any-method']
-------------------------------------------------------------------------------------------------------------
「呼び出し可能な」なので、例えば Function に次のように API 定義が含まれていなければエンドポイントの表示はされません。 ただし、関数 URL の場合はスタック情報を指定することでエンドポイントが確認することが出来ました。
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
:
Resources:
Function1:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/hoge0204sam-dotnet-serverless/
Handler: hoge0204sam-dotnet-serverless
Runtime: dotnet6
MemorySize: 1024
Function2:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/hoge0204sam-dotnet-serverless/
Handler: hoge0204sam-dotnet-serverless
Runtime: dotnet6
MemorySize: 1024
FunctionUrlConfig:
AuthType: AWS_IAM
% sam list endpoints
The --stack-name options was not provided, displaying only local template data. To see data about deployed resources, provide the corresponding stack name.
2023-02-04 17:22:37 Loading policies from IAM...
2023-02-04 17:22:43 Finished loading policies from IAM.
Endpoints
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Resource ID Physical ID Cloud Endpoints Methods
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function1 - - -
Function2 - - -
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
% sam list endpoints --stack-name hoge0204dotnetsam
2023-02-04 17:22:49 Loading policies from IAM...
2023-02-04 17:22:52 Finished loading policies from IAM.
Endpoints
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Resource ID Physical ID Cloud Endpoints Methods
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function1 hoge0204dotnetsam- - -
Function1-XnB8AWSIeyRQ
Function2 hoge0204dotnetsam- https://cenxia6n75grcm6ke2dwb6itii0htcp -
Function2-k9NmLLu1Ebca m.lambda-url.ap-northeast-1.on.aws/
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
ちなみにこちらはコマンド実行時に前処理として自動でsam validate
してくれるのでテンプレートチェックも出来ます。
ただし先日リリースされた--lint
オプションなどは使えないので、明らかにテンプレート構文として崩れている場合とかが検出出来るくらいのものでしょうか。
このコマンドはローカルテンプレートを解析するので、前提としてテンプレートファイルが必要です。
デフォルトはカレントディレクトリの template.yml を参照しますが、--template-file
で指定も可能です。
sam list resources
このコマンドはテンプレートを解析してローカルスタック内のリソースを表示することが出来ます。
先程と同様に--stack-name
オプションを指定している場合は、デプロイ済みリソースの Physical ID も表示することが出来ます。
変更セットを作成してエンドポイントや作成リソースの確認をしなくても、今回のコマンドでクラウドへの問い合わせなしでローカルで作成されるリソースのチェックが出来そうです。
デプロイ前
% sam list resources
The --stack-name options was not provided, displaying only local template data. To see data about deployed resources, provide the corresponding stack name.
2023-02-04 07:41:31 Loading policies from IAM...
2023-02-04 07:41:36 Finished loading policies from IAM.
Resources
-------------------------------------------------------------------------------------------------------------
Logical ID Physical ID
-------------------------------------------------------------------------------------------------------------
NetCodeWebAPIServerless -
NetCodeWebAPIServerlessRole -
NetCodeWebAPIServerlessRootResourcePermission -
NetCodeWebAPIServerlessProxyResourcePermission -
ServerlessHttpApi -
ServerlessHttpApiApiGatewayDefaultStage -
SampleTable -
-------------------------------------------------------------------------------------------------------------
デプロイ後
% sam list resources --stack-name hoge0204dotnetsam
2023-02-04 07:41:59 Loading policies from IAM...
2023-02-04 07:42:05 Finished loading policies from IAM.
Resources
-------------------------------------------------------------------------------------------------------------
Logical ID Physical ID
-------------------------------------------------------------------------------------------------------------
NetCodeWebAPIServerless hoge0204dotnetsam-NetCodeWebAPIServerless-
QIvUL3dR4Qza
NetCodeWebAPIServerlessProxyResourcePermission hoge0204dotnetsam-NetCodeWebAPIServerlessProxyResour
cePermission-3NSGT3MQVK38
NetCodeWebAPIServerlessRole hoge0204dotnetsam-
NetCodeWebAPIServerlessRole-1TH9J6O02VGUA
NetCodeWebAPIServerlessRootResourcePermission hoge0204dotnetsam-NetCodeWebAPIServerlessRootResourc
ePermission-14V4O34I6SXEB
SampleTable hoge0204sam-dotnet-serverlessBookCatalog
ServerlessHttpApi loyp0o0cb3
ServerlessHttpApiApiGatewayDefaultStage $default
-------------------------------------------------------------------------------------------------------------
sam list stack-outputs
このコマンドはローカルテンプレート情報を出力するものではなく、デプロイ済みのスタックを指定して「出力」を表示するものです。
% sam list stack-outputs --stack-name aws-sam-cli-managed-stage1-pipeline-resources
Stack Outputs
------------------------------------------------------------------------------------------------------------------------------------------------------------------
OutputKey OutputValue Description
------------------------------------------------------------------------------------------------------------------------------------------------------------------
PipelineUser arn:aws:iam::123456789012:user/aws-sam-cli-managed- ARN of the Pipeline IAM User
stage1-pipeline-r-PipelineUser-E9KCKVOVHAYN
PipelineUserSecretKey arn:aws:secretsmanager:ap-northeast-1:123456789012:s AWS Access Key and Secret Key of pipeline user.
ecret:PipelineUserSecretKey-DiRwtkntsLkg-CxIAUC
CloudFormationExecutionRole arn:aws:iam::123456789012:role/aws-sam-cli-managed- ARN of the IAM Role(CloudFormationExecutionRole)
stage-CloudFormationExecutionR-J6KU1L8E0MCD
ArtifactsBucket arn:aws:s3:::aws-sam-cli-managed-stage1-pipeli- ARN of the Artifacts bucket
artifactsbucket-gx43qsrp0wlx
PipelineExecutionRole arn:aws:iam::123456789012:role/aws-sam-cli-managed- ARN of the IAM Role(PipelineExecutionRole)
stage1-p-PipelineExecutionRole-NN58Z19XOUOF
------------------------------------------------------------------------------------------------------------------------------------------------------------------
背景としては、スタック作成後に出力されるエンドポイントやリソース情報などをチーム内などで共有する際にsam deploy
時以外に取得する方法がありませんでした。
そのため、これまではマネジメントコンソールの出力タブを確認するか、次のように AWS CLI で加工しつつ取得する必要がありました。
% aws cloudformation describe-stacks --stack-name hoge0204dotnetsam | jq ".Stacks[0].Outputs"
[
{
"OutputKey": "WebEndpoint",
"OutputValue": "https://loyp0o0cb3.execute-api.ap-northeast-1.amazonaws.com/",
"Description": "API Gateway endpoint URL"
}
]
このあたりが SAM CLI のみで簡単に取得出来るようになったよ、ということのようです。
参考 Issue はこのあたりです。
出力フォーマットは既定のテーブル形式以外に JSON も
今回の 3 つのサブコマンド全てで出力形式を指定することが出来ます。
テーブルか JSON かを指定することが出来て、省略時はデフォルトとしてテーブルで動作します。
JSON
% sam list stack-outputs --stack-name hoge0204dotnetsam --output json
[
{
"OutputKey": "WebEndpoint",
"OutputValue": "https://loyp0o0cb3.execute-api.ap-northeast-1.amazonaws.com/",
"Description": "API Gateway endpoint URL"
}
]
テーブル
% sam list stack-outputs --stack-name hoge0204dotnetsam --output table
Stack Outputs
------------------------------------------------------------------------------------------------------------
OutputKey OutputValue Description
------------------------------------------------------------------------------------------------------------
WebEndpoint https://loyp0o0cb3.execute-api.ap- API Gateway endpoint URL
northeast-1.amazonaws.com/
------------------------------------------------------------------------------------------------------------
さいごに
本日は AWS SAM CLI に新しく追加されたsam list
コマンドについて確認してみました。
過去のアップデートであったsam pipeline
やsam sync
のように開発方法が変わる、というようなコマンドではないのかもしれませんが、色々なシーンで気軽に使える便利なコマンドという感じでしょうか。
覚えておくと良さそうです。