troposphere icon indicating copy to clipboard operation
troposphere copied to clipboard

Is there a better approach to Sub with Parameters?

Open avoidik opened this issue 6 years ago • 2 comments

Hi,

Could you please suggest a better approach without so much curly braces to Sub with Parameter than provided below?

param_a = troposphere.Parameter("ParamA", Type='String')
s = 'foo ${{{0}}} ${{{1}}}'.format("AWS::Region", param_a.title)
print(troposphere.Sub(s).to_dict())

avoidik avatar Mar 13 '19 07:03 avoidik

This is what I do, given, say, a Bash script like this that's in my user_data variable:

export AWS_DEFAULT_REGION=${AWS::Region}
export COOKBOOK=${COOKBOOK}
export PROJECT=${PROJECT}

Then in the troposphere Python script:

troposphere.Sub(user_data, COOKBOOK=cookbook, PROJECT=name)

When referring to a variable in the Bash script, I remove the curly braces, since that's what CloudFormation attempts to replace, e.g.:

tar -C cookbooks -xf $COOKBOOK.tar.gz

shatil avatar Mar 15 '19 19:03 shatil

You could use awacs (PyPI, GitHub). Here are examples:

from awacs.codepipeline import ARN
from troposphere import Sub

Sub(
    ARN(
        resource=pipeline_name,
        region="${AWS::Region}",
        account="${AWS::AccountId}",
    )
)
from awacs.cloudformation import ARN
from troposphere import Sub

Sub(
    ARN(
        resource="transform/Serverless-2016-10-31",
        region="${AWS::Region}",
        account="aws",
    )
)
from awacs.s3 import ARN
from troposphere import ImportValue, Sub

bucket = "foo"
Sub(ARN(resource="${BucketName}"), {"BucketName": ImportValue(bucket)})

michael-k avatar Mar 22 '19 10:03 michael-k