I want to extract the instance ID and the tags from the result of a command ec2-describe-instances and want to store the result in a text file. The result set gives :
But i want the tags owner and cost.centre also to be fetched Kindly guide me how to do that
Here is another way to do without using jq and other parsing tools.
This should work:
TL;DR: The way AWS does tags is a living nightmare, even with
jq
Also, use AWS CLI, not old, unsupported tools.
this will help to find instance-id
$ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-xxx | awk '{ print $8 }' | sort -n | grep "i-"
i-4115d38c
i-5d534697
i-6e679a45
i-7a659851
i-8d6bae40
i-cd6f9000
i-d264ad1e
i-d5888618
i-e2332e2e
ps considering that you already configured / run "aws configure"
If I understand the question correctly, I think you just need to add expressions to your 2nd grep:
This is going to be unnecessarily complicated to do in a shell script. Here are some suggestion:
ec2cli
. Don't use that. UseAWS-CLI
instead. Because parsing the output inec2cli
is a pain. WhereasAWS-CLI
provides output inJSON
, it is way more easier to parse. Also, AWS is going to supportAWS-CLI
only henceforth.AWs-CLI
commands via a perl script and then capture the output in a hash.Perl
is very powerful for handling such data structure.Bottom line is, you need to capture the tags in a hash to make your life easier. Ans this becomes more and more prominent when you have multiple tags.
Using awk