3

Given these parameters:

  1. An Ubuntu instance running anywhere (any region and any availability zone in that region)
  2. Only the AWS PHP SDK2 installed on the instance (no other EC2 Command line tools, etc.)
  3. CURL and WGET available

What is the most elegant way for an instance script to explicitly determine what Region it is running in?

* **Instance ID:** wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
* **Availability Zone:** wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone

The meta-data does not currently provide this information.

We are aware that currently, the trailing character can be stripped off the availability zone in order to naively determine the Region, however, there is no guarantee that Amazon will not change that in the future.

The ultimate goal is that our cron jobs and other custom services can figure out who and where they are, so that they can interact with other services and instances appropriately.

4

3 に答える 3

5

これはあなたのために働くはずです:

REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`

echo $REGION

このスレッドからです:

EC2 インスタンス内からリージョンを検索する

于 2013-08-01T16:50:13.283 に答える
2

PHP SDK を使用すると、次のことができます。

$instance = \Aws\Common\InstanceMetadata\InstanceMetadataClient::factory()
    ->get('dynamic/instance-identity/document')
    ->send()
    ->json();

echo $instance['region'];
于 2013-08-01T21:38:04.203 に答える