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.
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.
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.
Name=tag:Name
– lisak
Apr 09 '15 at 14:40
--query 'Reservations[].Instances[].[InstanceId] which works for me
– jaygooby
Sep 05 '16 at 20:35
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 08 '17 at 19:04
'Name=instance-state-name,Values=running', as terminated instances show up in results for a while.
– yurez
Nov 23 '20 at 10:59
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
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'