How to refer a derived variable in Cloudformation

有些话、适合烂在心里 提交于 2019-12-21 07:58:06

问题


I am looking for small help in cloudformation and could not find help from documentation or may be not searching the question in right way.

Here is the question.

I am getting availability zones for the environment from mappings as follows.

"AvailabilityZone": {
"Fn::Select": [
    "1",
    {
        "Fn::FindInMap": [
            "Environment",
            {
                "Ref": "EnvType"
            },
            "AvailabilityZones"
        ]
    }
]

}

I need to use the AZ name in my volume naming convention. How could refer the derived variable "AvailabilityZone" again.?

Currently i am doing this.

    {
    "Key": "Name",
    "Value": {
        "Fn::Join": [
            "-",
            [
                {
                    "Ref": "NamePrefix"
                },
                {
                    "Ref": "EnvType"
                },
                "myconstant",
                {
                    "Fn::Select": [
                        "2",
                        {
                            "Fn::Split": [
                                "-",
                                {
                                    "Fn::Select": [
                                        "1",
                                        {
                                            "Fn::FindInMap": [
                                                "Environment",
                                                {
                                                    "Ref": "EnvType"
                                                },
                                                "AvailabilityZones"
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        ]
    }
}

I am doing the same code twice. How can i re-use the derived variable here.?


回答1:


Unfortunately, the short answer is you can't. Hopefully someday AWS supports variables in CloudFormation.

There are some hacks that might be of interest. Emphasis on hack!

  • Use a CloudFormation pre-processor that does what you want (eg, Troposphere)
  • Use a Custom Resource that outputs the value, then use GetAtt to refer to the value. (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html)
  • Use the AWS::Include transform to refer to a template snippet in S3 (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/create-reusable-transform-function-snippets-and-add-to-your-template-with-aws-include-transform.html), in that case you're not really using variables, off course.


来源:https://stackoverflow.com/questions/43926728/how-to-refer-a-derived-variable-in-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!