Reputation: 1
I need to produce a list of AWS instances NOT containing a specific tag. These instances spread out across multiple accounts.
I've already got working the part that finds and outputs instances that meet my criteria, but I can't figure out how to get the account to output to the file as well.
Here's the script I have so far:
$aws_accounts =
<list of accounts>
foreach ($account in $aws_accounts) {
aws --profile $account ec2 describe-instances --output text --region us-east-1 --query 'Reservations[].Instances[?!not_null(Tags[?Key == `tag`].Value)] | [].[InstanceId, OwnerID]' >> c:\temp\file.csv
}
AWS docs lead me to believe I could just add "OwnerID" as seen here but it just gives me the instance ID and "None".
Admittedly I'm not particularly familiar with the syntax of queries. This page demonstrates what I'm trying to do, but I can't figure it out.
Upvotes: 0
Views: 436
Reputation: 4077
The ec2 describe-instances command return a list of Reservations. Each Reservation object has a Instances list. In this list is where you can find information about the EC2 instances like InstanceId, Image Id. But the OwnerId is not found on the instances list but in the Reservations list. This is why the command output shows Owner as None.
To get the Reservation OwnerId (that apply to all the instances for that Reservation), you need to specify the field one level above:
aws ec2 describe-instances --output text --region us-east-1 --query 'Reservations[].[OwnerId, Instances[?!not_null(Tags[?Key == `tag`].Value)].InstanceId]'
Reference:
Upvotes: 1