-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathawsenv
More file actions
executable file
·105 lines (85 loc) · 2.78 KB
/
awsenv
File metadata and controls
executable file
·105 lines (85 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
# Set the AWS environment for CLI usage using environment variables
# To run: eval $(awsenv <aws_profile_name>)
# AWS CLI
# Valid options for AWS_DEFAULT_OUTPUT:
# json: good for processing with `jq`
# text: (recommended with --query option) good for processing with *nix tools (tab-delimited)
# table: can be a bit more human readable
: ${AWS_DEFAULT_OUTPUT:="json"}
: ${AWS_CONFIG_FILE:="${HOME}/.aws/config"}
: ${AWS_CREDENTIAL_FILE:="${HOME}/.aws/credentials"}
: ${AWS_DEFAULT_REGION:="us-east-1"}
prg=${BASH_SOURCE##*/}
prg_version="1.1.0"
log() {
# Write messages to screen
echo "$(date +"%F %T") [${prg}] $1"
}
usage() {
cat <<EOM
${prg}
Setup your local environment to use AWS based on provided profile.
Usage: ${prg} [options] [profile]
The [profile] specified must exist in the provided AWS credentials. By default
this is the file ~/.aws/credentials as set in the environment variable, AWS_CREDENTIAL_FILE.
To use a different file set the environment variable to the required file.
OPTIONS:
-h, --help
Output help (this message)
-v=, --version
Output version.
EOM
}
version() {
cat <<EOM
${prg} v${prg_version}
EOM
}
set_env() {
echo export AWS_DEFAULT_OUTPUT=${AWS_DEFAULT_OUTPUT}
[[ -f ${HOME}/.aws/config ]] && [[ -z ${AWS_CONFIG_FILE} ]] && echo export AWS_CONFIG_FILE=${AWS_CONFIG_FILE}
[[ -f ${HOME}/.aws/credentials ]] && [[ -z ${AWS_CREDENTIAL_FILE} ]] && echo export AWS_CREDENTIAL_FILE=${AWS_CREDENTIAL_FILE}
[[ -z ${AWS_DEFAULT_REGION} ]] && echo export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
# Read from the files to set the environment using profiles if provided
awsCreds=( $(awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print section "=" $1 "=" "" $2 "" }' ${AWS_CREDENTIAL_FILE} | grep -v "##") )
for rl in "${awsCreds[@]}"; do
profile=$(echo "${rl}" | cut -d"=" -f1)
if [[ "${profile}" == "[${arg}]" ]]; then
key=$(echo "${rl}" | cut -d"=" -f2)
value=$(echo "${rl}" | cut -d"=" -f3)
if [[ "aws_access_key_id" == ${key} ]]; then
echo export AWS_ACCESS_KEY_ID=${value}
elif [[ "aws_secret_access_key" == ${key} ]]; then
echo export AWS_SECRET_ACCESS_KEY=${value}
echo export AWS_PROFILE=${arg}
elif [[ "aws_session_token" == ${key} ]]; then
echo export AWS_SESSION_TOKEN="${value}"
fi
fi
done
}
# Process command line
for arg in "$@"; do
if test -n "$prev_arg"; then
eval "$prev_arg=\$arg"
prev_arg=
fi
case "$arg" in
-*=*) optarg=`echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case $arg in
-h | --help)
usage && exit 0
;;
-v=* | --version)
version && exit 0
;;
-*)
log "Error - Unknown option ${arg}. Exiting."
exit 1
;;
esac
done
set_env