I tried to make a list by extracting only the information about the parameters from the cloudFormation yaml file. It is an unfinished product that may cause an error depending on how to write yaml, but I personally got the point, so I will leave it here.
This is the site that I used as a reference when creating this script.
-Handling YAML files in Python -[Small story] How to avoid being disturbed by the abbreviated syntax of built-in functions when parsing CloudFormation templates in Python
Immediately, I will introduce the script.
--Script flow
-(1) Read the CloudFormation code (yaml) as a text file
-(2) Extend the abbreviated syntax (Example: ! Sub-> Fn :: Sub, ! Ref-> Fn :: Ref)
-(3) Read as yaml without abbreviated syntax
-(4) Check the contents of yaml and display the information under Parameters in a list.
paramlist.py
## command sample
## python paramlist.py test.yml
#yaml module installation`$ pip install pyyaml`
import yaml
import sys
import re
#Do not dig deep
exclusionStr = "|AWSTemplateFormatVersion|Description|Type|TemplateURL|DependsOn|Mappings|Outputs|"
args = sys.argv
path = args[1]
#(1) CloudFormation code(yaml)As a text file
f = open(path)
s0 = f.read()
f.close()
#(2) Extend the abbreviated syntax (example:`!Sub` -> `Fn::Sub` , `!Ref` -> `Fn::Ref`)
s1 = re.sub("!((Sub|Ref|Join|GetAtt|FindInMap))\s", r'Fn::\1 ', s0)
#(3) Read as yaml without abbreviated syntax
obj = yaml.safe_load(s1)
#(4) Check the contents of yaml and display a list of information under Parameters.
def readYaml( curObj, pathStr , exeFlg):
    try:
        if exeFlg == 0:
            for key in curObj:
                #Go to the next level
                curFlg = key in exclusionStr
                if not curFlg:
                    if key == "Parameters":
                        nxtFlg = 1
                    else:
                        nxtFlg = 0
                    pathStr += "/" + key
                    readYaml( curObj[key] , pathStr , nxtFlg)
        else:
            print("---- {0} ----".format( pathStr ) )
            #Display parameter items and values
            for key in curObj:
                print( "\t{0} - {1}".format(key , curObj[key] ) )
    except Exception as e:
        print("ERROR curObj = {0}, pathStr = {1}, exeFlg = {2}".format( curObj, pathStr, exeFlg ) )
        print(e)
#############################
## -------- START -------- ##
print("---- Parameter List ----" )
readYaml( obj , "" , 0 )
Run the script against the following CloudFormation code.
test.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: cloudformation yaml sample
Parameters:
  hogePrefix:  { Type: String , Default: hogefuga123 }
  BucketUrl: { Type: String , Default: "https://hogefuga123.s3.amazonaws.com/" }
  AZName001: { Type: String , Default: ap-northeast-1a }
  AZName002: { Type: String , Default: ap-northeast-1c }
  VPCName: { Type: String , Default: vhoge01 }
Resources:
  VPC:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: !Sub "${BucketUrl}${VPCTemplate}"
      Parameters: 
        hogePrefix: !Ref hogePrefix
        BucketUrl: !Ref BucketUrl
        VPCName: !Ref VPCName
Execution result
$ python paramlist.py test.yml
---- Parameter List ----
---- /Parameters ----
        hogePrefix - {'Type': 'String', 'Default': 'hogefuga123'}
        BucketUrl - {'Type': 'String', 'Default': 'https://hogefuga123.s3.amazonaws.com/'}
        AZName001 - {'Type': 'String', 'Default': 'ap-northeast-1a'}
        AZName002 - {'Type': 'String', 'Default': 'ap-northeast-1c'}
        VPCName - {'Type': 'String', 'Default': 'vhoge01'}
---- /Parameters/Resources/VPC/Properties/Parameters ----
        hogePrefix - Fn::Ref hogePrefix
        BucketUrl - Fn::Ref BucketUrl
        VPCName - Fn::Ref VPCName
This is the environment setting when using this script on AWS Cloud9.
#Default to Python 2->Switch to Python 3
$ sudo alternatives --config python
$ pip -V
$ sudo pip install --upgrade pip
$ pip -V
#install yaml module
$ pip install pyyaml
As introduced on the site above, loading CloudFormation code with abbreviated syntax as yaml will result in an error.
$ python sample.py test.yml
test.yaml
Exception occurred while loading YAML...
could not determine a constructor for the tag '!Sub'
  in "test.yaml", line 72, column 20

The script I made is incomplete and I want to make sure there are no errors in any yaml format.
Recommended Posts