0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

awscliでPublicIpAddressからPublicDnsNameを抽出する

Last updated at Posted at 2019-10-14

パブリックIPアドレスでfilterをかけたい

まずaws ec2 describe-instancesを叩くと以下のように表示される(関係ある項目以外は省いてます)

$ aws ec2 describe-instances --region ap-northeast-1

  "Instances": [
      {
          "PublicIpAddress": "52.198.249.123",
          "PrivateIpAddress": "172.31.40.37",
          "NetworkInterfaces": [
              {
                  "Association": {
                        "PublicIp": "52.198.249.123",
                        "PublicDnsName": "ec2-52-198-249-123.ap-northeast-1.compute.amazonaws.com"
                  },
・・・・・・・・・・・・・・・・

googleで調べてみると、出力結果に表示されるキーを小文字にして単語をハイフンで区切ると覚えておけば大丈夫らしい。
参考:AWS CLIのフィルタの使い方
たとえばPrivateIpAddressからPublicDnsNameを取得するときは、Name=private-ip-addressで抽出する。

aws ec2 describe-instances --filters "Name=private-ip-address,Values=172.31.40.37" --query 'Reservations[].Instances[].PublicDnsName' --region ap-northeast-1 
[
    "ec2-52-198-249-123.ap-northeast-1.compute.amazonaws.com"
]

public-ip-addressではNGになる

次にPublicIpAddressからPublicDnsNameを取得するために、以下のようにコマンドを打つとNGになった。

aws ec2 describe-instances --filters "Name=public-ip-address,Values=52.198.249.123" --query 'Reservations[].Instances[].PublicDnsName'  --region ap-northeast-1 

An error occurred (InvalidParameterValue) when calling the DescribeInstances operation: The filter 'public-ip-address' is invalid

調べてみると、name=network-interface.association.public-ipでfileterをかけると抽出できるらしい。
参考:EC2 describe instances, public-ip invalid filter #1299

aws ec2 describe-instances --filters Name=network-interface.association.public-ip,Values=52.198.249.123 --query 'Reservations[].Instances[].PublicDnsName' --region ap-northeast-1
[
    "ec2-52-198-249-123.ap-northeast-1.compute.amazonaws.com"
]

以下にfiltersで使用できる項目の一覧があります。
参考:AWS CLI 1.16.258 Command Reference describe-instances

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?