1

Here's the relevant part of my Troposphere file:

LaunchConfiguration = t.add_resource(LaunchConfiguration(
    "LaunchConfigA",
    ImageId=UBUNTU_IMG,
    SecurityGroups=[Ref(SecurityGroup)],
    InstanceType="m3.medium",
    UserData=Base64(Join('', [
        "#cloud-boothook\n",
        "#!/bin/bash\n",
        "sudo hostname test\n",
        "sudo sh -c 'echo test > /etc/hostname'\n",
        "sudo sh -c 'echo 127.0.0.1 test >> /etc/hosts'\n",
        "sudo touch /var/log/TESTING\n"
    ])),
))

AutoScalingGroupA = t.add_resource(AutoScalingGroup(
    "GroupA",
    AvailabilityZones=GetAZs(Ref(AWS_REGION)),
    LaunchConfigurationName=Ref(LaunchConfiguration),
    MinSize="1",
    DesiredCapacity="2",
    MaxSize="2",
))

When I create a brand new CloudFormation stack from this template, the hostnames on the instances look like ip-172-XXX-XXX-XXX, the default.

I am certain that the script is running, because of my TESTING file:

atrose@ip-172-31-32-40:~$ ls -la /var/log/TESTING
-rw-r--r-- 1 root root 0 Jul 14 20:10 /var/log/TESTING

If I run the script manually, the hostname is properly set. Like so:

atrose@ip-172-31-32-40:~$ hostname
ip-172-31-32-40

atrose@ip-172-31-32-40:~$ sudo cat /var/lib/cloud/instance/user-data.txt
#cloud-boothook
#!/bin/bash
sudo hostname test
sudo sh -c 'echo test > /etc/hostname'
sudo sh -c 'echo 127.0.0.1 test >> /etc/hosts'

atrose@ip-172-31-32-40:~$ sudo bash /var/lib/cloud/instance/user-data.txt

atrose@ip-172-31-32-40:~$ hostname
test

How can I set hostnames on instances when they first boot into an ASG?

1 Answers1

-1

It looks like you're using an Ubuntu AMI, which means CloudInit should have a hostname parameter built into it, and you shouldn't need a shell script to do what you want. I'm going to guess that cloudinit itself is colliding with your script. You should check this out:

http://bazaar.launchpad.net/~cloud-init-dev/cloud-init/trunk/view/head:/doc/examples/cloud-config.txt#L540

Let me know if you have any questions about how to use that. Thanks!

phobologic
  • 424
  • 3
  • 5