#!/bin/bash # Setup AWS EKS Cluster for VIMS # Creates EKS cluster, node groups, and required IAM roles set -e REGION="eu-north-1" CLUSTER_NAME="landvex-prod" NAMESPACE="vims" PROJECT="vims" NODE_TYPE="t3.medium" NODE_COUNT=2 # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' echo -e "${GREEN}🔧 VIMS EKS Setup${NC}" echo "==================" echo "Region: ${REGION}" echo "Cluster: ${CLUSTER_NAME}" echo "" # Check prerequisites echo -e "${YELLOW}Checking prerequisites...${NC}" if ! command -v eksctl &> /dev/null; then echo -e "${YELLOW}Installing eksctl...${NC}" curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp sudo mv /tmp/eksctl /usr/local/bin fi if ! command -v kubectl &> /dev/null; then echo -e "${RED}kubectl not found. Please install kubectl.${NC}" exit 1 fi # Get AWS account ID AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) echo -e "${BLUE}AWS Account: ${AWS_ACCOUNT_ID}${NC}" # Create ECR repository echo -e "${YELLOW}Creating ECR repository...${NC}" ECR_URI="${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com" aws ecr describe-repositories --repository-names ${PROJECT} --region ${REGION} 2>/dev/null || \ aws ecr create-repository \ --repository-name ${PROJECT} \ --region ${REGION} \ --image-scanning-configuration scanOnPush=true \ --encryption-configuration encryptionType=AES256 echo -e "${GREEN}✓ ECR repository created${NC}" # Create EKS cluster echo -e "${YELLOW}Creating EKS cluster (this takes ~15 minutes)...${NC}" echo -e "${BLUE} Cluster: ${CLUSTER_NAME}${NC}" echo -e "${BLUE} Nodes: ${NODE_COUNT}x ${NODE_TYPE}${NC}" eksctl create cluster \ --name ${CLUSTER_NAME} \ --region ${REGION} \ --node-type ${NODE_TYPE} \ --nodes ${NODE_COUNT} \ --nodes-min 1 \ --nodes-max 4 \ --managed \ --asg-access \ --external-dns-access \ --full-ecr-access \ --appmesh-access \ --alb-ingress-access \ --node-private-networking \ --ssh-access \ --ssh-public-key ~/.ssh/id_rsa.pub \ --tags "Project=${PROJECT}" \ --tags "Environment=production" \ --verbose 2 echo -e "${GREEN}✓ EKS cluster created${NC}" # Update kubeconfig echo -e "${YELLOW}Updating kubeconfig...${NC}" aws eks update-kubeconfig \ --region ${REGION} \ --name ${CLUSTER_NAME} \ --alias ${CLUSTER_NAME} # Verify cluster echo -e "${YELLOW}Verifying cluster...${NC}" kubectl get nodes # Install AWS Load Balancer Controller echo -e "${YELLOW}Installing AWS Load Balancer Controller...${NC}" # Create IAM policy for ALB POLICY_ARN=$(aws iam list-policies --query 'Policies[?PolicyName==`AWSLoadBalancerControllerIAMPolicy`].Arn' --output text) if [ -z "${POLICY_ARN}" ]; then curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.4.4/docs/install/iam_policy.json aws iam create-policy \ --policy-name AWSLoadBalancerControllerIAMPolicy \ --policy-document file://iam_policy.json POLICY_ARN=$(aws iam list-policies --query 'Policies[?PolicyName==`AWSLoadBalancerControllerIAMPolicy`].Arn' --output text) fi # Create service account eksctl create iamserviceaccount \ --cluster=${CLUSTER_NAME} \ --namespace=kube-system \ --name=aws-load-balancer-controller \ --attach-policy-arn=${POLICY_ARN} \ --approve \ --region ${REGION} # Install cert-manager echo -e "${YELLOW}Installing cert-manager...${NC}" kubectl apply \ --validate=false \ -f https://github.com/jetstack/cert-manager/releases/download/v1.5.4/cert-manager.yaml kubectl wait --for=condition=available --timeout=300s deployment/cert-manager -n cert-manager kubectl wait --for=condition=available --timeout=300s deployment/cert-manager-webhook -n cert-manager kubectl wait --for=condition=available --timeout=300s deployment/cert-manager-cainjector -n cert-manager # Install AWS Load Balancer Controller echo -e "${YELLOW}Installing AWS Load Balancer Controller...${NC}" kubectl apply -k "github.com/aws/eks-charts/stable/aws-load-balancer-controller//crds?ref=master" helm repo add eks https://aws.github.io/eks-charts helm repo update helm install aws-load-balancer-controller eks/aws-load-balancer-controller \ -n kube-system \ --set clusterName=${CLUSTER_NAME} \ --set serviceAccount.create=false \ --set serviceAccount.name=aws-load-balancer-controller \ --set region=${REGION} \ --set vpcId=$(aws eks describe-cluster --name ${CLUSTER_NAME} --region ${REGION} --query 'cluster.resourcesVpcConfig.vpcId' --output text) echo -e "${GREEN}✓ AWS Load Balancer Controller installed${NC}" # Install metrics-server echo -e "${YELLOW}Installing metrics-server...${NC}" kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml # Create namespace echo -e "${YELLOW}Creating VIMS namespace...${NC}" kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f - echo "" echo -e "${GREEN}🎉 EKS Setup Complete!${NC}" echo "" echo -e "${BLUE}Cluster Info:${NC}" echo " Name: ${CLUSTER_NAME}" echo " Region: ${REGION}" echo " Nodes: ${NODE_COUNT}x ${NODE_TYPE}" echo " ECR: ${ECR_URI}/${PROJECT}" echo "" echo -e "${BLUE}Next steps:${NC}" echo " 1. Build and push Docker image: docker build -t ${PROJECT} . && docker push ${ECR_URI}/${PROJECT}:latest" echo " 2. Deploy VIMS: ./scripts/deploy-eks.sh" echo " 3. Check status: kubectl get all -n ${NAMESPACE}" echo "" echo -e "${GREEN}✅ Setup complete!${NC}"