Where and Who Am I? OCI Instance Metadata Service

By | December 9, 2018

When you are running your instances on OCI, you sometimes would like to know inside the instance where it really it. Well this is possible by using OCI Metadata service. 

The Metadata service listen on 169.254.169.254 and will report back information about the Instance, like OCID, Displayname, Shape, Region, etc. Including any custom Metadata that was supplied when creating the Instance will also be reported back on.

From the Linux CLI inside your instance, you can for example run:

Shape=$(curl -s http://169.254.169.254/opc/v1/instance/shape)
echo "$Shape"

DC=$(curl -s http://169.254.169.254/opc/v1/instance/availabilityDomain)
echo "$DC"

If you are running a webserver (with PHP enabled), here is an example web page that queries the information and displays it.

<html>
 <head>
   <title>OCI - Webserver</title>
 </head>
 <body>
 <img src="https://dka575ofm4ao0.cloudfront.net/pages-transactional_logos/retina/41865/Z9Ik6huVSQC0sSJf8To4"><br>
 Welcome to the OCI Webserver!<br>
<?php

$json = file_get_contents("http://169.254.169.254/opc/v1/instance/");
$obj = json_decode($json);
$displayName = $obj->displayName;
$shape = $obj->shape;
$ad = $obj->availabilityDomain;
$region = $obj->region;
$faultDomain = $obj->faultDomain;
$internal_ip = $_SERVER['SERVER_ADDR'];
$external_ip = exec('curl http://ipecho.net/plain; echo');

print ("<p><b>My info</b><br>");
print ("<table border = 1>");
print ("<tr><td>My Public IP</td><td>$external_ip</td></tr>");
print ("<tr><td>My Private IP</td><td>$internal_ip</td></tr>");
print ("<tr><td>Instance</td><td>$displayName</td></tr>");
print ("<tr><td>Shape</td><td>$shape</td></tr>");
print ("<tr><td>Region</td><td>$region</td></tr>");
print ("<tr><td>Availability Domain</td><td>$ad</td></tr>");
print ("<tr><td>Fault Domain</td><td>$faultDomain</tr></tr>");
print ("</table>");

?>
  </body>
</html>

More information about the Metadata service can be found here:

https://docs.cloud.oracle.com/iaas/Content/Compute/Tasks/gettingmetadata.htm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.