I am trying to write a JMSEpath query to return a list of Instances that's 90days old returning instances ID number, Launch Tine and the Instance Tag name if not null.
I can get the instance ID and the launch time, however when I add the Tags key and values, it only returns the tags name value. See below.
aws ec2 describe-instances \
--query "Reservations[].Instances[?LaunchTime<='$(date --date='-90 days' '+%Y-%m-%d')']" | \
jq .[][] | jq -r '. | .InstanceId + " " + .Name + " " + .LaunchTime'
i-0d1b4afee437442a4 2023-02-25T03:17:00+00:00
i-0e2464a16b9e6b1af 2023-10-19T18:44:34+00:00
aws ec2 describe-instances \
--instance-ids i-0d1b4afee437442a4 \
--query "Reservations[*].Instances[?LaunchTime<='$(date --date='-90 days' '+%Y-%m-%d')'].[ Tags[?Key=='Name'].Value][0][0]"
[
[
"MDM-server-01"
]
]
Assuming you are really looking for the argument to pass to jq that will provide you the InstanceName, LaunchTime and the Tags, your first aws command provides everything you need, you just need to adjust your jq command to extract those values. The following works for me:
Note that I put a comma (",") between the fields, and an equal ("=") between the key and its value.