Created
August 19, 2021 13:30
-
-
Save thomashartm/cfe9cdcf9db0dae57897a52e0de98ad5 to your computer and use it in GitHub Desktop.
Manage different AWS settings and export them if necessary
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
#!/bin/bash | |
#################################### | |
# Prepares dev environment settings | |
# call: | |
# set-aws-env <environment> | |
# or for help: | |
# set-aws-env | |
# | |
# How to use and expected file system structure: | |
# Install gsed | |
# brew install gsed | |
# | |
# Make sure the following folder structure is in place | |
# - ~/.aws/ | |
# |_ config | |
# |_ credentials (this file is replaced and symlinked) | |
# |_ custom-vars (autogenerated for sourcing env variables) | |
# | |
# - ~/.aws-accounts/ | |
# |_ /customer1/ | |
# |_ credentials (contains ID and access key and maybe multiple profiles) | |
# |_ /customer2/ | |
# |_ credentials (contains ID and access key and maybe multiple profiles) | |
# | |
# What do you need to do: | |
# | |
# 1. Make sure to have the accounts directory created | |
# mkdir ~/.aws-accounts | |
# 2. create a subfolder for each aws profile/credentials pair e.g. | |
# mkdir ~/.aws-accounts/poc | |
# 3. add a credentials file and put AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY in | |
# use the [default] profile name e.g. | |
# | |
# [default] | |
# AWS_ACCESS_KEY_ID=sadsadasdasd | |
# AWS_SECRET_ACCESS_KEY=dadasdadas | |
# | |
#################################### | |
ACCOUNTS_DIR=$HOME/.aws-accounts | |
list_envs() | |
{ | |
echo -e "Prepare AWS environment credentials settings." | |
echo -e "Usage\t\t: set-aws-env <environment>" | |
echo -e "Available environments:" | |
ls -1 "$ACCOUNTS_DIR" | |
exit 1 | |
} | |
export_credentials() | |
{ | |
CREDENTIALS_FILE=$1 | |
AWS_ID=$(gsed -nr "/^\[default\]/ { :l /^aws_access_key_id[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $CREDENTIALS_FILE) | |
AWS_SECRET=$(gsed -nr "/^\[default\]/ { :l /^aws_secret_access_key[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $CREDENTIALS_FILE) | |
# cleanup work first. unset env variables and remove old bash source file first | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_SECRET_ACCESS_KEY | |
rm -f $HOME/.aws/custom-vars | |
# add new source file and source the env variables | |
cat > $HOME/.aws/custom-vars <<EOF | |
AWS_ACCESS_KEY_ID=$AWS_ID | |
AWS_SECRET_ACCESS_KEY=$AWS_SECRET | |
EOF | |
source $HOME/.aws/custom-vars | |
echo -e "---------" | |
echo -e "Updated Environment Variables: " | |
echo -e "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" | |
echo -e "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY" | |
} | |
LOCATION="$1" | |
set +x | |
if [ -n "$LOCATION" ]; then | |
LINKABLE_CREDENTIALS="$ACCOUNTS_DIR/$LOCATION/credentials" | |
if [ -f $LINKABLE_CREDENTIALS ]; then | |
CREDENTIALS_FILE="$HOME/.aws/credentials" | |
echo -e "---------" | |
echo -e "Linked: $CREDENTIALS_FILE" | |
echo -e "to: $LINKABLE_CREDENTIALS" | |
if [ -f $CREDENTIALS_FILE ]; then | |
rm $CREDENTIALS_FILE | |
fi | |
# link the profile credentials | |
ln -sf $LINKABLE_CREDENTIALS $CREDENTIALS_FILE | |
# now export the variables | |
export_credentials $LINKABLE_CREDENTIALS | |
else | |
echo -e "$LINKABLE_CREDENTIALS can not be found." | |
fi | |
else | |
list_envs | |
exit 1 | |
fi | |
set -x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment