株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

AWSEC2の名前のみを取得したい場合は、Tags.Nameを取得しないといけない。
InstanceNameのような都合のよいものはなく、Nameタグになっているので工夫しないといけない。

aws ec2 describe-instances | jq -r '.[][].Instances[].Tags[]'
{
  "Key": "Name",
  "Value": "Server1"
}
{
  "Key": "Name",
  "Value": "Server2"
}
{
  "Key": "Kind",
  "Value": "EC2"
}

selectを使って、キー名.KeyがNameの場合のみをフィルタすることができる

aws ec2 describe-instances | jq -r '.[][].Instances[].Tags[] | select( .Key == "Name")'

このように出力される

{
  "Key": "Name",
  "Value": "Server1"
}
{
  "Key": "Name",
  "Value": "Server2"
}

ちなみに値のみを出力したい場合は|で区切ったあとに、.Valueを指定すればOK

aws ec2 describe-instances | jq -r '.[][].Instances[].Tags[] | select( .Key == "Name") | .Value'
Server1
Server2

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)