2

I can see my instance with:

$ aws ec2 describe-instances --output text
RESERVATIONS    193693970645    r-06e25c9702ca1a586
INSTANCES       0       x86_64          False   True    xen     ami-00c03f7f7f2ec15c3   i-03006b8712ac593f9     t2.micro        mdaws   2019-10-11T12:08:56.000Z                    /dev/xvda       ebs     User initiated (2019-10-11 12:51:09 GMT)        hvm
CAPACITYRESERVATIONSPECIFICATION        open
CPUOPTIONS      1       1
HIBERNATIONOPTIONS      False
MONITORING      disabled
PLACEMENT       us-east-2c              default
STATE   48      terminated
STATEREASON     Client.UserInitiatedShutdown    Client.UserInitiatedShutdown: User initiated shutdown

but how can I pick out the termination code (48) or description (terminated) when I just want a few fields ?

I have tried a few things including:

$ aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId, ImageId, State]" --output text

'str' object has no attribute 'items'

$ aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId, ImageId, State[*]]" --output text
i-03006b8712ac593f9     ami-00c03f7f7f2ec15c3   None

$ aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId, ImageId, State[*].[Code]]" --output text
i-03006b8712ac593f9     ami-00c03f7f7f2ec15c3   None

$ aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId, ImageId, State[*].[Code[*]]]" --output text
i-03006b8712ac593f9     ami-00c03f7f7f2ec15c3   None

1 Answer 1

5

The format is [InstanceId, ImageId, State.Code, State.Name], i.e.

$ aws ec2 describe-instances \
--query "Reservations[*].Instances[*].[InstanceId, ImageId, State.code, State.Name]" --o text

Output in this example:

i-03006b8712ac593f9     ami-00c03f7f7f2ec15c3   48    terminated
Sign up to request clarification or add additional context in comments.

2 Comments

I think it's not bad to accept your own answer so the community will not visit your question. answered but not accepted normally mean that question still looking for answer as I am here because of this.
Yes. However I have to wait two days before being allowed to accept my own answer (SO limit). Still waiting for that time period to elapse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.