curlコマンドでIMDSv1のアカウントIDを取得する

EC2インスタンスでは169.254.169.254にHTTPでアクセスするとインスタンスのメタデータを取得できる。

アカウントIDはhttp://169.254.169.254/latest/dynamic/instance-identity/documentに含まれている。

EC2でcurlを使って以下のように確認できる。

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "accountId" : "123456789012",
  "architecture" : "",
  "availabilityZone" : "",
  "billingProducts" : null,
  "devpayProductCodes" : null,
  "marketplaceProductCodes" : null,
  "imageId" : "",
  "instanceId" : "",
  "instanceType" : "",
  "kernelId" : null,
  "pendingTime" : "",
  "privateIp" : "",
  "ramdiskId" : null,
  "region" : "",
  "version" : ""
}

そのEC2インスタンスが属するAWSアカウントのアカウントIDはaccountIdキーで取得できる。

IMDSv1でアカウントIDを取得するワンライナー

jqコマンドを使う場合

jqコマンドを使ってaccountIdキーの値を取得する。

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | \
  jq -r .accountId
123456789012

sedコマンドを使う場合

jqコマンドをインストールしていないサーバの場合は以下のようにsedコマンドを使ってaccountIdキーの値を取得できる。

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | \
  sed -n 's/.*"accountId" : "\(.*\)".*/\1/p'
123456789012

awkコマンドを使う場合

以下ではawkコマンドを使ってaccountIdキーの値を取得する。

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | \
  awk -F'"' '/accountId/ {print $4}'
123456789012

grepコマンドを使う場合

以下ではgrepコマンドを使ってaccountIdキーの値を取得する。

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | \
  grep -oP '"accountId" : "\K[^"]+'
123456789012

IMDSv2でアカウントIDを取得するワンライナー

IMDSv2では事前にトークンを取得し、X-aws-ec2-metadata-tokenヘッダに指定してリクエストする必要がある。
トークンを取得するにはhttp://169.254.169.254/latest/api/tokenに対してPUTメソッドでリクエストする。

$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")

X-aws-ec2-metadata-token-ttl-secondsヘッダでは取得したトークンの有効期限を指定する。
上記では300秒に設定している。

こうして取得したトークンをX-aws-ec2-metadata-tokenヘッダに指定して以下のように情報を取得する。

$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "accountId" : "123456789012",
  "architecture" : "",
  "availabilityZone" : "",
  "billingProducts" : null,
  "devpayProductCodes" : null,
  "marketplaceProductCodes" : null,
  "imageId" : "",
  "instanceId" : "",
  "instanceType" : "",
  "kernelId" : null,
  "pendingTime" : "",
  "privateIp" : "",
  "ramdiskId" : null,
  "region" : "",
  "version" : ""
}

jqコマンドを使う場合

IMDSv2でもjqコマンドを使ってaccountIdキーの値を取得する。
TOKENを取得してヘッダに指定する部分を除けばIMDSv1と同じ。

$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/dynamic/instance-identity/document | \
  jq -r .accountId
123456789012

sedコマンドを使う場合

IMDSv2でsedコマンドを使ってaccountIdキーの値を取得する。

$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/dynamic/instance-identity/document | \
  sed -n 's/.*"accountId" : "\(.*\)".*/\1/p'
123456789012

awkコマンドを使う場合

IMDSv2でawkコマンドを使ってaccountIdキーの値を取得する。

$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/dynamic/instance-identity/document | \
  awk -F'"' '/accountId/ {print $4}'
123456789012

grepコマンドを使う場合

IMDSv2でgrepコマンドを使ってaccountIdキーの値を取得する。

$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/dynamic/instance-identity/document | \
  grep -oP '"accountId" : "\K[^"]+'
123456789012