3

I'm trying to provision a new EC2 instance and attaching it to the existing targetGroup which I created earlier. I do have the targetGroupArn, and the new instance details. In all the documents, I could see the sample templates of creating a new instance and new targetgroup and referring the instance id to the creating targetgroup.

Is there any option to create only new instance and attaching it to the existing target group?

So that I can mention the targetGroupArn to which the new instances will be attaching. Below I've mentioned the sample templates, what I'm looking for.

{
  "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
  "Properties": {
    "TargetGroupArn": "arn:aws:elasticloadbalancing:ap-south-1:4444444444:targetgroup/MyTG/56abcccccccc56",
    "Targets": [
      {
        "Id": {
          "Ref": "ec2Instance1"
        }
      }
    ]
  }
}
Sonu
  • 179
  • 1
  • 11

1 Answers1

2

Is there any option to create only new instance and attaching it to the existing target group?

Yes, you can do that through a custom resources in the form of a lambda function. The function would use AWS SDK, such as boto3, to attach the instances to the existing target group.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thank you for your response. Here I dnt have the stack Id of the existing target group which I created earlier. I have only it's arn. So rather than updating the existing stack with the new instance details, I want an option to add the instances to the existing targetgroup with the targetgroup arn referance only... – Sonu Mar 24 '22 at 04:02
  • I've updated my comments, please check. – Sonu Mar 24 '22 at 04:06
  • @Sonu You can do whatever you want with `custom resources` because you program it yourself. Without `custom resources` you can't do what you want. – Marcin Mar 24 '22 at 04:09
  • Hi, Is there any option to do this by SDK approach instead of template? @Marcin – Sonu Mar 30 '22 at 08:42
  • @Sonu Yes, your custom resource could use any AWS SDK you want, including AWS CLI. I would recommend to try to write that, and make new question if you have issue. Also acceptance of my answer would be appreciated if it was helpful. – Marcin Mar 30 '22 at 08:43
  • 1
    With the custom resources, I'm able to do this. – Sonu Apr 01 '22 at 08:12