How to set the hostname of an AWS Lightsail VM

amazon-web-servicescloud-init

Reviewing cloudinit configs on my new Lightsail VM, it seems to be talking to a metadata API for settings like hostname. I don't see any lightsail UI component to set the hostname, but if I poke at the ec2 metadata from the VM, I see some user-data. Is there any way to write to it so that cloudinit won't override a hostname I set?

EDIT:

Looks like Lightsail has its own purpse for user-data:

$ cat /var/lib/cloud/instances/user-data.txt

#!/bin/sh
echo Lightsail: Starting Instance Initialization.

cat > /etc/ssh/lightsail_instance_ca.pub << EOF
ssh-rsa nope EOF
echo Lightsail: SSH CA Public Key created. 

echo >> /etc/ssh/sshd_config
echo 'TrustedUserCAKeys /etc/ssh/lightsail_instance_ca.pub' >> /etc/ssh/sshd_config
echo Lightsail: SSH CA Public Key registered.

service sshd restart
echo Lightsail: sshd restarted.

#cloud-config
hostname: pwnguin

So in cloud-init output log, it complains about hostname: command not found. I can probably put a shell command to set a hostname, but I might be out of luck on the cloudconfig route.

Best Answer

When launching your instance, you can pass the user-data containing your initial configuration (eg. hostname) as follows:

aws lightsail create-instances --instance-names <value> --availability-zone <value> --blueprint-id <value> --bundle-id <value> --user-data file:///full/path/to/myconfig

The content of myconfig file should be:

#cloud-config
hostname: test-vm