When working with AWS infrastructure, we often need to extract specific information about EC2 instances within a particular VPC. The standard describe-instances
command returns comprehensive data, but filtering for just the Name tag, private IP, and instance ID requires some finesse.
The most efficient approach combines AWS CLI's native query capability with JMESPath syntax:
aws ec2 describe-instances \
--filters Name=vpc-id,Values=vpc-e2f17e8b \
--query 'Reservations[].Instances[].[Tags[?Key==Name].Value | [0], PrivateIpAddress, InstanceId]' \
--output text
This command structure:
- Filters by VPC ID using
--filters
- Uses JMESPath to select exactly the fields we want
- Handles the Name tag extraction with array dereferencing
- Outputs in clean tab-delimited format
For more complex scenarios where you need jq's processing power:
aws ec2 describe-instances \
--filters Name=vpc-id,Values=vpc-e2f17e8b | \
jq -r '.Reservations[].Instances[] |
[.Tags[] | select(.Key == "Name").Value // "null",
.PrivateIpAddress,
.InstanceId] | @tsv'
To account for instances without Name tags:
aws ec2 describe-instances \
--filters Name=vpc-id,Values=vpc-e2f17e8b \
--query 'Reservations[].Instances[].[Tags[?Key==Name].Value | [0] || unnamed, PrivateIpAddress, InstanceId]' \
--output table
Choose from these output formats based on your needs:
--output text
(tab-delimited)--output table
(human-readable)--output json
(structured data)
When working with AWS infrastructure, we often need to extract specific information about EC2 instances within a particular VPC. The key challenge is filtering the output to include only the relevant fields: instance name (from Tags), private IP address, and instance ID.
Here's the foundational command that fetches all instances in a VPC:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b
The AWS CLI provides a powerful --query
parameter for JMESPath expressions. Here's how to get just the Name tags:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b \
--query 'Reservations[].Instances[].Tags[?Key==Name].Value[]'
To get all three required fields in a single command:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b \
--query 'Reservations[].Instances[].[InstanceId, PrivateIpAddress, Tags[?Key==Name].Value | [0]]' \
--output table
For more complex processing, we can pipe to jq:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | \
jq -r '.Reservations[].Instances[] | [.InstanceId, .PrivateIpAddress,
(.Tags[] | select(.Key == "Name").Value)] | @tsv'
Here's how to generate CSV output:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | \
jq -r '.Reservations[].Instances[] | [.InstanceId, .PrivateIpAddress,
(.Tags[]? | select(.Key == "Name").Value // "N/A")] | @csv'
Some instances might not have a Name tag. Here's a robust version:
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | \
jq -r '.Reservations[].Instances[] |
"\(.InstanceId)\t\(.PrivateIpAddress)\t\(.Tags[]? | select(.Key == "Name").Value // "Unnamed")"'
For large VPCs with many instances, consider adding --no-paginate
and using --max-items
if you only need a subset of results.