Skip to content

Instantly share code, notes, and snippets.

@cdechery
Last active November 21, 2022 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdechery/06ed9aa38e6266e680347e69845f0765 to your computer and use it in GitHub Desktop.
Save cdechery/06ed9aa38e6266e680347e69845f0765 to your computer and use it in GitHub Desktop.
CloudFormation command line generator from template
# You need to install cfn-tools with "pip install cfn_flip"
import sys
from cfn_tools import load_yaml
try:
INPUT_FILE = sys.argv[1]
except Exception as e:
print('No input file!\nUsage: python cfn-cmdline-gen.py template.yml [separator-char]')
exit(1)
SEPARATOR = '^' # default for Windows
try:
SEPARATOR = sys.argv[2]
except Exception as e:
pass
CMD_LINE = 'aws cloudformation create-stack '+SEPARATOR+'\n--stack-name <stackname> '+SEPARATOR+'\n' + \
'--parameters '+SEPARATOR+'\n'
with open(INPUT_FILE, 'r') as fdata:
ydata = load_yaml(fdata)
# no support for NAMED_IAM and AUTO_EXPAND capabilities yet
capability_iam = False
for key, value in ydata.items():
if key == 'Parameters':
for pkey, pvalue in value.items():
CMD_LINE = CMD_LINE + 'ParameterKey='+pkey+',ParameterValue=<value> '+SEPARATOR+'\n'
if key == 'Resources':
for rkey, rvalue in value.items():
if rvalue['Type'].startswith('AWS::IAM'):
capability_iam = True
CMD_LINE = CMD_LINE + '--template-body file://'+INPUT_FILE+' '+SEPARATOR+'\n'
if capability_iam:
CMD_LINE = CMD_LINE + '--capabilities CAPABILITY_IAM '+SEPARATOR+'\n'
CMD_LINE = CMD_LINE[:-3]
print(CMD_LINE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment