Why does this command work?:
aws ec2 describe-instances \
--profile ike \
--query 'Reservations[].Instances[].[InstanceId,State.Name,InstanceType,PublicIpAddress, Tags[?Key==`Name`]|[0].Value, Tags[?Key==`Owner`]|[0].Value]' \
--filter 'Name=instance-state-name,Values=running' \
--output table
But not this command when I add a third tag?:
aws ec2 describe-instances \
--profile ike \
--query 'Reservations[].Instances[].[InstanceId,State.Name,InstanceType,PublicIpAddress, Tags[?Key==`Name`]|[0].Value, Tags[?Key==`Owner`]|[0].Value], Tags[?Key==`Schedule`]|[0].Value]' \
--filter 'Name=instance-state-name,Values=running' \
--output table
Error output:
Bad value for --query Reservations[].Instances[].[InstanceId,State.Name,InstanceType,PublicIpAddress, Tags[?Key==
Name
]|[0].Value, Tags[?Key==Owner
]|[0].Value], Tags[?Key==Schedule
]|[0].Value]: Unexpected token: ,: Parse error at column 140, token "," (COMMA), for expression: "Reservations[].Instances[].[InstanceId,State.Name,InstanceType,PublicIpAddress, Tags[?Key==Name
]|[0].Value, Tags[?Key==Owner
]|[0].Value], Tags[?Key==Schedule
]|[0].Value]"
Ii only works if I remove one of the tags.
I can change one of the TWO tags, and it works, but not with all three tags.
I'm still learning, obviously.
describe-instances
accepts a valid JMESPath expression as a value for the parameter--query
.Your first expression is a valid JMESPath which I will paste here with indentations to make it more legible:
It means:
Flatten all Instances (take every instance from every reservation and put them into a flat array).
Project each Instance to an array with the following members:
.InstanceId
.State.Name
.InstanceType
.PublicIpAddress
.Tags
array which has a{"key": "Name"}
element, and project its.Value
.Tags
array which has a{"key": "Owner"}
element, and project its.Value
Your second expression has an unbalanced bracket which you will easily find if you indent it properly.