[PYTHON] A script that displays the running CloudFormation stack like a mannequin

If you make various trials and errors with CloudFormation, it will be awkward to click from the mannequin, and you will want to create-stack or delete-stack with CLI. However, it is troublesome to hit the command one by one to read JSON, and it is also troublesome to just look at the mannequin and hit the load button, so I created it quickly. I like how easy it is for python to write this within 30 steps ...

Since boto3 and tabulate are not included as standard, `pip install` should be done. For boto3, refer to the Around site.

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)

When I run it, ↓ This screen is ... キャプチャ.PNG

This is it! On the console, when a new record comes up, it will flow, so the display is reversed.

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

Recommended Posts

A script that displays the running CloudFormation stack like a mannequin
A shell program that displays the Fibonacci sequence
What's in that variable (when running a Python script)
Creating a Python script that supports the e-Stat API (ver.2)
A shell script that just emails the SQL execution result
A Python script that compares the contents of two directories
I wrote a script that splits the image in two
A script that returns 0, 1 attached to the first Python prime number
A class that hits the DMM API
Remote command that behaves like a local command
Run the Python interpreter in a script
A script that morphologically parses a specified URL
A code that corrects the yoon / sokuon (sokuon)
[Python] A program that rounds the score
A python script that draws a band diagram from the VASP output file EIGENVAL
Create a BOT that displays the number of infected people in the new corona
I wrote a script to revive the gulp watch that will die soon