Skip to content

Instantly share code, notes, and snippets.

@antimius
Last active August 9, 2018 14:59
Show Gist options
  • Save antimius/26ea431ba5711d3683b171a083f60ecc to your computer and use it in GitHub Desktop.
Save antimius/26ea431ba5711d3683b171a083f60ecc to your computer and use it in GitHub Desktop.
Format CloudFormation changesets in CLI
#!/usr/bin/env python
from __future__ import print_function
from terminaltables import DoubleTable
from termcolor import colored
import sys, json
def colorize(s):
colors = {
"Add": "green",
"Remove": "red",
"Modify": "yellow"
}
return colored(s.upper(), colors[s]) if colors[s] else s
def table_changeset(changeset):
table_data = [list(map(lambda x : colored(x, attrs=['bold']), ["Action", "Logical ID", "Physical ID", "Resource Type", "Replacement"]))]
for c in changeset["Changes"]:
if c["Type"] == "Resource":
r = c["ResourceChange"]
if (r["Action"] == "Add") or (r["Action"] == "Remove"):
table_data.append([colorize(r["Action"]),
r["LogicalResourceId"], "", r["ResourceType"], ""])
elif r["Action"] == "Modify":
table_data.append([colorize(r["Action"]),
r["LogicalResourceId"], r["PhysicalResourceId"],
r["ResourceType"], r["Replacement"]])
table_instance = DoubleTable(table_data)
return table_instance.table
def main():
input = sys.stdin.read()
changeset = json.loads(input)
print (table_changeset(changeset))
if __name__ == '__main__':
main()
@antimius
Copy link
Author

antimius commented Aug 9, 2018

pip install terminaltables termcolor to use. Reads STDIN and generates a table like this:

Action Logical ID Physical ID Resource Type Replacement
ADD CloudfrontGlobalSecurityGroup AWS::EC2::SecurityGroup
ADD CloudfrontRegionalSecurityGroup AWS::EC2::SecurityGroup
MODIFY CreateInstanceCwAlarmsFunction shared-infra-CreateInstanceCwAlarmsFunction-SFMU32II2YYN AWS::Lambda::Function False
MODIFY ECSAutoScalingGroup shared-infra-ECSAutoScalingGroup-Q2K2TR4O7PC6 AWS::AutoScaling::AutoScalingGroup Conditional
MODIFY ECSLaunchConfiguration shared-infra-ECSLaunchConfiguration-1RBHVLKWP84DU AWS::AutoScaling::LaunchConfiguration True
MODIFY LogsToELKFunction shared-infra-LogsToELKFunction-1QIYG9BDN3TW9 AWS::Lambda::Function False
ADD ManageTopicSubscriptionFunction AWS::Lambda::Function
ADD ManageTopicSubscriptionRole AWS::IAM::Role
REMOVE ManageTopicSuscriptionFunction AWS::Lambda::Function
REMOVE ManageTopicSuscriptionRole AWS::IAM::Role
MODIFY SendToSlackFunction shared-infra-SendToSlackFunction-1CYCDP8W951IS AWS::Lambda::Function False
MODIFY SendToSlackTopic arn:aws:sns:eu-west-1:900100097820:shared-infra-SendToSlackTopic-59TQ96JK5W31 AWS::SNS::Topic False
ADD SubscribeToAmazonIpChangesCustomResource Custom::SubscribeToAmazonIpChangesFunction
REMOVE SuscribeToAmazonIpChangesCustomResource Custom::SuscribeToAmazonIpChangesFunction
MODIFY UpdateCloudfrontSgFunction shared-infra-UpdateCloudfrontSgFunction-6U78LURYT9DL AWS::Lambda::Function False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment