この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
ども、大瀧です。
きづきあきら+サトウナンキ先生の作品が好きなんですが、バター猫のパラドクス 1巻を買ったのに積み漫画になってて全然読めていないのが悔しい今日この頃です。至急キャッチアップせねば!
今回は子ネタです!!AWS CloudFormation Command Line Interface(Java版)には、テンプレートファイルの文法をチェックをしてくれるcfn-validate-templateコマンドがあります。これをawscli(Python版)でやろうとするとちょっと面倒なので、そのやり方のご紹介です。
awscli(Python版)のCloudFormation Validate Template
はい、awscli(Python版)にも、CloudFormation Validate Templateは実装されています。しかし、ローカルファイルをチェックする--template-fileオプションがないんです。(えー)
helpオプションの結果は、以下です。
$ aws cloudformation validate-template help
VALIDATE-TEMPLATE() VALIDATE-TEMPLATE()
NAME
validate-template -
DESCRIPTION
Validates a specified template.
SYNOPSIS
aws cloudformation validate-template
[--template-body <value>]
[--template-url <value>]
REQUIRED PARAMETERS
None
OPTIONAL PARAMETERS
--template-body (string)
String containing the template body. (For more information, go
to the AWS CloudFormation User Guide .)
Conditional: You must pass --template-url or --template-body .
If both are passed, only --template-body is used.
--template-url (string)
Location of file containing the template body. The URL must
point to a template (max size: 307,200 bytes) located in an S3
bucket in the same region as the stack. For more information, go
to the AWS CloudFormation User Guide .
Conditional: You must pass --template-url or --template-body .
If both are passed, only --template-body is used.
VALIDATE-TEMPLATE()
$
--template-urlオプションは、従来のコマンドと同様、テンプレートを配置したS3のURLを指定するものなので、--template-bodyオプションを使います。
--template-bodyオプションの使い方
bodyということなので、引数にJSON文字列を直接突っ込むわけですが、今回はあらかじめ作ったテンプレートファイルを読み込ませたいので、工夫が必要です。
以下のようにxargsコマンドを駆使して、動かすことができました!(チェックの結果、エラーなしの場合はParameter一覧が出力されます)
$ cat XXXX.template | xargs -0 aws cloudformation validate-template --template-body
{
"ResponseMetadata": {
"RequestId": "708c5633-c909-11e2-a893-ff22ce8e0bbf"
},
"Description": "sample CloudFormation Template.",
"Parameters": [
{
"NoEcho": false,
"Description": "Parameter 1",
"ParameterKey": "parameter1"
},
{
"NoEcho": false,
"Description": "Parameter 2",
"ParameterKey": "parameter2"
}
],
"Capabilities": []
}
ポイントは、xargsコマンドの-0オプションです。xargsコマンドは標準入力を改行、スペース区切りで引数のコマンドに複数のパラメータとして渡してしまうので、今回のようにaws cloudformation validate-templateコマンドに1つのパラメータで渡すようにしています。
Python版awscliを使う機会は徐々に増えてくると思うので、ちょっとした情報として役立てられれば幸いです。