[小ネタ] EC2インスタンスメタデータを簡単に確認する

2018.11.20

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは、望月です。

EC2インスタンスの情報をEC2自身から取得するのに便利なインスタンスメタデータですが、長いURIにアクセスするのがなかなかくせ者だったりします。

そこで、簡単にインスタンスメタデータを取得できる方法があるのでやってみました。

やってみた

EC2インスタンスのメタデータを取得する方法として、下記URIにアクセスするというのがあります。 curl や wget などを利用して、欲しい情報のURIにアクセスし、情報取得をします。

http://169.254.169.254/latest/meta-data/

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

インスタンスメタデータとユーザーデータ

大変便利な機能ですがURIを打ち込むには長く、コピペなどで対応することも多いかと思います。

それでは、簡単にメタデータを取得してみたいと思います。ドドンッ。

$ ec2-metadata --help
ec2-metadata v0.1.2
Use to retrieve EC2 instance metadata from within a running EC2 instance.
e.g. to retrieve instance id: ec2-metadata -i
to retrieve ami id: ec2-metadata -a
to get help: ec2-metadata --help
For more information on Amazon EC2 instance meta-data, refer to the documentation at
http://docs.amazonwebservices.com/AWSEC2/2008-05-05/DeveloperGuide/AESDG-chapter-instancedata.html

Usage: ec2-metadataOptions:--all Show all metadata information for this host (also default).-a/--ami-id The AMI ID used to launch this instance-l/--ami-launch-index The index of this instance in the reservation (per AMI).-m/--ami-manifest-path The manifest path of the AMI with which the instance was launched.-n/--ancestor-ami-ids The AMI IDs of any instances that were rebundled to create this AMI.-b/--block-device-mapping Defines native device names to use when exposing virtual devices.-i/--instance-id The ID of this instance-t/--instance-type The type of instance to launch. For more information, see Instance Types.-h/--local-hostname The local hostname of the instance.-o/--local-ipv4 Public IP address if launched with direct addressing; private IP address if launched with public addressing.-k/--kernel-id The ID of the kernel launched with this instance, if applicable.-z/--availability-zone The availability zone in which the instance launched. Same as placement-c/--product-codes Product codes associated with this instance.-p/--public-hostname The public hostname of the instance.-v/--public-ipv4 NATted public IP Address-u/--public-keys Public keys. Only available if supplied at instance launch time-r/--ramdisk-id The ID of the RAM disk launched with this instance, if applicable.-e/--reservation-id ID of the reservation.-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time-d/--user-data User-supplied data.Only available if supplied at instance launch time.```

その名も ec2-metadata コマンドです。そのまんまですね。直感的な名前とオプションにより簡単にメタデータを取得することができます。

試しにインスタンスタイプを取得してみましょう。

$ curl http://169.254.169.254/latest/meta-data/instance-type t2.micro

$ ec2-metadata -t instance-type: t2.micro

<br />簡単ですね。このコマンドによりメタデータを取得する際にURIなんだっけと悩む必要もなくなりなりそうです!!

また Amazon Linux では、標準でインストールされていますが、CentOSなどで使うには別途パッケージ cloud-utils のインストールが必要となります。

$ sudo yum install cloud-utils

$ rpm -ql cloud-utils /usr/bin/cloud-localds /usr/bin/cloud-publish-image /usr/bin/cloud-publish-tarball /usr/bin/cloud-run-instances /usr/bin/ec2metadata /usr/bin/resize-part-image /usr/bin/write-mime-multipart /usr/share/doc/cloud-utils-0.27 /usr/share/doc/cloud-utils-0.27/ChangeLog /usr/share/doc/cloud-utils-0.27/LICENSE /usr/share/man/man1/cloud-publish-image.1.gz /usr/share/man/man1/cloud-publish-tarball.1.gz /usr/share/man/man1/cloud-run-instances.1.gz /usr/share/man/man1/resize-part-image.1.gz /usr/share/man/man1/write-mime-multipart.1.gz

$ ec2metadata --help Syntax: /usr/bin/ec2metadata [options]

Query and display EC2 metadata.

If no options are provided, all options will be displayed

Options: -h --help show this help

--kernel-id display the kernel id --ramdisk-id display the ramdisk id --reservation-id display the reservation id

--ami-id display the ami id --ami-launch-index display the ami launch index --ami-manifest-path display the ami manifest path --ancestor-ami-ids display the ami ancestor id --product-codes display the ami associated product codes --availability-zone display the ami placement zone

--instance-id display the instance id --instance-type display the instance type

--local-hostname display the local hostname --public-hostname display the public hostname

--local-ipv4 display the local ipv4 ip address --public-ipv4 display the public ipv4 ip address

--block-device-mapping display the block device id --security-groups display the security groups

--mac display the instance mac address --profile display the instance profile --instance-action display the instance-action

--public-keys display the openssh public keys --user-data display the user data (not actually metadata)

-u | --url URL use URL (default: http://169.254.169.254/2009-04-04) ```

Amazon Linux に入ってるコマンドとは、オプションが若干違うのでバージョンに違いがありそうですね。

最後に

URIでアクセスし、メタデータを取得する方法は広く知られていますが ec2-metadata コマンドでも、メタデータを取得できるというのはあまり知られていない気がしたので、今回ブログにしました。(私自身、このコマンドのこと知りませんでした。。)

古くからあるコマンドなのでご存じな人も多いかと思いますが、このコマンドのことを初めて知ったという方のお役に立てばと思います。