The following template fixes this issue. It uses the Name property of the TargetGroup resource. Whenever the Name property changes the TargetGroup needs to be recreated. So this template uses the unique identifier part of the load balancer name and adds a prefix to it. The TG1 prefix is there incase you have multiple target groups connected to this same load balancer. Each of these groups should use a different prefix so the Names don't conflict.
AWSTemplateFormatVersion: '2010-09-09' Description: Test of an issue with CloudFormation TargetGroups Parameters: ELBScheme: Type: String Default: internet-facing AllowedValues: [ 'internet-facing', 'internal'] Subnet1: Type: AWS::EC2::Subnet::Id Subnet2: Type: AWS::EC2::Subnet::Id VPC: Type: AWS::EC2::VPC::Id Resources: ELBv2: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Subnets: - !Ref Subnet1 - !Ref Subnet2 Scheme: !Ref ELBScheme ELBTargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: Name: !Join - '-' - - 'TG1' - !Select [ 2, !Split [ '-', !GetAtt ELBv2.LoadBalancerName]] Port: '80' Protocol: HTTP VpcId: !Ref VPC ELBListener80: Type: AWS::ElasticLoadBalancingV2::Listener Properties: DefaultActions: - TargetGroupArn: !Ref 'ELBTargetGroup' Type: forward LoadBalancerArn: !Ref 'ELBv2' Port: '80' Protocol: HTTP
Thanks! This helped immensely. In the end I just named my target group the same as my loadbalancer and that was enough to draw a dependency between em.
Malfunctioning Application LoadBalancer / Doesn't execute the rulesasked 8 months ago
Error: TargetGroup cannot be associated with more than one load balancerasked 5 years ago
CloudFormation stack in UPDATE_ROLLBACK_FAILED state with missing LoadBalancer listener rule ARNasked 2 months ago
Unable to update the default certificate on application load balancer using AWS cliasked 9 hours ago
The web acl is associated with the Application Load Balancer in the AWS region. After a few hours, it is found that the association has been cancelledasked 3 months ago
CloudFront - One or more of the CNAMEs you provided are already associated with a different resourceAccepted Answerasked 3 years ago
Certificate is associated with an arn:aws:cloudfront:::xxxxx/xxxx that cannot be foundasked 7 months ago
Bug: Cannot revert Load Balancer listener from HTTPS to HTTPasked 4 years ago
CloudFormation Script Fails on LoadBalancer with Message "ElasticLoadBalancerV2 LoadBalancer did not stabilize"Accepted Answerasked 9 months ago
Cannot select a target group with alb target type when creating an application load balancerAccepted Answerasked 3 months ago