简体   繁体   中英

How to get EC2 instance availability zone and id using php in aws?

I don't know not to get EC2 instance availability zone and instance id details using php in amazon web services. As I don't know how to fetch data, so I haven't tried anything. Help me out.

The thing I want: /info

This is exact what I wanted: 在此处输入图像描述

In that info page I can want to display that Instance details.

Thanks in advance

You can get the current instances values by using the meta-data api , these can be accessed from within your application by using a HTTP request library such as GuzzleHTTP or by using native cURL commands built into PHP.

To get the instance ID you would need to request from your current server to the following URL.

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

To get the instances current availability zone you would need to request from your current server the following URL

http://169.254.169.254/latest/meta-data/placement/availability-zone

Assuming you use GuzzleHTTP it would be as simple as calling the below

$client = new GuzzleHttp\Client();
$response = $client->get('http://169.254.169.254/latest/meta-data/instance-id');
echo "Instance ID: " . $response->getBody();
$response = $client->get('http://169.254.169.254/latest/meta-data/placement/availability-zone');
echo "Availability Zone: " . $response->getBody();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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