LoginSignup
0
0

More than 5 years have passed since last update.

EC2で自身のPublicIPを知る方法

Posted at

Motivation

オートメーションとかでEC2自身のIPをhostnameに設定できたらいいなー。と思って調べたけど、環境変数にもいないし、どうやって調べたらいいのかなー。と思ったらどうやらメタデータを取得するAPIがあるようでした。

EC2の中でPublicIPを取得する

EC2にsshして、

curl http://169.254.169.254/latest/meta-data/public-ipv4

これで取得できます。
各インスタンスで起動時のshellでこれを使ってhostnameを変更するとか色々使えそうですね。

ちなみに、他のメタデータも色々取れます。

[hoge ~]$ curl http://169.254.169.254/latest/meta-data/
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
iam/
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
product-codes
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/

以上、快適なEC2生活を。

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