简体   繁体   English

如何以编程方式获取实例的公共DNS?

[英]How to programmatically get public dns of an instance?

Is there a way to get the public DNS of an EC2 instance using PHP amazon SDK (or amazon's API command line tools)? 有没有办法使用PHP amazon SDK(或亚马逊的API命令行工具)获取EC2实例的公共DNS?

I tried this PHP code (among others) but it won't work: 我尝试了这个PHP代码(以及其他),但它不起作用:

require_once '/full/path/sdk.class.php';
$ec2      = new AmazonEC2();
$response = $ec2->describe_regions();
print_r($response);

and also 并且

require_once '/full/path/sdk.class.php';
$ec2      = new AmazonEC2();
$response = $ec2->describe_instances(array(
    'Filter' => array(
        array('Name' => 'availability-zone', 'Value' => 'eu-west-1')
    )
));

print_r($response);

but I can't see the public dns in the response 但我无法在回应中看到公众dns

If you're running this on the instance itself, you can hit AWS's internal metadata endpoint: 如果您在实例本身上运行此操作,则可以访问AWS的内部元数据端点:

$hostname = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname');

http://169.254.169.254/latest/meta-data/ will give you a list of the various metadata available to you. http://169.254.169.254/latest/meta-data/将为您提供可供您使用的各种元数据的列表。 Currently: 目前:

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
kernel-id
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups

Apparently you'd like to retrieve your Amazon EC2 instance in region eu-west-1 (which isn't a correct value for 'availability-zone' btw.). 显然,您想要在区域eu-west-1检索您的Amazon EC2实例(这对于'availability-zone'btw来说不是正确的值。)。 However, you are not specifying any region and all services default to the US-East region , see the AWS team response to the related question describeInstances() is only giving me instances in us-east : 但是,您没有指定任何区域,并且所有服务都默认为美国东部地区 ,请参阅AWS团队对相关问题的回复describeInstances()仅向我提供us-east中的实例

While you can't grab data for all regions in a single call, you can call the describe_instances() method in each region. 虽然您无法在一次调用中获取所有区域的数据,但您可以在每个区域中调用describe_instances()方法。

 $ec2 = new AmazonEC2(); $ec2->set_region(AmazonEC2::REGION_US_W1); // US-West 1 $response = $ec2->describe_instances(); 

Using this code with the appropriate constant for your region of choice (eg AmazonEC2::REGION_EU_W1 ) should yield the desired result. 使用此代码与您选择的区域(例如AmazonEC2::REGION_EU_W1 )的适当常量应该产生所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM