LoginSignup
1
0

More than 3 years have passed since last update.

aws ssm describe-instance-information の ComputerNameをいい感じにする

Posted at

aws ssm describe-instance-information の出力項目に ComputerName というのがある。
これは、公式ドキュメントによると

The fully qualified host name of the managed instance.

とのことで、FQDNが表示されるとのこと。
ところが、自分の現場ではこれの表示がlocalhostだったりしてうまく表示できていなかった。

$ aws ssm describe-instance-information --output table --query "InstanceInformationList[].[InstanceId,ComputerName]" --filters "Key=InstanceIds,Values=i-01234567890123456"
----------------------------------------------
|  DescribeInstanceInformation               |
+----------------------+---------------------+
|  i-01234567890123456 |  localhost          |  
+----------------------+---------------------+

これを解決した方法。

hostname が適切でない場合

$ hostname 
ip-172-31-15-172

この様になっている場合は、ホスト名を変更する。
Ubuntu 18.04の場合は以下。

sudo hostnamectl set-hostname awesome-production
$ hostname 
awesome-production
$ hostname -f
awesome-production

となればOK。

$ aws ssm describe-instance-information --output table --query "InstanceInformationList[].[InstanceId,ComputerName]" --filters "Key=InstanceIds,Values=i-01234567890123456"
----------------------------------------------
|  DescribeInstanceInformation               |
+----------------------+---------------------+
|  i-01234567890123456 |  awesome-production |  
+----------------------+---------------------+

ただ、自分の環境ではこれでも解決しないケースがあった。
原因は /etc/hosts の設定だった。

/etc/hosts が適切でない場合

まず、ComputerName に表示される内容は、自分の環境の複数インスタンスで確認した限りでは
hostname -f の出力と一致していた。
(ほんとはソースコードまで追っかけて裏取りたかったが、時間無く断念)

ComputerNamelocalhost になっていたインスタンスの /etc/hosts をみると
以下のようになっていた。

127.0.0.1 localhost awesome-production

いろいろ試したところ、localhost が単独行でないと、ダメらしく、以下のように修正した。

127.0.0.1 localhost 
127.0.0.1 awesome-production

こう修正したうえで hostname -f をすると、hostname と一致した。
(なぜこうなるか、までの理由はわからなかった)

$ hostname 
awesome-production
$ hostname -f
awesome-production
$ aws ssm describe-instance-information --output table --query "InstanceInformationList[].[InstanceId,ComputerName]" --filters "Key=InstanceIds,Values=i-01234567890123456"
----------------------------------------------
|  DescribeInstanceInformation               |
+----------------------+---------------------+
|  i-01234567890123456 |  awesome-production |  
+----------------------+---------------------+

ちなみに、上記hostname等修正後、 aws ssm describe-instance-information の出力に修正が反映されるまでは、少しタイムラグがある。
体感で数分程度。
おそらく SSM Agentが情報を取りに行くタイミングがあるのだろう。

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