Skip to content

Instantly share code, notes, and snippets.

@RichardTMiles
Created June 15, 2024 20:45
Show Gist options
  • Save RichardTMiles/145f7a8e85c974ef7c7637a9862a1a74 to your computer and use it in GitHub Desktop.
Save RichardTMiles/145f7a8e85c974ef7c7637a9862a1a74 to your computer and use it in GitHub Desktop.
Compile all metadata for an EC2 instance
<?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);
@RichardTMiles
Copy link
Author

RichardTMiles commented Jun 15, 2024

curl -s https://gist.githubusercontent.com/RichardTMiles/145f7a8e85c974ef7c7637a9862a1a74/raw/aws_ec2_metadata_json.php | php
{
    "ami-id": "ami-0e73b72b22d60430e",
    "ami-launch-index": "0",
    "ami-manifest-path": "(unknown)",
    "autoscaling": {
        "target-lifecycle-state": "InService"
    },
    "block-device-mapping": {
        "ami": "xvda",
        "root": "\/dev\/xvda"
    },
    "events": {
        "maintenance": {
            "history": "[]",
            "scheduled": "[]"
        }
    },
    "hostname": "ip-10-0-44-91.ec2.internal",
    "iam": {
        "info": "{\n  \"Code\" : \"Success\",\n  \"LastUpdated\" : \"2024-06-15T20:50:01Z\",\n  \"InstanceProfileArn\" : \"arn:aws:iam::891377212071:instance-profile\/EC2RoleForSSM\",\n  \"InstanceProfileId\" : \"AIPA47CRY3KTUDV3SK3QV\"\n}",
        "security-credentials": {
            "EC2RoleForSSM": "{\n  \"Code\" : \"Success\",\n  \"LastUpdated\" : \"2024-06-15T20:49:59Z\",\n  \"Type\" : \"AWS-HMAC\",\n  \"AccessKeyId\" : \"ASIA47CRY3KTSJHMMAHU\",\n  \"SecretAccessKey\" : \"imX+cog\/yNRJYxAYOGheLBDa2\/Y7b1r+UlmyB5c1\",\n  \"Token\" : \"IQoJb3JpZ2luX2VjEC0aCXVzLWVhc3QtMSJHMEUCIQDEDORITfHaKcPu5Qi+y0bckNNwRxjYODiPIGSFv55rbQIgZVVhvQLoIzqlKkcm7L\/+WREDiXBQ2s0CYHzSetaAWvgqwgUIxv\/\/\/\/\/\/\/\/\/\/ARAAGgw4OTEzNzcyMTIwNzEiDNSp7RizwyZOYJts1iqWBb0HLB7ZH\/e6bU7wyhX7V7z89PuIuVVjywazhiIn2YrlDKC1BIBvFSr+c6REPvhn6YMjHYzf7TvUXy3txrSZL\/m30ahKl3Par\/ob4rY7u3hcAfoIine93hfzcISRM7D\/YuohpKj2qHK+5yvCVdrbz6ySrkRIl7DA+8sVtZVD3lqLqddPtVPUp8U272j9Mc0o\/RCRptCfWKbuS92QZWZqFEXFC6GISk8GG5C+H4UmYCCkZGTm7uwCiVAwyDvihCtekt4yVY7reoqbBuwcwMDsfCPgZVSFZGAKucT81vPP18ehsg0u1skWfjQ3Ci8g\/o6NsxuUzraPi7Ah90M2MmzyyW9X+omT\/\/DZMGP95LmMwPn38+EH9i3LY+QvAjfKBEhn2lsFUGB\/UAGCVMZoF0LOzFlQifrvmdqPTrf7BhLtJhH6xp1puwteluGllzJmbvZRKZ8EwdJucdc40N4AQZREJVVWo83GWbF2\/D7tXKiGM+7dqH6nFD+o7j2rSQpXla5R5RNuikjcx5vIQKm6WLHC4W88cE2BA9uxWGjs+wYNTarlsDbWS52HInjvwTPpR5ilfp1Hc6zIPSjZdpAfP6RVYw2wy9JZMkq+LD\/v\/o7VRvht7G4W4QE1DLDjMdHi8y2TdIvow78YFV7pvDqbDGcvLx0nqzumWytkJo5fjpU28wQIfZUDNG602NuHyRK6El4bQH7gH2H38w2Xx7n70RSKKJAym8SjOW4IgCtutksE9TjO7rvIfBB1trDJcuPfMFlftxQQMjby0mVXS5WXDdb5l8GW+WYfpFkgXefl0JHey7\/8YQpuHK0kxfoFLk2JEc+StO\/yGJNViiPmIW+MOZBa6\/hAVLPcDf0XwIuqsPa7w\/iQK+3Ph6pyMPn8t7MGOrEBGCE0x7u2SmdOmQPIFPjLain+8TJkx3OWYa2dafUiFQKXClHz55rjzCDmJ7dCty3aA7JopHgD7goscoF8IiAQmv+iUZItg2Qrvxpy4EomElF5rSJ7cIEycG3Tl1OBSQrcReeL5YViLCIaPp+MqX7FeFW\/ocz90T9aZ80Ap\/KKGOQNGnc+3FssrnhkBeLDSDoK9fn1uDMHlZi793Yscmqu5jP5kZkxNQfHoSZZ9I8WO6Yn\",\n  \"Expiration\" : \"2024-06-16T03:25:01Z\"\n}"
        }
    },
    "identity-credentials": {
        "ec2": {
            "info": "{\n  \"Code\" : \"Success\",\n  \"LastUpdated\" : \"2024-06-15T20:49:47Z\",\n  \"AccountId\" : \"891377212071\"\n}",
            "security-credentials": {
                "ec2-instance": "{\n  \"Code\" : \"Success\",\n  \"LastUpdated\" : \"2024-06-15T20:49:11Z\",\n  \"Type\" : \"AWS-HMAC\",\n  \"AccessKeyId\" : \"ASIA47CRY3KTYFLX4JIL\",\n  \"SecretAccessKey\" : \"DsmjKdV0arPKNLgyOV6dv3T9pQ3dA6eCVNFDgCNY\",\n  \"Token\" : \"IQoJb3JpZ2luX2VjEC0aCXVzLWVhc3QtMSJHMEUCIQDbRU++Fo9Kwur7XbM6\/8aUdEpuHLyAgeo69QBno9qu9wIga3psGb7L\/+cOj0hK0EHE5B5AqdiXvfUFFrDEVl3EMQ8qzwQIxv\/\/\/\/\/\/\/\/\/\/ARAAGgw4OTEzNzcyMTIwNzEiDCqQfF\/OZhLLLbf5gSqjBMjl0YhTySzCIlPHwc70J2rdOTzUK2eKGjUVhRFXzkoaZyZ2wp0o+\/Oyh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-06-16T02:54:12Z\"\n}"
            }
        }
    },
    "instance-action": "none",
    "instance-id": "i-0645fa50326d63e44",
    "instance-life-cycle": "on-demand",
    "instance-type": "t3.micro",
    "local-hostname": "ip-10-0-44-91.ec2.internal",
    "local-ipv4": "10.0.44.91",
    "mac": "0e:14:6f:d7:bd:05",
    "metrics": {
        "vhostmd": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    },
    "network": {
        "interfaces": {
            "macs": {
                "0e:14:6f:d7:bd:05": {
                    "device-number": "0",
                    "interface-id": "eni-08558aeecd7c0b70c",
                    "ipv4-associations": {
                        "34.227.88.68": "10.0.44.91"
                    },
                    "local-hostname": "ip-10-0-44-91.ec2.internal",
                    "local-ipv4s": "10.0.44.91",
                    "mac": "0e:14:6f:d7:bd:05",
                    "owner-id": "891377212071",
                    "public-hostname": "ec2-34-227-88-68.compute-1.amazonaws.com",
                    "public-ipv4s": "34.227.88.68",
                    "security-group-ids": "sg-07d588d7104dfe852",
                    "security-groups": "s-Ec2SecurityGroup-SR8mUUc4pA6b",
                    "subnet-id": "subnet-0c8b9d28763f67a4d",
                    "subnet-ipv4-cidr-block": "10.0.32.0\/20",
                    "vpc-id": "vpc-078e6bdc63714b325",
                    "vpc-ipv4-cidr-block": "10.0.0.0\/16",
                    "vpc-ipv4-cidr-blocks": "10.0.0.0\/16"
                }
            }
        }
    },
    "placement": {
        "availability-zone": "us-east-1d",
        "availability-zone-id": "use1-az6",
        "region": "us-east-1"
    },
    "profile": "default-hvm",
    "public-hostname": "ec2-34-227-88-68.compute-1.amazonaws.com",
    "public-ipv4": "34.227.88.68",
    "reservation-id": "r-05826a9cdfb5eddc8",
    "security-groups": "s-Ec2SecurityGroup-SR8mUUc4pA6b",
    "services": {
        "domain": "amazonaws.com",
        "partition": "aws"
    },
    "system": "nitro"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment