Search code examples
amazon-web-servicesamazon-ec2aws-auto-scaling

give index number to ec2 instances of auto scaling group


i have an autoscaling group in AWS.

i want/need to be able to "number" those instances. i'll explain: lets say the ASG raise 5 instances. i want to be able to name those instance as follows: instance-name-0 instance-name-1 ... instance-name-4

is there any way to keep such numbering on the instances? (including when increasing/decreasing the desired count of the instances?)

and extra question of the same nature: how can i do that with a Spot Fleet? (give numbers to the instances, that will hold even after changes in the instances - including spot termination and change in scale)

i kinda believe that there is no such easy way to do what i want, or even hard way - without possible bugs and/or overhead, but i ask anyway - maybe i'll be surprised :)

thanks!


Solution

  • The simplest way to do this would be to create a EventBridge event rule that will apply whenever an instance is launched or terminated successfully. The event would look something like the below:

    {
      "source": [
        "aws.autoscaling"
      ],
      "detail-type": [
        "EC2 Instance Launch Successful",
        "EC2 Instance Terminate Successful"
      ],
      "detail": {
        "AutoScalingGroupName": [
          "YOUR-ASG-NAME-HERE"
        ]
      }
    }
    

    By adding a target of a Lambda function whenever this event triggers you would be able to review the instances that exist and then apply the logic as you deem fit.

    One thing I will mention about naming each instance is that it generally feeds into the concept of pets vs cattle.