Skip to content

Instantly share code, notes, and snippets.

@chrowe
Last active September 5, 2025 20:55
Show Gist options
  • Select an option

  • Save chrowe/f0d7eca243f0ecc322e509ade5cdbe9d to your computer and use it in GitHub Desktop.

Select an option

Save chrowe/f0d7eca243f0ecc322e509ade5cdbe9d to your computer and use it in GitHub Desktop.
Improved aws sso cli experience

AWS CLI Helper for SSO users

This setup helps to streamline the process of authenticating with aws cli, especially if you have muliple profiles. It's primary featurs are

  1. Logs you in automatically (via the browser)
  2. Giving you a list of profiles to choose from if you have more than one or have not set the AWS_PROFILE environment variable.

0. Install and configure aws cli with at least one profile

1. Install aws-vault

2. Add this code to ~/.zshrc

# Transparent aws wrapper with aws-vault + SSO
aws() {
  local _AWS=aws

  # Already inside aws-vault? just run it
  if [[ -n "$AWS_VAULT" ]]; then
    $_AWS "$@"
    return
  fi

  # Respect env-selected profile
  if [[ -n "$AWS_PROFILE" ]]; then
    command aws-vault exec "$AWS_PROFILE" -- $_AWS "$@"
    return
  fi
  if [[ -n "$AWS_DEFAULT_PROFILE" ]]; then
    command aws-vault exec "$AWS_DEFAULT_PROFILE" -- $_AWS "$@"
    return
  fi

  # Discover profiles
  local -a profiles
  profiles=("${(@f)$(aws-vault list --profiles 2>/dev/null)}")

  # If exactly one, use it
  if (( ${#profiles[@]} == 1 )); then
    command aws-vault exec "${profiles[1]}" -- $_AWS "$@"
    return
  fi

  # Multiple: choose (fzf if present, else select)
  local profile
  if command -v fzf >/dev/null 2>&1; then
    profile="$(printf "%s\n" "${profiles[@]}" | fzf --prompt='Select AWS profile > ')"
  else
    echo "Select AWS profile:"
    select p in "${profiles[@]}"; do profile="$p"; break; done
  fi
  [[ -z "$profile" ]] && { echo "No profile selected."; return 1 }

  command aws-vault exec "$profile" -- $_AWS "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment