Ritul Chavda
Ritul Chavda

Reputation: 203

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: enter image description here

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

Thanks in advance

Upvotes: 1

Views: 1430

Answers (1)

Chris Williams
Chris Williams

Reputation: 35188

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();

Upvotes: 1

Related Questions