-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws-select-profile
More file actions
executable file
·39 lines (33 loc) · 1.31 KB
/
Copy pathaws-select-profile
File metadata and controls
executable file
·39 lines (33 loc) · 1.31 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
#!/usr/bin/env bash
# aws-select-profile - Use 'dialog' to select an AWS profile and export it into your current shell.
#
# Assuming this script is in your PATH, simply run:
# $ `aws-select-profile`
# or:
# $ . aws-select-profile
[ "${DEBUG:-0}" = "1" ] && set -x
_cleanup () { [ -n "$_TMP" ] && rm -rf "$_TMP" ; } ; trap _cleanup EXIT
_list_profiles () {
grep '^\[profile ' ~/.aws/config | cut -d ' ' -f 2 | cut -d ']' -f 1 | sort -u
}
declare -a profiles=()
for item in $(_list_profiles) ; do
profiles+=("$item" "$item")
done
_TMP=`mktemp`
dialog --stdout --backtitle "AWS Profile" --menu "Select an AWS Profile to export" 25 80 20 "${profiles[@]}" > "$_TMP"
selected="$(cat "$_TMP")"
export AWS_PROFILE="$selected"
echo "export AWS_PROFILE=$selected"
if aws --profile "$selected" configure list | grep -q -e "region.*<not set>" ; then
declare -a regions=()
for region in $(aws --region us-east-1 ec2 describe-regions --query 'Regions[*].RegionName' --output text) ; do
regions+=("$region" "$region")
done
dialog --stdout --backtitle "AWS Region" --menu "Select a default AWS Region" 25 80 20 "${regions[@]}" > "$_TMP"
selected="$(cat "$_TMP")"
if [ -n "${selected:-}" ] ; then
export AWS_DEFAULT_REGION="$selected"
echo "export AWS_DEFAULT_REGION=$selected"
fi
fi