Cannot create service due to Elastic Load Balancer Error

0

Hello,

I have a script that creates a cluster and a service which runs a docker image. I am able to create the cluster but the service fails where it tries to create an elastic load balancer. This used to work a year ago, our project was put on hold so I'm trying to get it to run again. Can you please help with this error? Thank you.

Resource handler returned message: "Invalid request provided: CreateService error: The target group with targetGroupArn arn:aws:elasticloadbalancing:ca-central-1:xxxxxxxxxxxxx:targetgroup/grpc1/70c4efc846481f4b does not have an associated load balancer. (Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException; Request ID: fe4999db-66de-4940-842b-61a29b5e2c20; Proxy: null)" (RequestToken: b4fc4fad-a284-fb32-87a9-2abc841d069b, HandlerErrorCode: InvalidRequest)

Here is the part of the yaml for the elb: Service: Type: AWS::ECS::Service Properties: ServiceName: !Ref 'ServiceName' Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']] DeploymentConfiguration: MaximumPercent: 100 MinimumHealthyPercent: 0 DesiredCount: !Ref 'DesiredCount' TaskDefinition: !Ref 'TaskDefinition' NetworkConfiguration: AwsvpcConfiguration: Subnets: - Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']] - Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']] SecurityGroups: [!Ref 'PublicLoadBalancerSG'] AssignPublicIp: 'DISABLED' LoadBalancers: - ContainerName: !Ref 'ServiceName' ContainerPort: !Ref 'ContainerPort' TargetGroupArn: !Ref 'TargetGroup'

TargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: HealthCheckProtocol: TCP HealthCheckIntervalSeconds: 10 HealthyThresholdCount: 2 UnhealthyThresholdCount: 2 Name: !Ref 'ServiceName' Port: !Ref 'ContainerPort' Protocol: TCP TargetType: ip VpcId: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'VPCId']]

PublicLoadBalancerListener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: DefaultActions: - TargetGroupArn: !Ref 'TargetGroup' Type: 'forward' LoadBalancerArn: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PublicLoadBalancer']] Port: !Ref ContainerPort Protocol: TCP

2 Answers
1

I would try a DependsOn in the Service resource waiting on the Listener to be created.

Service:
  Type: AWS::ECS::Service
  DependsOn: PublicLoadBalancerListener
  Properties:
    ServiceName: !Ref 'ServiceName'
  ...
profile pictureAWS
EXPERT
kentrad
answered a year ago
  • Can you show me how to write it in my yaml snippet?

0

The error message indicates that the target group associated with the service does not have an associated load balancer. Based on the code you provided, it seems like the service is attempting to create a new load balancer and associate it with the target group. However, it's possible that the load balancer was previously created and is no longer associated with the target group.

To fix the issue, you can try the following:

Check if a load balancer exists: You can check in the AWS Management Console to see if there is an existing load balancer. If there is, you can try to associate the load balancer with the target group.

Update the service to use an existing load balancer: If a load balancer already exists, you can modify the service YAML to use the existing load balancer instead of creating a new one. You can use the "LoadBalancerArn" property to specify the ARN of the existing load balancer.

Here is an example of how you can modify the YAML to use an existing load balancer:

Service:
Type: AWS::ECS::Service
Properties:
ServiceName: !Ref 'ServiceName'
Cluster: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']]
DeploymentConfiguration:
MaximumPercent: 100
MinimumHealthyPercent: 0
DesiredCount: !Ref 'DesiredCount'
TaskDefinition: !Ref 'TaskDefinition'
NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnetOne']]
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnetTwo']]
SecurityGroups: [!Ref 'PublicLoadBalancerSG']
AssignPublicIp: 'DISABLED'
LoadBalancers:
- ContainerName: !Ref 'ServiceName'
ContainerPort: !Ref 'ContainerPort'
TargetGroupArn: !Ref 'TargetGroup'

TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckProtocol: TCP
HealthCheckIntervalSeconds: 10
HealthyThresholdCount: 2
UnhealthyThresholdCount: 2
Name: !Ref 'ServiceName'
Port: !Ref 'ContainerPort'
Protocol: TCP
TargetType: ip
VpcId: Fn::ImportValue: !Join [':', [!Ref 'StackName', 'VPCId']]

PublicLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref 'TargetGroup'
Type: 'forward'
LoadBalancerArn: <existing load balancer ARN>
Port: !Ref ContainerPort
Protocol: TCP

Make sure to replace <existing load balancer ARN> with the ARN of the existing load balancer.

profile pictureAWS
answered a year ago
  • Thank you very much for your reply. I will try this, the only issue is that my script was working before and the aws specialist who once helped me get it working made one script that creates the cluster, then references the created items (such as the loadbalancer that was created right before the service attempted to get created. What is going wrong? Is it possible to reference the loadbalancer dynamically?

    Mary

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions