LoginSignup
18
12

More than 5 years have passed since last update.

EC2インスタンス内でリージョンを取得する方法

Last updated at Posted at 2014-09-08

はじめに

IAMロールをつかったEC2インスタンスは認証情報を設定することなくAWS CLIが利用できますが、リージョンだけは設定する必要があります。面倒ですね。ということでリージョンを取得する方法を見つけてきました。

コード

これだけです。

curl -s http://169.254.169.254/latest/meta-data/local-hostname | cut -d '.' -f2

実行した結果はこんなかんじです。

[ec2-user@ip-172-31-11-236 ~]$ curl -s http://169.254.169.254/latest/meta-data/local-hostname | cut -d '.' -f2
us-west-2
[ec2-user@ip-172-31-11-236 ~]$

と書いたところ、 masaomocさんより、こんなかんじでも取れますとのことでした。こちらのほうがスマートですね。

[ec2-user@ip-172-31-11-236 ~] $ curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/.$//g'
us-west-2 [ec2-user@ip-172-31-11-236 ~]$

masaomocさん(一応仮名ですよね?) ありがとうございました。

ではでは。

18
12
5

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
12