troposphere
troposphere copied to clipboard
Is there a better approach to Sub with Parameters?
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())
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
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)})