-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_aws_credentials.sh
executable file
·38 lines (24 loc) · 1.73 KB
/
change_aws_credentials.sh
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
#!/bin/bash
# This script will change the default AWS credentials to the ones provided by the user stored in the conf file in the same directory as this script.
# To run this script you must have the AWS CLI installed and configured with the default credentials.
# This script will also change the default region to the one provided by the user stored in the conf file(aws_credentials.conf) in the same directory as this script.
# To run this script go to ~/.bashrc and add the following line: alias aws_crd='source /path/to/change_aws_credentials.sh'
# Then run the script by typing aws_crd in the terminal.
# Example aws_credentials.conf file:
# [company]
# aws_access_key_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# aws_secret_access_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
set -e
set -o pipefail
DIR="$(dirname "${BASH_SOURCE[0]}")"
echo "Choose the number of the credential from the conf file:"
awk -F'[][]' '/\[/{print ++i ": " $2}' $DIR/aws_credentials.conf
read -p "Enter the number of the credential: " credential_number
credential_name=$(awk -F'[][]' '/\[/{print ++i ": " $2}' $DIR/aws_credentials.conf | grep "^$credential_number" | cut -d ':' -f2 | cut -d ' ' -f2)
aws_access_key_id=$(awk -F'=' '/aws_access_key_id/{print $2}' $DIR/aws_credentials.conf | sed -n "$credential_number"p)
aws_secret_access_key=$(awk -F'=' '/aws_secret_access_key/{print $2}' $DIR/aws_credentials.conf | sed -n "$credential_number"p)
echo "Changing the default credentials to the ones from the $credential_name section in the conf file."
# Change the default credentials in the AWS CLI.
aws configure set aws_access_key_id $aws_access_key_id
aws configure set aws_secret_access_key $aws_secret_access_key
echo "Credentials changed successfully! Bye!"