Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoscaling group hostnames & cloud-init

From a brief search - there does not seem to be a method to set dynamic hostnames for members of an autoscaling group. The functionality exists within OpenStack Heat using index - but I cannot find anything on doing so with AWS autoscaling groups.

For example, using OpenStack Heat - nodes are automatically given a hostname based on the number of nodes in the autoscaling group:

  instance_group:
    type: OS::Heat::ResourceGroup
    properties:
      count: { get_param: instance_number }
      resource_def:
        type: OS::Nova::Server
        properties:
          name: instance%index%

Would give me the following if I were to have 3 instances in the autoscaling group

instance0
instance1
instance2

Is there a similar method I can use with the AWS autoscaling groups launch configuration and or cloud-init?

like image 382
oldredmule Avatar asked Nov 09 '22 18:11

oldredmule


1 Answers

I've found a solution that works pretty well, if you stick to some not-unreasonable conventions.

Every kind of EC2 instance that I launch, whether there are N servers of this kind in an autoscaling group or it's stand-alone instance, I create an Instance Profile for it. This is a good idea anyway in my experience, even if you don't need the instance to access any aws services it doesn't hurt to have a role/profile with empty permissions, it makes it that much easier to give it access to an s3 bucket or whatever else in the future if you need to.

Then at server launch in the user_data script (or your configuration management tool if you're using something like puppet or ansible), I query the instance profile name from the metadata service and append something unique to each server like the private ip and set that as the hostname.

You'll end up with hostnames like webserver-10-0-12-58 which is both human readable and unique to each server.

(The downside of this vs incrementing integers is that these aren't predictable, and can't be used to set up unique behavior for a single server. For example if you had webserver-{0-8} and needed to run some process on exactly one server, you could use logic like if hostname == webserver-0 then run_thing.)

like image 132
danny Avatar answered Nov 15 '22 11:11

danny