From 758f86611df2a668b77dca42d0ada3eebd6bb247 Mon Sep 17 00:00:00 2001 From: Tim Steiner <tim.steiner@nebraska.edu> Date: Tue, 19 Dec 2023 17:33:41 -0600 Subject: [PATCH] More IMDSv2 tokens. --- httpd-to-cloudwatch.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/httpd-to-cloudwatch.py b/httpd-to-cloudwatch.py index 95d3b7b..87d6fbb 100755 --- a/httpd-to-cloudwatch.py +++ b/httpd-to-cloudwatch.py @@ -1,8 +1,7 @@ #!/usr/bin/python import boto3 -import urllib2 -import json +import requests import sys #import re #import tempfile @@ -41,8 +40,8 @@ def main(): # Returns the region the instance is currently running in (eg: 'us-east') def get_region(): - instance_info = urllib2.urlopen('http://169.254.169.254/2018-09-24/dynamic/instance-identity/document/').read() - instance_info = json.loads(instance_info) + session_token = requests.put('http://169.254.169.254/latest/api/token', headers={'X-aws-ec2-metadata-token-ttl-seconds': 60}).text + instance_info = requests.get('http://169.254.169.254/2018-09-24/dynamic/instance-identity/document/', headers={'X-aws-ec2-metadata-token': session_token}).json() return instance_info['region'] # Returns the instance_id of this VM. -- GitLab