Skip to content

Instantly share code, notes, and snippets.

@TomA-R
Forked from samkeen/ec2-instance-subnet-id.md
Created November 15, 2017 05:02
Show Gist options
  • Save TomA-R/30b674c46a4b4f26b4394ddcde6d4143 to your computer and use it in GitHub Desktop.
Save TomA-R/30b674c46a4b4f26b4394ddcde6d4143 to your computer and use it in GitHub Desktop.
Get the subnet and VPC id of a running EC2 instance

First get the Interface mac address, then use that to run the full command

INTERFACE=$(curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/)
SUBNET_ID=$(curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/${INTERFACE}/subnet-id)
VPC_ID=$(curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/${INTERFACE}/vpc-id)

echo $SUBNET_ID

Not sure this will work if you have more than one network interface

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