【问题标题】:How to assign multiple Targets to a AWS Loadbalancer TargetGroup Target in a single line?如何在一行中将多个目标分配给 AWS 负载均衡器目标组目标?
【发布时间】:2026-01-17 13:05:02
【问题描述】:

如何将参数(CommaDelimitedList 或 String 类型)传递给单个 TargetGroup 的 Target:-Id: 属性并让它遍历并分配列表中的每个实例?

目前,我可以将一个或多个 Target-Servers 传递给 Target-Group,方法是将每个 Target 作为它自己的 -Id: 属性。下面的代码块 #1 可以工作,我怎样才能使代码块 #2 工作?

    MyTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
            Properties: 
             #[All the Target Group properties that aren't in question]
              Targets: 
                - Id: "i-000AWS001"
                Or 
                - Id: !Ref WebServer1
#Two Files:
#   resource.yml  -  Cloudformation yaml file
#   conf_resource.json  -  json paramaters file

# Parameter value in the Conf file
{
    "WebServers":"i-000AWS001,i-000AWS002,i-000AWS003,i-000AWS004,i-000AWS005"
}
# ..

#Target Group Resource
Parameters:
    WebServers:
        Type: CommaDelimitedList
        Default: "@@@WebServers@@@"
        Description: " A comma delimited list of AWS Ec2 Webserver instances"

Resources:
    MyAlb:
        ALBProperties:
            #[All the Application Load balancer properties that aren't in question]
        
    MyTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
            Properties: 
             #[All the Target Group properties that aren't in question]
              Targets: 
                - Id: !Split[ "," , !Ref WebServers ]
    #Also tried - Id: !Ref [ !Split[ "," , !Ref WebServers ] ]

AWS TargetGroup 文档: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html#cfn-elasticloadbalancingv2-targetgroup-targets

【问题讨论】:

    标签: amazon-web-services yaml amazon-cloudformation


    【解决方案1】:

    不能这样做,CloudFormation 不支持此类列表操作。

    但是,通常您所做的是将您的实例放在 AutoScaling Group 中。这样,该组将自动向您的平衡器注册实例,您不必担心单个实例 ID。

    【讨论】:

    • 我认为这可能是不可能的,但我想尝试一下,我将研究 Autoscaling 以获得未来的解决方案。非常感谢您的回复!
    • @JosephBourgoin 没问题。 ASG 应该走的路。 ASG+LB 配合得很好。
    最近更新 更多