简体   繁体   English

传递 List 类型的参数<aws::ec2::subnet::id>到嵌套的 CloudFormation 模板</aws::ec2::subnet::id>

[英]Passing parameters of type List<AWS::EC2::Subnet::Id> to nested CloudFormation template

I'm trying to nest a CloudFormation template into another by using the AWS::CloudFormation::Stack resource type.我正在尝试使用AWS::CloudFormation::Stack资源类型将一个 CloudFormation 模板嵌套到另一个模板中。 The nested template has a parameter of type List<AWS::EC2::Su.net::Id> .嵌套模板有一个类型为List<AWS::EC2::Su.net::Id>的参数。

Individually, the nested template runs just fine.单独地,嵌套模板运行得很好。 But when embedding AWS Console says Encountered unsupported property ELBSu.netList .但是当嵌入 AWS 控制台时,会显示 Encountered unsupported property ELBSu.netList

Changing the parameter's type to String/CommaSeparated list seems to be a workaround, as dicussed here but I'd loose the fancy UI when creating the template interactivly via AWS Console.将参数的类型更改为 String/CommaSeparated 列表似乎是一种解决方法,正如此处讨论的那样,但在通过 AWS 控制台交互式创建模板时,我会放弃花哨的 UI。

Do you have any idea how to pass the list of su.net ids as a paramter?你知道如何将 su.net id 列表作为参数传递吗?

This is the embedded template:这是嵌入式模板:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters" : {
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources" : {
        "ELB" : {
            "Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
            "Properties" : {
                "Subnets": { "Ref": "ELBSubnetList" },
                "CrossZone" : "true",
                "SecurityGroups": { "Ref": "ELBSecurityGroupList" },
                "LBCookieStickinessPolicy" : [ {
                    "PolicyName" : "CookieBasedPolicy",
                    "CookieExpirationPeriod" : "30"
                }],
                "Listeners" : [ {
                    "LoadBalancerPort" : "80",
                    "InstancePort" : "80",
                    "Protocol" : "HTTP",
                    "PolicyNames" : [ "CookieBasedPolicy" ]
                } ],
                "HealthCheck" : {
                    "Target" : "HTTP:80/wordpress/wp-admin/install.php",
                    "HealthyThreshold" : "2",
                    "UnhealthyThreshold" : "5",
                    "Interval" : "10",
                    "Timeout" : "5"
                }
            }
        }
    }
}

And the template that embedds:以及嵌入的模板:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters": {
        "ChildTemplate": {
            "Type": "String",
            "Default": "https://s3.eu-central-1.amazonaws.com/cf-templates-xxxxxxxxxxx-eu-central-1/sample_child.template"
        },
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources": {
        "Child": {
            "Type": "AWS::CloudFormation::Stack",
            "Properties": {
                "TemplateURL": { "Ref": "ChildTemplate" },
                "Parameters": {
                    "ELBSubnetList": { "Ref": "ELBSubnetList" },
                    "ELBSecurityGroupList": { "Ref": "ELBSecurityGroupList" }
                }
            }
        }
    }
}

Successfully built in YAML using the following excerpt:使用以下摘录在 YAML 中成功构建:

Parameters:
  pSubnetIDs:
    Description: The array of Subnet IDs for the Subnet group
    Type: List<AWS::EC2::Subnet::Id>
Resources:
  rDBSubnetGroup:
    Type: "AWS::RDS::DBSubnetGroup"
    Properties: 
      DBSubnetGroupDescription: The subnet group for the RDS instance
      SubnetIds: !Ref pSubnetIDs

I tried a bunch of variations of !Join and !Ref with no success.我尝试了一堆 !Join 和 !Ref 的变体,但没有成功。 Turns out it is just straightforward !Ref of the list.事实证明它很简单! Ref 列表。

In YAML you need to "split" the Subnets by using Select.在 YAML 中,您需要使用 Select 来“拆分”子网。 For example with two subnets :例如有两个子网:

!Join [",", [!Select [0, !Ref Subnets], !Select [1, !Ref Subnets]]]

Lists can be converted into Strings and vice versa.列表可以转换为字符串,反之亦然。 So the working invocation is所以工作调用是

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters": {
        "ChildTemplate": {
            "Type": "String",
            "Default": "https://s3.eu-central-1.amazonaws.com/cf-templates-xxxxxxxxxxx-eu-central-1/sample_child.template"
        },
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources": {
        "Child": {
            "Type": "AWS::CloudFormation::Stack",
            "Properties": {
                "TemplateURL": { "Ref": "ChildTemplate" },
                "Parameters": {
                    "ELBSubnetList": {"Fn::Join": [",", { "Ref": "ELBSubnetList" }]},
                    "ELBSecurityGroupList": {"Fn::Join": [",", { "Ref": "ELBSecurityGroupList" }]}
                }
            }
        }
    }
}

To convert a list of SubnetIds to a list of Strings, use a combination of both JOIN and SPLIT .要将 SubnetId 列表转换为字符串列表,请结合使用JOINSPLIT

TLDR; TLDR;
In YAML, add在 YAML 中,添加

!Split [',', !Join [',', !Ref SubnetIds]]

The full answer is 2 parts.完整的答案是 2 部分。

Part 1: JOIN第 1 部分:加入

Here SubnetIds is a list of type Subnet.Id .这里SubnetIds是类型为Subnet.Id的列表。 JOIN will combine all IDs to be one string. JOIN会将所有 ID 组合为一个字符串。 For example, a list of subnet ids JOIN ed with , as the delimiter will be as follows:例如,子网 id 列表JOIN ed with ,作为分隔符将如下所示:

[abc, def, hij] => "abc,def,hij" . [abc, def, hij] => "abc,def,hij"

Part 2: SPLIT第 2 部分:拆分

Now let's take the output from part 1, and SPLIT on the delimiter , .现在让我们获取第 1 部分的输出,并在分隔符,上进行SPLIT

"abc,def,hij" => ["abc", "def", "hij"] "abc,def,hij" => ["abc", "def", "hij"]


Here is an example of my use case with creating a Scheduled Task:这是我创建计划任务的用例示例:

AWSTemplateFormatVersion: '2010-09-09'
Parameters
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
    Description: Select at least two subnets in your selected VPC.

  ScheduleTask:
    Type: AWS::Events::Rule
    Properties:
      Description: !Sub 'Trigger Sitemap Generation according to the specified schedule'
      ScheduleExpression: !Ref CronExpression
      State: ENABLED
      Targets:
      - Id: 'targetId'
        Arn: !GetAtt ECSCluster.Arn
        RoleArn: !GetAtt ECSEventsRole.Arn
        EcsParameters:
          TaskDefinitionArn: !Ref TaskDefinition
          TaskCount: 1
          LaunchType: 'FARGATE'
          PlatformVersion: 'LATEST'
          NetworkConfiguration:
            AwsVpcConfiguration:
              AssignPublicIp: ENABLED
              SecurityGroups:
                - !Ref SecurityGroup
              Subnets: !Split [',', !Join [',', !Ref SubnetIds]]

Maybe help to another person: but for me works this:也许对另一个人有帮助:但对我来说是这样的:

In child template:在子模板中:

SubnetIds:
    Description: Choose which subnets should be deployed to
    Type: List<AWS::EC2::Subnet::Id>

but in the parent template但在父模板中

    SubnetIds:
        Description: Choose which subnets should be deployed to
        Type: CommaDelimitedList
        
        
    Stack:
      Type: AWS::CloudFormation::Stack
      Properties:
        TemplateURL: {s3}
        Parameters:
            SubnetIds: !Join [',', !Ref SubnetIds]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CloudFormation,传递一个列表<AWS::EC2::Subnet::Id>参数作为逗号分隔的字符串? - CloudFormation, passing a List<AWS::EC2::Subnet::Id> parameter as a comma separated string? 在AWS CloudFormation模板中,如何将EC2实例放置在Spot Fleet确定的子网中? - In an AWS CloudFormation template, how can I place an EC2 instance in the subnet determined by a Spot Fleet? 用于创建EC2的AWS CloudFormation模板-列出IAM角色 - AWS CloudFormation Template to Create EC2 - List IAM Roles 具有VPC,子网和安全组选择的EC2的CloudFormation模板(JSON) - CloudFormation Template (JSON) for EC2 with VPC, Subnet & Security Group Choices 将 EC2 实例放置在特定子网中时出现 CloudFormation 模板错误 - CloudFormation Template Error when placing EC2 Instance in particular subnet AWS CloudFormation EC2模板失败 - AWS CloudFormation EC2 Template getting failed Terraform AWS subnet_id列表被视为ec2实例的单值字符串 - Terraform AWS subnet_id list is treated as single value string for ec2 instance 在 AWS Cloudformation 的 EC2 UserData 中使用 Parameter Store 参数 - Use Parameter Store parameters in EC2 UserData in AWS Cloudformation AWS Cloudformation 模板 EC2 角色/策略循环依赖 - AWS Cloudformation template EC2 Role/Policy circular dependency 如何在 Cloudformation 模板上为 AWS EC2 实例设置 MemorySize? - How to set MemorySize for AWS EC2 Instance on Cloudformation Template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM