Skip to content

Instantly share code, notes, and snippets.

@ygbr
Created February 26, 2014 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygbr/9221319 to your computer and use it in GitHub Desktop.
Save ygbr/9221319 to your computer and use it in GitHub Desktop.
Amazon AWS EC2 Instance Info
#!/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