Skip to content

Instantly share code, notes, and snippets.

@mojodna
Last active December 15, 2015 15:29
Show Gist options
  • Save mojodna/5282595 to your computer and use it in GitHub Desktop.
Save mojodna/5282595 to your computer and use it in GitHub Desktop.
EC2 metadata
# Get the EC2 region from an instance, because ec2metadata only includes the availability zone (which is close, but not close enough).
REGION=$(curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}')
# extended ec2metadata, including tags (requires an IAM role with ec2:DescribeTags)
list_instances -r $REGION -H T:Name,ID,Hostname,State -f "instance-id=$(ec2metadata --instance-id)"
export HOST=$(list_instances -r $REGION -H T:Name,ID,Hostname,State -f "instance-id=$(ec2metadata --instance-id)" | grep running | awk '{print $1}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment