3

I'm busy experimenting with pyplates to create AWS CloudFormation JSON templates. I am rather new to python programming and I've run into an issue I'm not sure how to solve.

The CF templates can get very big very quickly and thus the same happens when I use a monolithic file to store all of the resources, etc. I want to break up the resources, etc into separate files to makes things more modular.

To generate the JSON you have to run the following command via cli:

cfpy_generate <pyplate> [<outfile>]

The file is a python file so you should (at least to me) be able to run any Python code in them however when I try import these files I get errors.

For example:

pyplates_subfile.py:

from cfn_pyplates.core import Resource, Properties
from cfn_pyplates.functions import ref

subnet = Resource (
    'ExampleSubnet', 'AWS::EC2::Subnet', # name, resource type
    Properties (
        {
        'AvailabilityZone': 'eu-west-1b',
        'CidrBlock': '192.168.0.0/24',
        'Tags': {},
        'VpcId': ref('VPCID') ,
        }

    )

pyplates_main.py:

import pyplates_subfile

ctf = CloudFormationTemplate(description='Test Template')

ctf.parameters.add( Parameter(
    'VPCID', 'String',
    {
    'Default': VPCID,   
    'Description': 'The default VPC to use',
    })  
)

ctf.resources.add( pyplates_subfile.subnet )

When I try generate the file, I get the following:

$ cfn_py_generate pyplates_main.py 
Traceback (most recent call last):
  File "/Users/marcus/dev/virtenvs/cfn/bin/cfn_py_generate", line 9, in <module>
    load_entry_point('cfn-pyplates==0.3.0', 'console_scripts', 'cfn_py_generate')()
  File "/Users/marcus/dev/virtenvs/cfn/lib/python2.7/site-packages/cfn_pyplates/cli.py", line 120, in generate
    pyplate = _load_pyplate(args['<pyplate>'], options_mapping)
  File "/Users/marcus/dev/virtenvs/cfn/lib/python2.7/site-packages/cfn_pyplates/cli.py", line 40, in _load_pyplate
    exec pyplate in exec_namespace
  File "pyplates_main.py", line 1, in <module>
    import pyplates_subfile
ImportError: No module named pyplates_subfile

However, I can import the module fine via the python console or in an executable script.

In addition, I've looked at the source code to see if there's a way to create my own script using the library that will generate the output for me, however I can't seem to find a way to do this.

Any help is appreciated.

Marcus
  • 51
  • 5

0 Answers0