3

I have instances in AWS that have the same ReservationId (they were launched at the same time and they have AmiLaunchIndex of 0 thru x ). My goal is to produce text output with one line per instance, such as this. I added column headers for clarity.

OwnerId      ReservationId InstanceId  PrivateIpAddress AmiLaunchIndex
12345678910  r-poiu4567    i-asdf1234  10.0.0.1         0
12345678910  r-poiu4567    i-qwer4312  10.0.1.1         1
... etc ...

In the jmespath gitter channel, the map function was suggested as a way to accomplish this, but I can't figure out how to use the function. Any suggestions?

LHWizard
  • 2,121
  • 19
  • 30

5 Answers5

10
--query Reservations[*].Instances[*].[InstanceId] --output text

Just add the brakets

Adiii
  • 54,482
  • 7
  • 145
  • 148
martin
  • 101
  • 1
  • 2
  • This one does not work `aws eks list-clusters --region eu-central-1 --output=text --query "[clusters]"` – t7e Jul 06 '22 at 21:56
  • aws ec2 describe-instances --region {{ region }} --filters Name=instance-state-name,Values=running --query 'Reservations[*].Instances[].[{ID:InstanceId, Name:Tags[?Key==`Name`].Value,EnvID:Tags[?Key==`EnvID`].Value, EnvLayer:Tags[?Key==`EnvLayer`].Value, Type:InstanceType, AZ:Placement.AvailabilityZone, State:State.Name, IP:PrivateIpAddress | [0] }]' --output text | sed -e 's,ENVID,,g' -e 's,ENVLAYER,,g' -e 's,NAME,,g' | tr 't' 'n' | sed -e '/^$/d' | awk '{ORS=NR%8?", ":"n";print}' > /tmp/output-text.csv....I am using this in ansible playbook which is ran in jenkins and getting output – Ashish Karpe Dec 21 '22 at 05:10
  • in a sigle line as well as having \t, when I run this command on cli directly there is no \t but inside pipeline not able to remove this \t as well as print each instance on new line – Ashish Karpe Dec 21 '22 at 05:12
  • @t7e `aws eks list-clusters --query 'clusters[].[@]' --output text` – Darren Bishop Aug 11 '23 at 11:27
6

you would need to run the following command

aws ec2 describe-instances \
    --filters "Name=reservation-id,Values=r-poiu4567" 
    --query 'Reservations[*].{owner:OwnerId,ReservationId:ReservationId,instance:Instances[].InstanceId | [0]}' \ 
    --output text

You can add the other parameters you want

This will provide the desired output (all elements in one line) without the header as something like

i-08eec92943c9cc576 325979260958    r-0b13a131efa6b3af8
i-07a25c4ae7e6abecb 325979260958    r-05a51aefe5b72358d
....
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
4

Unfortunately, to do this right I think we'd need https://github.com/jmespath/jmespath.site/pull/6

In this particular case you can hack this result by using the owner in Network Interfaces, which is almost certain to be the same in practice:

Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, KeyName]

(use an object instead of an array if you want the column headings)

james.haggerty
  • 1,170
  • 6
  • 10
2
ec2 describe-instances --query 
'Reservations[*].
{
   id:ReservationId,
   requester:RequesterId,
   instance:Instances[].InstanceId |[0],
   lifecycle:Instances[].InstanceLifecycle | [0]
}
'
--output text

....worked for me.

vr00n
  • 550
  • 1
  • 4
  • 12
0

Finally, I was able to get my desired result by using the map function of jmespath as follows. Kudos to folks in the jmespath/chat channel on gitter.

aws ec2 describe-instances --query "map(&[], Reservations[].[OwnerId,ReservationId,Instances[].InstanceId | []])" --output text

An equivalent alternative expression is:

aws ec2 describe-instances --query "Reservations[].[OwnerId,ReservationId,Instances[].InstanceId | []] | map(&[], @)" --output text

see this jmespath issue and this one where this is discussed.

LHWizard
  • 2,121
  • 19
  • 30