Skip to content

Instantly share code, notes, and snippets.

@phnahes
Last active October 22, 2025 13:38
Show Gist options
  • Select an option

  • Save phnahes/e2b412bf5396c8f3783771dd104d8813 to your computer and use it in GitHub Desktop.

Select an option

Save phnahes/e2b412bf5396c8f3783771dd104d8813 to your computer and use it in GitHub Desktop.
Automatically generate an EKS kubeconfig based on AWS_PROFILE (kubernetes)
#!/usr/bin/env bash
# --------------------------------------------------
# Script: eks-connect.sh
# Purpose: Automatically generate an EKS kubeconfig using AWS_PROFILE and region
# Compatible with bash/zsh
# Creates per-profile + per-region kubeconfig
# --------------------------------------------------
# Usage:
# ./eks-connect.sh <aws_profile> [region]
# Example:
# ./eks-connect.sh production us-east-1
# ./eks-connect.sh staging eu-west-1
PROFILE=${1:-default}
REGION=${2:-us-east-1}
# Define kubeconfig path including profile and region
KUBECONFIG_PATH="$HOME/.kube/config-${PROFILE}-${REGION}"
export AWS_PROFILE="$PROFILE"
export KUBECONFIG="$KUBECONFIG_PATH"
echo "Using AWS profile: $PROFILE"
echo "Region: $REGION"
echo "KUBECONFIG path: $KUBECONFIG_PATH"
echo
# List available clusters
CLUSTERS=$(aws eks list-clusters --region "$REGION" --profile "$PROFILE" --query "clusters" --output text)
if [ -z "$CLUSTERS" ]; then
echo "No EKS clusters found for profile '$PROFILE' in region '$REGION'."
exit 1
fi
# Count clusters
CLUSTER_COUNT=$(echo "$CLUSTERS" | wc -w | tr -d ' ')
if [ "$CLUSTER_COUNT" -eq 1 ]; then
CLUSTER_NAME=$CLUSTERS
echo "Found only one cluster: $CLUSTER_NAME"
else
echo "Available clusters:"
i=1
CLUSTER_LIST=()
for c in $CLUSTERS; do
echo " [$i] $c"
CLUSTER_LIST+=("$c")
((i++))
done
echo
read -p "Enter the number of the cluster to connect to: " choice
CLUSTER_NAME="${CLUSTER_LIST[$((choice-1))]}"
if [ -z "$CLUSTER_NAME" ]; then
echo "Invalid option."
exit 1
fi
fi
echo
echo "Generating kubeconfig for cluster '$CLUSTER_NAME'..."
aws eks update-kubeconfig \
--name "$CLUSTER_NAME" \
--region "$REGION" \
--profile "$PROFILE" \
--kubeconfig "$KUBECONFIG_PATH"
if [ $? -eq 0 ]; then
echo
echo "Successfully connected to cluster '$CLUSTER_NAME'."
echo "Current context:"
kubectl --kubeconfig "$KUBECONFIG_PATH" config current-context
echo
echo "--------------------------------------------------"
echo " KUBECONFIG USAGE GUIDE"
echo "--------------------------------------------------"
echo
echo "Temporarily use this kubeconfig:"
echo " export KUBECONFIG=$KUBECONFIG_PATH"
echo " kubectl get nodes"
echo
echo "Combine multiple kubeconfig files:"
echo " export KUBECONFIG=~/.kube/config-dev-us-east-1:~/.kube/config-staging-us-east-1:~/.kube/config-production-us-east-1"
echo " kubectl config get-contexts"
echo
echo "Return to default kubeconfig:"
echo " unset KUBECONFIG"
echo
echo "--------------------------------------------------"
else
echo "Failed to generate kubeconfig."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment