Passing Target Group Target as Parameters Cloud Formation

Viewed 375

I am trying to create a AWS::ElasticLoadBalancingV2::TargetGroup that accepts Target as parameters as template below :

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,!Split [",", !Ref TargetGroupIPs]]
      VpcId: !Ref VPC  

And the Parameter :

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

The problem here this is static to 2 IPs, I want it to accept x number of IPs. Ex :

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      

But I Get validation error: Target accepts List of Objects. How it can be done correctly ?

1 Answers

How it can be done correctly ?

It can't be done in plain CloudFormation (CFN). This would require a looping mechanism which does not exist in CFN.

To overcome this, you would have to either create CFN macro or custom resource. In both cases you would have to write a lambda function. The difference is that the function for macro would parse your original template (or fragment of it) and construct dynamically the Targets. In contrast, the function for the custom resource would fully create the target groups using AWS SDK taking your TargetGroupIPs as its input parameter.

Related