in cloudformation is it possible to create a reusable string?

i have a string the is formed from a number of variables which i use for many different things such as Tags and host names.

is there a way of creating a reusable string that i can just Ref though out the template?

any thoughts on this would be much appreciated. edit: An example would be passing in 2 parameters such as ProjectName and Environment joining them

"Fn::Join" : [ "-", [ {"Ref":"ProjectName"}, {"Ref":"Environment"} ] ]

and using the results in a number of places


Solution 1:

Unfortunately my experiments are to agree with your conclusion Graeme. I have struggled with this for some time but not found a way of computing a String via Fn::Join and keeping it for later in the template.

Can I share with you a typical example?

As at 2014-09-18, the most sophisticated way of storing a string value for re-use in the Resources section would be via a (second-level) Mapping. Better still, I can pre-compute a Condition and determine whether to use the string or not. But unfortunately the storing of a string just does not work.

Here is an example stanza of such a Mapping …

    "LoadBalancerBucketMap": {
        "BucketName": {
            "string": {
                "Fn::Join": [
                    "-",
                    [
                        "mylb",
                        {
                            "Ref": "Environment"
                        },
                        "logs"
                    ]
                ]
            }
        }
    },

And here is an access expression to it in a Resource

                "S3BucketName": {
                    "Fn::FindInMap": [
                        "LoadBalancerBucketMap",
                        "BucketName",
                        "string"
                    }

In this example, Environment is a literal string Parameter with a convenient default. So that is a very clear example of creating and re-using a string. But here is what the CloudFormation processor says …

Template validation error: Template format error: Every Mappings attribute must be a String or a List.

So I share your frustration that there does not seem to be a way in a template to provide the effect of a temporary string variable.

… So far as I can see. But the answer you originally received said the opposite of this (although without an example).

So if anyone does have an example workaround, then please could they share?

Solution 2:

You can use params or mappings to store strings that you can use in your templates.