amazon-ec2cloud-init

Is there anyway to access values from the EC2 tags in cloud-init


I know I can access the tags via the metadata and cli tools, but is there anyway to access them whilst running cloud-init? Ideally I'd like to look for a tag called hostname and use it to set the machine host name.

Thanks


Solution

  • This command can be run from userdata (or any time, really) to access the instance ID from the metadata, and use it to pull the tag called "hostname". You could assign this to a variable, or use the output to directly set the hostname of the instance.

    aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].Tags[?Key==`hostname`].Value' --instance-id `curl -s http://169.254.169.254/latest/meta-data/instance-id`