65

I have an instance named dev-server-03. Now how can I search all dev-server-* instances from command line?

I am using aws cli tool.

2
  • This depends on how you've named the instance. Did you set the hostname inside the OS, on the AWS console or both? Dec 10, 2013 at 13:16
  • Yeah inside the OS hostname is set to same Name. Dec 11, 2013 at 5:29

3 Answers 3

104

Assuming that you are using the convention of putting the name of the instance in a tag with the key of "Name" (this is what the AWS Console does when you enter a name), then you can use the --filters option to list those instances with aws-cli:

aws ec2 describe-instances --filters 'Name=tag:Name,Values=dev-server-*'

If you just wanted the instance ids of those instances, you could use:

aws ec2 describe-instances --filters 'Name=tag:Name,Values=dev-server-*' \
  --output text --query 'Reservations[*].Instances[*].InstanceId'

Note: --query may require a recent version of aws-cli but it's worth getting.

5
  • 2
    Omg it took me almost half an hour to figure this out... how weird is this : Name=tag:Name
    – lisak
    Apr 9, 2015 at 14:40
  • 2
    Thanks Eric - I was getting multiple instance IDs on the same line, using your query, but github.com/aws/aws-cli/issues/914#issuecomment-56210312 suggested --query 'Reservations[].Instances[].[InstanceId] which works for me
    – jaygooby
    Sep 5, 2016 at 20:35
  • 5
    FWIW, the jq tool can be used to get similar results to aws --query like this: aws ec2 describe-instances | jq '.Reservations[].Instances[].PrivateIpAddress' --note the preceding . dot.
    – MarkHu
    Nov 8, 2017 at 19:04
  • can you make describe-instances filters case insensitive?
    – red888
    Nov 6, 2018 at 14:59
  • 1
    Also worth adding filter 'Name=instance-state-name,Values=running', as terminated instances show up in results for a while.
    – yurez
    Nov 23, 2020 at 10:59
14

You can further filter with name, instance id and private ip with below,

aws ec2 describe-instances --filters "Name=tag:Name,Values=*myinstance*" --output json --query 'Reservations[*].Instances[*].[PrivateIpAddress,InstanceId,Tags[?Key==`Name`].Value]' --region us-east-1
3
  • This filtering by Key name was exactly what I was looking for, thank you!
    – ingernet
    Dec 13, 2019 at 4:43
  • Specifying region was exactly thing without which command refused to work. Now it works, thank you! May 12, 2020 at 8:00
  • surrounding with * worked for me Jun 1, 2021 at 3:37
1

If you're using jq, you can achieve this with:

aws ec2 describe-instances | 
jq -r '.Reservations[].Instances[] | 
select(.Tags[].Value | startswith("dev-server-"))'

You can append additional filters to jq for more specific results, such as:

... startswith("dev-server-")) | .PublicDnsName'

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.