Skip to content

Instantly share code, notes, and snippets.

@hwshim0810 hwshim0810/
Last active Jul 10, 2019

What would you like to do?
Get ec2 private ip
import os
def is_ec2_linux():
"""Detect if we are running on an EC2 Linux Instance
if os.path.isfile("/sys/hypervisor/uuid"):
with open("/sys/hypervisor/uuid") as f:
uuid =
return uuid.startswith("ec2")
return False
def get_linux_ec2_private_ip():
"""Get the private IP Address of the machine if running on an EC2 linux server"""
from urllib.request import urlopen
if not is_ec2_linux():
return None
response = urlopen('')
ec2_ip ='utf-8')
if response:
return ec2_ip
except Exception as e:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.