I'm currently using the following CLI command to get the instance PublicIPAddress and LaunchTime for a given instance Name tag, 'myInstanceName':
aws ec2 describe-instances --filters 'Name=tag:Name,Values=myInstanceName' \
--region us-east-1 \
--query 'Reservations[*].Instances[*].{PublicIpAddress: PublicIpAddress, LaunchTime: LaunchTime}'
This results in the following:
[
{
"LaunchTime": "2019-01-25T11:49:06.000Z",
"PublicIpAddress": "11.111.111.11"
}
]
This is fine, but if there are two instances with the same name I will get two results in my result JSON. I need to find a way to get the most recent instance for a given name.
Solution Update
This question is quite specific to EC2 instances. The issue can be resolved using two different methods, answered below:
Parsing Result with jq
Using JMESPath
Please see this related question for more general sorting by date with JMESPath, and for further reading.