Created
August 3, 2023 16:12
-
-
Save oguzhancoskun/87bfbf6993e20715e0334a5f9aa67ae6 to your computer and use it in GitHub Desktop.
ec2 metadata json wrapper
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
import requests | |
import json | |
def get_instance_metadata(): | |
metadata_url = "http://169.254.169.254/latest/meta-data/tags/instance" | |
res = requests.get(metadata_url) | |
data_list = res.text.split() | |
data_dict = {} | |
for item in data_list: | |
r = requests.get(f"{metadata_url}/{item}") | |
data_dict[item] = r.text | |
return data_dict | |
if __name__ == "__main__": | |
metadata = get_instance_metadata() | |
json_data = json.dumps(metadata) | |
print(json_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment