Created
February 26, 2014 01:00
-
-
Save ygbr/9221319 to your computer and use it in GitHub Desktop.
Amazon AWS EC2 Instance Info
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# This generates a dict with several info regarding your EC2 instance. | |
# If you need extra info, add the required info on the indicators list | |
# | |
# | |
import http.client | |
import hashlib | |
import socket | |
indicators = [ | |
"instance-id", | |
"public-ipv4", | |
"public-hostname", | |
"ami-id", | |
"hostname", | |
"placement/availability-zone", | |
"reservation-id", | |
"instance-type", | |
"ami-launch-index" | |
] | |
def getInfo(datapoint): | |
conn = http.client.HTTPConnection("169.254.169.254") | |
conn.request("GET","/latest/meta-data/" + datapoint) | |
return conn.getresponse().read().decode() | |
def getSignature(): | |
conn = http.client.HTTPConnection("169.254.169.254") | |
conn.request("GET","/latest/dynamic/instance-identity/signature") | |
return hashlib.sha256(conn.getresponse().read()).hexdigest() | |
info = {"signature": getSignature(), "_id": socket.gethostname()} | |
for i in indicators: | |
info[i] = getInfo(i) | |
print(info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment