LoginSignup
1
1

More than 3 years have passed since last update.

AWS CloudFormationのデプロイ済みスタックのテンプレートをYAMLで取得

Last updated at Posted at 2021-01-07

デプロイ済みのスタックのテンプレートファイルがどうだったかを見たいときに、awscliのaws cloudformation get-templateコマンドで見ることができます。

$ aws cloudformation get-template --stack-name STACK_NAME

しかし、フォーマットはJSONで固定のようです。たとえデプロイ時はYAMLだったとしてもです。YAMLで見たい場合は、JSONからYAMLに変換する必要があります。

2021/03/08 追記
この記事を書いた当時なにを勘違いしていたのか不明ですが、YAMLでデプロイしたらYAMLで取得できます。↓もう一度記事にして整理しました。
AWS CloudFormationのデプロイ済みスタックのテンプレートを取得 - Qiita

yqコマンドでYAMLに変換

yqというコマンドがあります。JSONを扱うjqコマンドのYAML版です。

yq: Command-line YAML/XML processor - jq wrapper for YAML and XML documents — yq documentation

実行例

$ aws cloudformation get-template --stack-name STACK_NAME | yq . -y

YAMLはJSONの上位互換なので、yqコマンドはYAML、JSONどちらも入力に受け付けます。出力はデフォルトではJSONで、 -y を付けることでYAML出力になります。

yqコマンドのインストール

yqはPythonで書かれており、pipでインストールできます。

$ pip install yq

内部でjqを呼び出しているようで、jqがないと次のようなエラーになります。

yq: Error starting jq: FileNotFoundError: [Errno 2] No such file or directory: 'jq'. Is jq installed and available on PATH?

jq のインストールはUbuntuならば

$ sudo apt install -y jq

CentOS 8ならば

$ sudo yum install -y jq

RubyのワンライナーでYAMLに変換

JSONからYAMLへの変換はRubyのワンライナーでもできます。

$ aws cloudformation get-template --stack-name STACK_NAME | ruby -rjson -ryaml -e 'puts YAML.dump(JSON.load(ARGF))'

と書いてから、ついさっきYAMLはJSONの上位互換と書いたばかりなのを思い出しました。次のほうが短くて済みます。

$ aws cloudformation get-template --stack-name STACK_NAME | ruby -ryaml -e 'puts YAML.dump(YAML.load(ARGF))'
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1