Skip to content

Instantly share code, notes, and snippets.

@vivasvan1
Last active September 30, 2025 12:51
Show Gist options
  • Select an option

  • Save vivasvan1/01ed8799c252d7e16c34b63f6deccbb4 to your computer and use it in GitHub Desktop.

Select an option

Save vivasvan1/01ed8799c252d7e16c34b63f6deccbb4 to your computer and use it in GitHub Desktop.
This script updates creds in your ~/.aws/credentials file using aws cli
#!/bin/bash
# Function to detect OS
detect_os() {
case "$(uname -s)" in
Darwin*) echo "macos" ;;
Linux*) echo "linux" ;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
# Function to check if AWS CLI is installed
check_aws_cli() {
if command -v aws &> /dev/null; then
return 0
else
return 1
fi
}
# Function to install AWS CLI based on OS
install_aws_cli() {
local os=$(detect_os)
echo "AWS CLI not found. Installing for $os..."
case $os in
"macos")
echo "Installing AWS CLI for macOS..."
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg ./AWSCLIV2.pkg -target /
rm -f AWSCLIV2.pkg
;;
"linux")
echo "Installing AWS CLI for Linux..."
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
rm -rf awscliv2.zip aws/
;;
"windows")
echo "Please install AWS CLI manually for Windows from: https://awscli.amazonaws.com/AWSCLIV2.msi"
echo "Or run: msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi"
exit 1
;;
*)
echo "Unsupported OS: $os"
echo "Please install AWS CLI manually from: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
exit 1
;;
esac
# Verify installation
if check_aws_cli; then
echo "AWS CLI installed successfully!"
else
echo "AWS CLI installation failed. Please install manually."
exit 1
fi
}
# Function to show usage
show_usage() {
echo "Usage: $0 [--profile PROFILE_NAME] [output_file_paths...]"
echo ""
echo "Options:"
echo " --profile PROFILE_NAME Use specific AWS profile (default: default)"
echo " output_file_paths... Additional paths to write credentials to"
echo ""
echo "Examples:"
echo " $0 # Use default profile"
echo " $0 --profile myprofile # Use 'myprofile' profile"
echo " $0 --profile dev /path/to/creds # Use 'dev' profile and write to file"
}
# Parse command line arguments
PROFILE="default"
OUTPUT_PATHS=()
while [[ $# -gt 0 ]]; do
case $1 in
--profile)
PROFILE="$2"
shift 2
;;
--help|-h)
show_usage
exit 0
;;
*)
OUTPUT_PATHS+=("$1")
shift
;;
esac
done
# Check if AWS CLI is installed, install if not
if ! check_aws_cli; then
install_aws_cli
fi
# Check if jq is available
if ! command -v jq &> /dev/null; then
echo "Warning: jq is not installed. Attempting to parse JSON manually."
echo "For better reliability, please install jq."
fi
echo "Using AWS profile: $PROFILE"
# Export the credentials using the AWS CLI with specified profile
if [ "$PROFILE" = "default" ]; then
eval "$(aws configure export-credentials --format env)"
output=$(aws configure export-credentials)
else
eval "$(aws configure export-credentials --profile $PROFILE --format env)"
output=$(aws configure export-credentials --profile $PROFILE)
fi
# Check if the output contains the required keys
if echo "$output" | grep -q "AccessKeyId" && echo "$output" | grep -q "SecretAccessKey"; then
# Extract values using jq if available, otherwise use bash string manipulation
if command -v jq &> /dev/null; then
access_key=$(echo "$output" | jq -r .AccessKeyId)
secret_key=$(echo "$output" | jq -r .SecretAccessKey)
session_token=$(echo "$output" | jq -r '.SessionToken // ""')
expiration=$(echo "$output" | jq -r '.Expiration // ""')
else
# Fallback to bash string manipulation
access_key=$(echo "$output" | grep -o '"AccessKeyId": *"[^"]*"' | cut -d'"' -f4)
secret_key=$(echo "$output" | grep -o '"SecretAccessKey": *"[^"]*"' | cut -d'"' -f4)
session_token=$(echo "$output" | grep -o '"SessionToken": *"[^"]*"' | cut -d'"' -f4)
fi
# Create credentials content
credentials_content="[$PROFILE]
aws_access_key_id=$access_key
aws_secret_access_key=$secret_key"
# Add session token if it exists (for temporary credentials)
if [ -n "$session_token" ] && [ "$session_token" != "null" ]; then
credentials_content="$credentials_content
aws_session_token=$session_token"
fi
# Add region
credentials_content="$credentials_content
region=us-east-2"
# Write credentials to the ~/.aws/credentials file
mkdir -p ~/.aws
echo "$credentials_content" > ~/.aws/credentials
# Write to additional output paths if provided
if [ ${#OUTPUT_PATHS[@]} -gt 0 ]; then
for path in "${OUTPUT_PATHS[@]}"; do
{
# Create directory if it doesn't exist
mkdir -p "$(dirname "$path")"
echo "$credentials_content" > "$path"
echo "AWS credentials have been successfully written to $path"
} || {
echo "Failed to write AWS credentials to $path, but continuing..."
}
done
fi
echo "AWS credentials have been successfully written to ~/.aws/credentials"
if [ -n "$session_token" ] && [ "$session_token" != "null" ]; then
if [ -n "$expiration" ] && [ "$expiration" != "null" ]; then
echo "Credentials expire at: $expiration"
fi
fi
else
echo "Failed to retrieve AWS credentials. Please check your AWS CLI configuration and profile '$PROFILE'."
echo "Available profiles:"
aws configure list-profiles 2>/dev/null || echo "Unable to list profiles"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment