Skip to content

Instantly share code, notes, and snippets.

@lioneltchami
Created October 10, 2024 04:06
Show Gist options
  • Save lioneltchami/ceee8de218bce38e9c1dbd79f9856038 to your computer and use it in GitHub Desktop.
Save lioneltchami/ceee8de218bce38e9c1dbd79f9856038 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
# Default values
EKS_CLUSTER="${EKS_CLUSTER:-test-cluster}"
EKS_VERSION="${EKS_VERSION:-1.27}"
AWS_REGION="${AWS_REGION:-us-west-2}"
NODE_TYPE="${NODE_TYPE:-t3.medium}"
NODE_COUNT="${NODE_COUNT:-3}"
NODE_MIN="${NODE_MIN:-1}"
NODE_MAX="${NODE_MAX:-5}"
ADDONS="${ADDONS:-coredns,vpc-cni,kube-proxy}"
# Function to display usage information
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -c, --cluster NAME Set EKS cluster name (default: $EKS_CLUSTER)"
echo " -v, --version VERSION Set Kubernetes version (default: $EKS_VERSION)"
echo " -r, --region REGION Set AWS region (default: $AWS_REGION)"
echo " -t, --node-type TYPE Set node instance type (default: $NODE_TYPE)"
echo " -n, --node-count NUM Set initial node count (default: $NODE_COUNT)"
echo " --min-nodes NUM Set minimum node count (default: $NODE_MIN)"
echo " --max-nodes NUM Set maximum node count (default: $NODE_MAX)"
echo " -a, --addons LIST Comma-separated list of addons (default: $ADDONS)"
echo " -h, --help Display this help message"
exit 1
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--cluster) EKS_CLUSTER="$2"; shift; shift ;;
-v|--version) EKS_VERSION="$2"; shift; shift ;;
-r|--region) AWS_REGION="$2"; shift; shift ;;
-t|--node-type) NODE_TYPE="$2"; shift; shift ;;
-n|--node-count) NODE_COUNT="$2"; shift; shift ;;
--min-nodes) NODE_MIN="$2"; shift; shift ;;
--max-nodes) NODE_MAX="$2"; shift; shift ;;
-a|--addons) ADDONS="$2"; shift; shift ;;
-h|--help) usage ;;
*) echo "Unknown option: $1"; usage ;;
esac
done
# Check if eksctl is installed
if ! command -v eksctl &>/dev/null; then
echo "eksctl is not installed. Please install it first."
exit 1
fi
# Check if AWS CLI is configured
if ! aws sts get-caller-identity &>/dev/null; then
echo "AWS CLI is not configured. Please run 'aws configure' first."
exit 1
fi
# Function to create EKS cluster
create_eks_cluster() {
echo "Creating EKS cluster: $EKS_CLUSTER"
eksctl create cluster \
--name "$EKS_CLUSTER" \
--version "$EKS_VERSION" \
--region "$AWS_REGION" \
--nodegroup-name standard-workers \
--node-type "$NODE_TYPE" \
--nodes "$NODE_COUNT" \
--nodes-min "$NODE_MIN" \
--nodes-max "$NODE_MAX" \
--managed \
--addons "$ADDONS" \
--with-oidc \
--ssh-access \
--ssh-public-key ~/.ssh/id_rsa.pub \
--tags "Environment=Test,Project=EKSDemo" \
--asg-access \
--full-ecr-access \
--alb-ingress-access
}
# Main execution
echo "EKS Cluster Configuration:"
echo " Cluster Name: $EKS_CLUSTER"
echo " Kubernetes Version: $EKS_VERSION"
echo " AWS Region: $AWS_REGION"
echo " Node Type: $NODE_TYPE"
echo " Initial Node Count: $NODE_COUNT"
echo " Min Nodes: $NODE_MIN"
echo " Max Nodes: $NODE_MAX"
echo " Addons: $ADDONS"
read -p "Do you want to proceed with this configuration? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
create_eks_cluster
else
echo "Aborted."
exit 1
fi
echo "EKS cluster creation complete. You can now use 'kubectl' to interact with the cluster."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment