The instance's id is random. You find it by its Name tag.
Filters
aws ec2 describe-instances --filters Name=tag:Name,Values=web --query "Reservations[].Instances[].[InstanceId,State.Name,PrivateIpAddress,PublicIpAddress]" --output table--filters asks EC2 to send back only the instances that match; tag:Name is the filter for the Name tag. The query then picks four fields from each one. Its path starts with Reservations[].Instances[] because every launch is a reservation with its instances inside.
Two addresses
The launch named no network, so the instance went into the region's default VPC: a ready-made network every region comes with (Chapter 4 builds your own). It gave the instance two addresses:
| Address | Example | Used from |
|---|---|---|
| private IP | 172.31.5.12 | inside the network only |
| public IP | 54.23.117.9 | the internet |
The id in a variable
Most EC2 commands take the instance's id. Find it once by name and keep it:
ID=$(aws ec2 describe-instances --filters Name=tag:Name,Values=web --query "Reservations[0].Instances[0].InstanceId" --output text)[0] takes the first reservation and its first instance. The same pattern with PublicIpAddress keeps the address in IP.
More tags
aws ec2 create-tags --resources $ID --tags Key=Env,Value=lab Key=Owner,Value=danacreate-tags adds tags to anything with an id. Tags are free and searchable, and later chapters split the bill by them.