0

I am building a cloud formation template(YML format) for my ECS service and stuck in load-balancer target group, it was not able to attach to my ECS instance and trying to add Targets by referring this official AWS docs https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-targetgroup-targetdescription.html

Below is my target group and as I stop start(which terminates) my instance several times, my instance id will be changing all the time and will not be static, like VPC or subnet ids and how can I build the value dynamically in Id field of Targets ?

TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties: 
      Matcher: 
       HttpCode: "200"
      Name: "foo"
      Port: "8080"
      Protocol: "HTTP"
      Targets:
        Id: String // This I need to build dynamically
        Port: 8080
      TargetType: "instance"
      UnhealthyThresholdCount: 3
      VpcId: "vpc-79251d11"            

Note: I tried search for EC2 resources and found this https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ec2.html but it didn't help me. Also I am using ASG and LC to create my ECS instance.

  • Why would instance id change? Its not like a public IP address when it changes each time you restart the instance. – Marcin May 23 '20 at 11:24
  • @Marcin thanks for your comment, I am stopping instance everyday using ASG and want to build this cloud formation generic, lets suppose if I use it for another service or even want to use for another AWS account, having a hardcode value wont create issue in that? –  May 23 '20 at 11:27
  • If an instance is created by your CFN template, then you are not hardcoding it. You will just reference it using `!Ref`. If you deploy your template in the other account it will still work, because you are referencing instance here, not hard coding its id. – Marcin May 23 '20 at 11:30
  • @Marcin and I just checked by stopping the instance using ASG and then again starting it, it changed the instance id –  May 23 '20 at 11:30
  • I think we are taking about different IDs. Can you provide an example? – Marcin May 23 '20 at 11:31
  • @Marcin, it looks like `i-07fbdd511c57f06a3` and I guess this is what I need to provide in targets of target group? –  May 23 '20 at 11:32
  • That's correct, and it does not change when you stop an instance. Maybe you are talking about terminating it? – Marcin May 23 '20 at 11:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214475/discussion-between-es-enthu-and-marcin). –  May 23 '20 at 11:33

1 Answers1

0

Based on the discussion in chat.

Since the instances will be running in an Auto Scaling Group, there is no need to specify their ids directly in the TargetGroup resource of type AWS::ElasticLoadBalancingV2::TargetGroup.

Instead TargetGroup ARN should be provided in the AWS::AutoScaling::AutoScalingGroup resource. Specifically, TargetGroupARNs parameter:

A list of Amazon Resource Names (ARN) of target groups to associate with the Auto Scaling group. Instances are registered as targets in a target group, and traffic is routed to the target group.

For example, since your AWS::ElasticLoadBalancingV2::TargetGroup resource is called TargetGroup, when defining your ASG you would do the following (if same template file):

MyASG:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
      # only one parameter shown
      TargetGroupARNs: 
        - !Ref TargetGroup

Naturally, you would skip Targets parameter in your TargetGroup. This will make MyASG automatically register/de-register your instances from the TargetGroup.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Is there a way to this if my ASG already exists, and I am creating LB later as per requirement? I can do this from CLI, but unable to find a was from CF. Additionally, my ASG was not created using CF. – ghitesh Dec 27 '21 at 15:40