FindInMap 可以返回列表吗?

FindInMap 可以返回列表吗?

我正在使用 CloudFormation 将 ELB 部署到具有预先存在的子网的预先存在的 VPC。我在模板的映射部分列出了子网,并根据环境参数选择了映射。如果映射是针对单个值的,则此方法可以正常工作,但我希望映射返回一个列表,因为我希望 ELB 是多可用区的。我尝试了以下示例的各种组合,但总是出现相同的错误Value of property Subnets must be of type List of String。有没有办法让 FindInMap 返回值列表?

ELB 配置示例

"ElasticLoadBalancerAPI": {
            "Properties": {
                "ConnectionDrainingPolicy": {
                    "Enabled": true,
                    "Timeout": 30
                },
                "ConnectionSettings": {
                    "IdleTimeout": 60
                },
                "CrossZone": "false",
                "Subnets" : [
                    {
                        "Fn::FindInMap": [
                            "AWSEnv2PublicSubnets",
                            {
                                "Ref": "Env"
                            },
                            "subList"
                        ]
                    }
                ],
                "SecurityGroups" : [ { "Ref" : "LoadBalancerSecurityGroup" }],
etc...

示例参数

"Parameters": {
  "Env": {
            "Description": "Environment",
            "Type": "String",
            "Default": "DEV",
            "AllowedValues": [
                "DEV",
                "TEST"
            ]
        }
}

示例映射

"Mappings": {
  "AWSEnv2PublicSubnets": {
            "DEV": {
                "subList": ["subnet-1111","subnet-2222","subnet-3333"]
            },
            "TEST": {
                "subList": ["subnet-4444"]
            }
        }
}

答案1

使用分裂函数工作原理:

JSON

"Mappings": {
   "AWSEnv2PublicSubnets": {
        "DEV": {
            "subList": "subnet-1111,subnet-2222,subnet-3333"
        }
    }
}

然后:

"Subnets" : { 
  "Fn::Split" : [
    ",", 
    { "Fn::FindInMap": [
      "AWSEnv2PublicSubnets",
      { "Ref": "Env" },
      "subList"
    ] }
  ]
}

YAML

Mappings: 
  AWSEnv2PublicSubnets:
    DEV:
      subList: subnet-1111,subnet-2222,subnet-3333

然后:

Subnets: !Split [",", !FindInMap [ AWSEnv2PublicSubnets, !Ref Env, subList] ]

我使用逗号 ( ,) 作为分隔符,但您可以使用任何您想要的字符,只要它不用作值的一部分。

答案2

我认为映射值支持List类型(至少截至目前)。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html

Mappings 部分由键名 Mappings 组成。mappings 中的键必须是文字字符串。值可以是StringList类型。

这是用 YAML 编写的示例。

Parameters:
  Env:
    Type: String
    AllowedValues: [dev, qa, prod]

Mappings:
  Environment:
    dev:
      Groups:
        - Developer
        - QA
    qa:
      Groups:
        - Developer
        - QA
    prod:
      Groups:
        - Operations

Resources:
  Policy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: iam-manage-role
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action:
              - iam:*Role*
            Resource: "*"
      Groups: !FindInMap [Environment, !Ref Env, Groups]

相关内容