将目标组目标作为参数传递 Cloud Formation

问题描述

我正在尝试创建一个 AWS::ElasticLoadBalancingV2::TargetGroup,它接受​​ Target 作为参数作为下面的模板:

FETargetGroup:
    Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
    Properties:
      Name: !Sub 'targetgroup-ccp-fe-${EnvironmentName}'
      Port: 443
      Protocol: HTTPS
      targettype: ip
      Targets: 
        - Id: !Select[0,!Split [",",!Ref TargetGroupIPs]]
        - Id: !Select[1,!Ref TargetGroupIPs]]
      VpcId: !Ref VPC  

和参数:

  TargetGroupIPs:
   Description: IPs to be included in Target Group
   Type: String 

这里的问题是静态到 2 个 IP,我希望它接受 x 个 IP。例如:

FETargetGroup:
    Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
    Properties:
      Name: !Sub 'targetgroup-ccp-fe-${EnvironmentName}'
      Port: 443
      Protocol: HTTPS
      targettype: ip
      Targets: !Split [",!Ref TargetGroupIPs]
      VpcId: !Ref VPC      

但我收到验证错误目标接受对象列表。如何正确完成?

解决方法

如何正确完成?

不能简单地完成 CloudFormation (CFN)。这将需要一个循环机制,而这在 CFN 中是不存在的。

要克服这个问题,您必须创建 CFN macrocustom resource。在这两种情况下,您都必须编写一个 lambda 函数。不同之处在于宏函数将解析您的原始模板(或其片段)并动态构造 Targets。相比之下,自定义资源的函数将使用 AWS 开发工具包将您的 TargetGroupIPs 作为其输入参数完全创建目标组。