18
16

More than 5 years have passed since last update.

AWS CLI の --query で複数のタグでインスタンスを検索

Posted at

例えば Name=hoge かつ Role=fuga のインスタンスの ID と AZ を表示する場合。

見やすくするために無理やり改行を入れています。

aws ec2 describe-instances --query 'Reservations[].Instances[] '\
'| [? Tags[?Key==`Name`][] | [?Value==`hoge`] ] '\
'| [? Tags[?Key==`Role`][] | [?Value==`fuga`] ] '\
'| [][InstanceId,Placement.AvailabilityZone]'

あるいは、次のように書いたほうが見やすいでしょうか。

aws ec2 describe-instances --query "$(echo 'Reservations[].Instances[]
  | [? Tags[?Key==`Name`][] | [?Value==`hoge`] ]
  | [? Tags[?Key==`Role`][] | [?Value==`fuga`] ]
  | [][InstanceId,Placement.AvailabilityZone]
')"

タグでの検索なら filter でもできるので filter の方が簡単です。

aws ec2 describe-instances --filter \
  'Name=tag:Name,Values=hoge' \
  'Name=tag:Role,Values=fuga' \
  --query 'Reservations[].Instances[][InstanceId,Placement.AvailabilityZone]'
18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16