【问题标题】:Cloudformation: Reference Created Subnets in ElastiCache SubnetGroupCloudformation:参考 ElastiCache SubnetGroup 中创建的子网
【发布时间】:2016-02-05 15:17:37
【问题描述】:

我在创建动态引用正确子网的 ElastiCache SubnetGroup 时遇到了挑战。我想在东部和西部地区使用相同的模板,因此我在映射中为子网组指定子网。但是,当我尝试运行更新堆栈时,出现以下错误:

Value of property SubnetIds must be of type List of String

这是一个大致显示我想要做的事情的要点:https://gist.github.com/brockhaywood/b71ed34c6a554a0a0fec

AWS 论坛上这个未回答的问题似乎是一个非常相似的问题:https://forums.aws.amazon.com/message.jspa?messageID=532454

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-elasticache amazon-cloudformation


    【解决方案1】:

    我认为SubnetIds should be an array,您只有一个对象。

    "ElastiCacheSubnetGroup": {
      "Type": "AWS::ElastiCache::SubnetGroup",
      "Properties": {
        "SubnetIds": [
            {
                "Fn::FindInMap":["RegionMap", { "Ref":"AWS::Region" }, AppSubnets" ]
            }
        ]
      }
    }
    

    【讨论】:

    • 哦,真的吗? 'AppSubnets' 引用是一个 Refs 数组,这不能满足将它作为一个数组的需要吗?
    • 一个通情达理的人可能会这么想,但它会给你一个抱怨数据类型的错误消息:)。
    • 我也尝试过,向 SubnetIds 提供以下内容,但会产生相同的错误:"SubnetIds": [{ "Fn::FindInMap":[ "RegionMap", { "Ref": "AWS::Region" }, "AppSubnets" ] }]
    【解决方案2】:

    具体问题是您不能在Mappings 值中使用Ref,如Mappings 文档中所述:

    您不能在映射部分中包含参数、伪参数或内部函数。

    作为替代方案,您可以使用Conditions 来完成您的模板正在尝试的操作。这是一个完整的工作示例:

    {
      "Description": "Create an ElastiCache SubnetGroup with different subnet depending on the current AWS region."
      "Conditions": {
        "us-east-1": {"Fn::Equals": [{"Ref":"AWS::Region"}, "us-east-1"]},
        "us-west-2": {"Fn::Equals": [{"Ref":"AWS::Region"}, "us-west-2"]}
      },
      "Resources": {
        "VPC": {
          "Type": "AWS::EC2::VPC",
          "Properties": {
            "CidrBlock": "10.0.0.0/16"
          }
        },
        "AppSubnetA": {
          "Type": "AWS::EC2::Subnet",
          "Properties": {
            "VpcId": {"Ref": "VPC"},
            "CidrBlock": "10.0.0.0/24",
            "AvailabilityZone": {"Fn::Select": [1, {"Fn::GetAZs": ""}]}
          }
        },
        "AppSubnetB": {
          "Type": "AWS::EC2::Subnet",
          "Properties": {
            "VpcId": {"Ref": "VPC"},
            "CidrBlock": "10.0.1.0/24",
            "AvailabilityZone": {"Fn::Select": [1, {"Fn::GetAZs": ""}]}
          }
        },
        "ElastiCacheSubnetGroup": {
          "Type": "AWS::ElastiCache::SubnetGroup",
          "Properties": {
            "Description": "SubnetGroup",
            "SubnetIds": {"Fn::If": ["us-east-1", [
                {"Ref": "AppSubnetA"}
              ],
              {"Fn::If": ["us-west-2",
                [
                  {"Ref": "AppSubnetB"}
                ],
                {"Ref":"AWS::NoValue"}
              ]}
            ]}
          }
        }
      }
    }
    

    【讨论】:

    • 这应该是公认的答案。无法在 CloudFormation 模板的 Mappings 部分中使用动态值。
    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2017-11-30
    • 2020-05-03
    • 2018-04-08
    相关资源
    最近更新 更多