0

This works:

aws ec2 describe-instances \
  --region eu-west-1 \
  --query Reservations[].Instances[].ImageId
[
    "ami-0123456789abcdefg",
    "ami-023456789bcdefghi",
    "ami-03456789cdefghijk"
]

But, when I try to add a filter it doesn't work anymore and returns empty arrays, plus I would only expect one result not many.

aws ec2 describe-instances \
  --region eu-west-1 \
  --query Reservations[].Instances[?ImageId=='"ami-0123456789abcdefg"'].ImageId
[
    [],
    [],
    []
]

What am I doing wrong?

1 Answer 1

1

Use backticks (`) instead of double quotes when using strings in the filter expression.

e.g.:

aws ec2 describe-instances \
  --query 'Reservations[].Instances[?ImageId==`ami-0123456789abcdefg`].ImageId'

Single quotes can be used, but I would recommend wrapping the entire query in those to keep the shell from interpreting any part of it.
Backticks are a alternative and allow for more human-readable queries.

2
  • Thanks @sborsky that gives me a better result. It prints out the AMI Id that matches the filter but then also still prints out a list of empty square brackets for instances that don't match the query.
    – Luke
    Commented May 2, 2024 at 8:43
  • Actually, adding --output text to the end of the aws cli command does what I need.
    – Luke
    Commented May 2, 2024 at 9:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.