troposphere icon indicating copy to clipboard operation
troposphere copied to clipboard

TemplateGenerator: TypeError when CustomResource string property is an expression

Open ns-cweber opened this issue 6 years ago • 0 comments

(Troposphere version 2.5.3)

I have a custom resource, and I'd like to set the service token on a conditional for compliance reasons; however, I get an error because TemplateGenerator seems to require string literals and forbids conditionals. I've also tested by creating an AWS::S3::Bucket with a conditional BucketName property; however, TemplateGenerator happily accepts the conditional there, leading me to think it's an issue particular to custom resources.

from troposphere.template_generator import TemplateGenerator

TemplateGenerator(
    {
        "AWSTemplateVersion": "2010-09-09",
        "Parameters": {"Environment": {"Type": "String"}},
        "Conditions": {"Production": {"Equals": [{"Ref": "Environment"}, "prod"]}},
        "Resources": {
            "MyResource": {
                "Type": "Custom::Resource",
                "Properties": {
                    "ServiceToken": {"If": ["Production", "prod", "nonprod"]}
                },
            }
        },
    }
)

produces:

Traceback (most recent call last):
  File "example.py", line 12, in <module>
    "ServiceToken": {"If": ["Production", "prod", "nonprod"]}
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/template_generator.py", line 72, in __init__
    self._get_resource_type_cls(k, v)
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/template_generator.py", line 178, in _convert_definition
    return self._create_instance(expected_type, args, ref)
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/template_generator.py", line 297, in _create_instance
    return cls(title=ref, **args)
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/__init__.py", line 127, in __init__
    self.__setattr__(k, v)
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/__init__.py", line 214, in __setattr__
    self._raise_type(name, value, expected_type)
  File "/Users/cweber/.pyenv/versions/3.6.8/lib/python3.6/site-packages/troposphere/__init__.py", line 232, in _raise_type
    expected_type))
TypeError: <class 'troposphere.template_generator.CustomResource'>: MyResource.ServiceToken is <class 'dict'>, expected <class 'str'>

ns-cweber avatar Jan 09 '20 22:01 ns-cweber