#!/bin/bash

# A script to create an Application Load Balancer (ALB) with a target group
# using AWS CLI.

# --- Configuration ---
REGION="us-east-1"
ALB_NAME="MyCLIAlb"
SG_NAME="MyCLIAlbSG"
TG_NAME="MyCLIAlbTG"

# --- 1. Get Default VPC and Subnets ---
echo "--- Getting Default VPC and Subnet IDs ---"
VPC_ID=$(aws ec2 describe-vpcs \
  --filters "Name=is-default,Values=true" \
  --query "Vpcs[0].VpcId" \
  --region $REGION \
  --output text)

if [ -z "$VPC_ID" ]; then
  echo "Error: Could not find a default VPC. Exiting."
  exit 1
fi
echo "Default VPC ID: $VPC_ID"

# Get at least two subnet IDs in different AZs within the default VPC
SUBNET_IDS=$(aws ec2 describe-subnets \
  --filters "Name=vpc-id,Values=$VPC_ID" "Name=default-for-az,Values=true" \
  --query "Subnets[0:2].SubnetId" \
  --region $REGION \
  --output text)

if [ -z "$SUBNET_IDS" ]; then
  echo "Error: Could not find enough default subnets. Exiting."
  exit 1
fi
echo "Default Subnet IDs: $SUBNET_IDS"

# --- 2. Create Security Group for ALB ---
echo -e "\n--- Creating Security Group: $SG_NAME ---"
SG_ID=$(aws ec2 create-security-group \
  --group-name $SG_NAME \
  --description "Allow HTTP traffic for ALB" \
  --vpc-id $VPC_ID \
  --region $REGION \
  --query 'GroupId' --output text)

# Authorize HTTP (port 80) from anywhere
aws ec2 authorize-security-group-ingress \
  --group-id $SG_ID \
  --protocol tcp \
  --port 80 \
  --cidr 0.0.0.0/0 \
  --region $REGION

echo "Security Group '$SG_NAME' created with ID: $SG_ID"

# --- 3. Create ALB ---
echo -e "\n--- Creating Application Load Balancer: $ALB_NAME ---"
ALB_ARN=$(aws elbv2 create-load-balancer \
  --name $ALB_NAME \
  --subnets $SUBNET_IDS \
  --security-groups $SG_ID \
  --scheme internet-facing \
  --type application \
  --region $REGION \
  --query 'LoadBalancers[0].LoadBalancerArn' --output text)

echo "ALB created with ARN: $ALB_ARN. Waiting for it to be active..."
aws elbv2 wait load-balancer-available \
  --load-balancer-arns $ALB_ARN \
  --region $REGION

echo "ALB is active."

# --- 4. Create Target Group ---
echo -e "\n--- Creating Target Group: $TG_NAME ---"
TG_ARN=$(aws elbv2 create-target-group \
  --name $TG_NAME \
  --protocol HTTP \
  --port 80 \
  --vpc-id $VPC_ID \
  --health-check-protocol HTTP \
  --health-check-path / \
  --health-check-interval-seconds 30 \
  --health-check-timeout-seconds 5 \
  --healthy-threshold-count 2 \
  --unhealthy-threshold-count 2 \
  --region $REGION \
  --query 'TargetGroups[0].TargetGroupArn' --output text)

echo "Target Group created with ARN: $TG_ARN"

# --- 5. Create Listener ---
echo -e "\n--- Creating Listener for ALB ---"
aws elbv2 create-listener \
  --load-balancer-arn $ALB_ARN \
  --protocol HTTP \
  --port 80 \
  --default-actions Type=forward,TargetGroupArn=$TG_ARN \
  --region $REGION

echo "Listener created."

# --- 6. Output ALB DNS Name ---
ALB_DNS_NAME=$(aws elbv2 describe-load-balancers \
  --load-balancer-arns $ALB_ARN \
  --query 'LoadBalancers[0].DNSName' \
  --region $REGION \
  --output text)

echo -e "\n--- ALB Setup Complete! ---"
echo "ALB DNS Name: $ALB_DNS_NAME"
echo "You can now register targets (e.g., EC2 instances) to the target group '$TG_NAME'."

read -p "Press Enter to delete the ALB and clean up resources..."

# --- Clean Up ---
echo -e "\n--- Cleaning up resources ---"

# Delete ALB
echo "Deleting ALB '$ALB_NAME' நான"
aws elbv2 delete-load-balancer \
  --load-balancer-arn $ALB_ARN \
  --region $REGION

echo "Waiting for ALB to be deleted..."
aws elbv2 wait load-balancer-not-exists \
  --load-balancer-arns $ALB_ARN \
  --region $REGION

echo "ALB deleted."

# Delete Target Group
echo "Deleting Target Group '$TG_NAME' நான"
aws elbv2 delete-target-group \
  --target-group-arn $TG_ARN \
  --region $REGION

echo "Target Group deleted."

# Delete Security Group
echo "Deleting Security Group '$SG_NAME' நான"
aws ec2 delete-security-group \
  --group-id $SG_ID \
  --region $REGION

echo "Security Group deleted."

echo -e "\n--- ALB demonstration and cleanup complete ---"
