-
Notifications
You must be signed in to change notification settings - Fork 3
/
cfn-summary
executable file
·47 lines (34 loc) · 1.31 KB
/
cfn-summary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
import simplejson as json
import sys
from argparse import ArgumentParser
from os import path
script_name = path.basename(__file__)
parser = ArgumentParser()
parser.add_argument('file_name',
metavar='FILE', help='CloudFormation json file')
parser.add_argument('--sections', metavar='SECTIONS',
help='sections to display in the output')
args = parser.parse_args()
with open(args.file_name) as stack_json:
try:
stack = json.load(stack_json)
except json.JSONDecodeError:
sys.stderr.write("%s: %s is not valid json.\n" % (script_name, args.file_name))
sys.exit(1)
# these meta keys do not contain resources so we drop them
unneeded_keys = ('AWSTemplateFormatVersion', 'Description')
[stack.pop(k, None) for k in unneeded_keys]
if args.sections:
sections = args.sections.split(',')
sections = [s.capitalize() for s in sections]
# verify all the section names exist in the json
for section in sections:
if section not in stack.keys():
sys.stderr.write("%s: section '%s' is not in the template\n" % (script_name, section))
sys.exit(1)
else:
sections = stack.keys()
for section in sorted(sections):
resources = len(stack[section])
print("%s has %d resources" % (section, resources))