Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save evandhoffman/9513198 to your computer and use it in GitHub Desktop.

Select an option

Save evandhoffman/9513198 to your computer and use it in GitHub Desktop.
Awful one-liner
root@ip-10-14-10-142:~# export INSTANCE_ID=`ec2metadata --instance-id`
root@ip-10-14-10-142:~# HOSTNAME=`aws ec2 describe-tags --region us-east-1 --filter "Name=resource-id,Values=$INSTANCE_ID" 'Name=key,Values=Name' | perl -ne 'chomp; my @a = split(/[ ]+/); next unless $a[1] eq "\"Value\":"; $a[2] =~ s/\"([\d\w\.\-]+)\".+/$1/; print "$a[2]\n"'`
root@ip-10-14-10-142:~# hostname $HOSTNAME
root@ip-10-14-10-142:~# echo $HOSTNAME
www.example.com
root@ip-10-14-10-142:~# echo $HOSTNAME > /etc/hostname
@evandhoffman
Copy link
Author

This presumes you've given the instance a tag of Name=.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment