LoginSignup
1
0

More than 3 years have passed since last update.

実行中のCloudFormationのスタックをマネコンっぽく表示するスクリプト

Last updated at Posted at 2020-05-10

CloudFormationでいろいろ試行錯誤しているとマネコンからポチポチしてるのがいい加減面倒になってきて、CLIでcreate-stackとかdelete-stackしたくなる。でも結果をいちいちコマンド叩いてJSON読んだりするのも面倒だし、そこだけマネコン見てポチポチリロードボタン叩くのもなぁ、なので、サクッと作成。pythonはこれが30step以内に書けちゃうお手軽さがいいね……。

boto3, tabulateあたりは標準では入っていないのでpip installしておくこと。
boto3についてはこの辺のサイトも参考に。

import os
import sys
import time
import boto3
import pprint
from tabulate import tabulate

args = sys.argv

client = boto3.client('cloudformation')

stackstatus = ""

while stackstatus != 'CREATE_COMPLETE':
  os.system('clear')

  response = client.describe_stacks(StackName=args[1])
  stacks = response['Stacks']
  stackstatus = stacks[0]['StackStatus']

  response = client.describe_stack_events(StackName=args[1])
  events = response['StackEvents']
  events.sort(key=lambda x:x['Timestamp'])

  rows = []
  for keys in events:
    cols = []
    cols.append(keys['Timestamp'])
    cols.append(keys['LogicalResourceId'])
    cols.append(keys['ResourceStatus'])
    rows.append(cols)

  headers = ['Timestamp', 'LogicalResourceId', 'Status']
  table = tabulate(rows, headers)
  print(table)

  if stackstatus != 'CREATE_COMPLETE':
    time.sleep(10)

実行してみると、↓この画面が…
キャプチャ.PNG

こうだ!コンソールだと、上に新しいレコードが来ると流れてしまうから、表示を逆順にしている。

Timestamp                         LogicalResourceId           Status
--------------------------------  --------------------------  ------------------
2020-05-10 10:47:57.928000+00:00  ApigwTest-issue01-Pipeline  CREATE_IN_PROGRESS
2020-05-10 10:48:02.269000+00:00  CODEBUILDLOGGROUP           CREATE_IN_PROGRESS
2020-05-10 10:48:02.699000+00:00  S3BUCKET                    CREATE_IN_PROGRESS
2020-05-10 10:48:02.789000+00:00  CODEBUILDLOGGROUP           CREATE_IN_PROGRESS
2020-05-10 10:48:03.178000+00:00  CODEBUILDLOGGROUP           CREATE_COMPLETE
2020-05-10 10:48:04.363000+00:00  S3BUCKET                    CREATE_IN_PROGRESS
2020-05-10 10:48:25.539000+00:00  S3BUCKET                    CREATE_COMPLETE
2020-05-10 10:48:27.772000+00:00  CODEBUILDIAMROLE            CREATE_IN_PROGRESS
2020-05-10 10:48:29.158000+00:00  CODEBUILDIAMROLE            CREATE_IN_PROGRESS
2020-05-10 10:48:45.746000+00:00  CODEBUILDIAMROLE            CREATE_COMPLETE
2020-05-10 10:48:49.115000+00:00  CODEBUILD                   CREATE_IN_PROGRESS
2020-05-10 10:48:51.445000+00:00  CODEBUILD                   CREATE_IN_PROGRESS
2020-05-10 10:48:52.222000+00:00  CODEBUILD                   CREATE_COMPLETE
2020-05-10 10:48:54.622000+00:00  PIPELINE                    CREATE_IN_PROGRESS
2020-05-10 10:48:55.360000+00:00  PIPELINE                    CREATE_IN_PROGRESS
2020-05-10 10:48:55.853000+00:00  PIPELINE                    CREATE_COMPLETE
2020-05-10 10:48:57.646000+00:00  ApigwTest-issue01-Pipeline  CREATE_COMPLETE
1
0
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
0