Created
November 28, 2019 20:20
-
-
Save mvaldesdeleon/7157562e2f50c2b6b42788f7459b771d to your computer and use it in GitHub Desktop.
"credential_source=Ec2InstanceMetadata" workaround
This file contains 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
[profile target-account] | |
role_arn = arn:aws:iam::123456789012:role/RoleInTargetAccount | |
source_profile = instance-role | |
region = eu-central-1 |
This file contains 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
[instance-role] | |
aws_access_key_id= | |
aws_secret_access_key= | |
aws_session_token= |
This file contains 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 | |
import os | |
import urllib.request | |
import json | |
import configparser | |
CREDENTIALS_FILE = '~/.aws/credentials' | |
PROFILE_NAME = 'instance-role' | |
# Retrieve the first instance role | |
response = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/iam/security-credentials/') | |
role_name = response.read().decode('utf-8').split('\n')[0] | |
# Retrieve the credentials for said role | |
response = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/iam/security-credentials/' + role_name) | |
credentials = json.load(response) | |
# Update credentials file | |
full_path = os.path.expanduser(CREDENTIALS_FILE) | |
config = configparser.ConfigParser() | |
config.read(full_path) | |
config[PROFILE_NAME] = {'aws_access_key_id': credentials['AccessKeyId'], 'aws_secret_access_key': credentials['SecretAccessKey'], 'aws_session_token': credentials['Token']} | |
with open(full_path, 'w') as file: | |
config.write(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment