Created
June 15, 2024 20:45
-
-
Save RichardTMiles/145f7a8e85c974ef7c7637a9862a1a74 to your computer and use it in GitHub Desktop.
Compile all metadata for an EC2 instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function fetch_metadata($p = '/') { | |
$t = file_get_contents("http://169.254.169.254/latest/api/token", false, stream_context_create(['http' => ['method' => 'PUT', 'header' => "X-aws-ec2-metadata-token-ttl-seconds: 21600\r\n"]])); | |
$u = "http://169.254.169.254/latest/meta-data$p"; | |
$o = stream_context_create(['http' => ['method' => 'GET', 'header' => "X-aws-ec2-metadata-token: $t\r\n"]]); | |
$r = file_get_contents($u, false, $o); | |
if ($r === false) return null; | |
$m = []; | |
foreach (explode("\n", trim($r)) as $l) { | |
$m[rtrim($l, '/')] = substr($l, -1) == '/' ? fetch_metadata("$p$l") : file_get_contents("$u/$l", false, $o); | |
} | |
return $m; | |
} | |
echo json_encode(fetch_metadata(), JSON_PRETTY_PRINT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -s https://gist.githubusercontent.com/RichardTMiles/145f7a8e85c974ef7c7637a9862a1a74/raw/aws_ec2_metadata_json.php | php