Last active
July 12, 2021 15:42
-
-
Save sepeth/a7827c5d6df2d67e86748ff4465524c4 to your computer and use it in GitHub Desktop.
Lets you pass your AWS CLI profile credentials to another program as environment variables
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 | |
# Say you have a NodeJS program that uses AWS.EnvironmentVariables, | |
# and you want to call that with different profiles you have: | |
# | |
# use-profile <profile> node your-script.js | |
import os | |
import sys | |
from configparser import ConfigParser | |
import subprocess | |
config = ConfigParser() | |
config.read(os.path.expanduser('~/.aws/credentials')) | |
try: | |
profile = sys.argv[1] | |
except IndexError: | |
sys.exit("First argument should be the profile name.") | |
try: | |
creds = config[profile] | |
except KeyError: | |
sys.exit("Couldn't find the profile in ~/.aws/credentials") | |
access_key_id = creds['aws_access_key_id'] | |
secret_access_key = creds['aws_secret_access_key'] | |
try: | |
session_token = creds['aws_session_token'] | |
except KeyError: | |
session_token = None | |
os.environ['AWS_ACCESS_KEY_ID'] = access_key_id | |
os.environ['AWS_SECRET_ACCESS_KEY'] = secret_access_key | |
if session_token: | |
os.environ['AWS_SESSION_TOKEN'] = session_token | |
subprocess.run(sys.argv[2:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment