53 lines
2.9 KiB
Bash
53 lines
2.9 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
# Set variables
|
||
|
REGION="us-east-1"
|
||
|
VPC_NAME="uam-bwc-vpc1"
|
||
|
PUBLIC_SUBNET1_NAME="uam-bwc-public-subnet1"
|
||
|
PUBLIC_SUBNET2_NAME="uam-bwc-public-subnet2"
|
||
|
SECURITY_GROUP_NAME="uam-bwc-sg"
|
||
|
TARGET_GROUP_NAME="uam-bwc-tg"
|
||
|
ALB_NAME="uam-bwc-alb"
|
||
|
LISTENER_NAME="uam-bwc-listener"
|
||
|
INSTANCE1_NAME="uam-bwc-httpd1"
|
||
|
INSTANCE2_NAME="uam-bwc-httpd2"
|
||
|
|
||
|
# Get VPC ID
|
||
|
VPC_ID=$(aws ec2 describe-vpcs --filters "Name=tag:Name,Values=$VPC_NAME" --region $REGION --query 'Vpcs[0].VpcId' --output text)
|
||
|
echo "Found VPC ID: $VPC_ID for VPC Name: $VPC_NAME"
|
||
|
|
||
|
# Get Public Subnet IDs
|
||
|
PUBLIC_SUBNET1_ID=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=$PUBLIC_SUBNET1_NAME" --region $REGION --query 'Subnets[0].SubnetId' --output text)
|
||
|
echo "Found Public Subnet 1 ID: $PUBLIC_SUBNET1_ID for Subnet Name: $PUBLIC_SUBNET1_NAME"
|
||
|
|
||
|
PUBLIC_SUBNET2_ID=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=$PUBLIC_SUBNET2_NAME" --region $REGION --query 'Subnets[0].SubnetId' --output text)
|
||
|
echo "Found Public Subnet 2 ID: $PUBLIC_SUBNET2_ID for Subnet Name: $PUBLIC_SUBNET2_NAME"
|
||
|
|
||
|
# Get Security Group ID
|
||
|
SG_ID=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=$SECURITY_GROUP_NAME" --region $REGION --query 'SecurityGroups[0].GroupId' --output text)
|
||
|
echo "Found Security Group ID: $SG_ID for Security Group Name: $SECURITY_GROUP_NAME"
|
||
|
|
||
|
# Get EC2 Instance IDs
|
||
|
INSTANCE1_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$INSTANCE1_NAME" --region $REGION --query 'Reservations[0].Instances[0].InstanceId' --output text)
|
||
|
echo "Found EC2 Instance 1 ID: $INSTANCE1_ID for Instance Name: $INSTANCE1_NAME"
|
||
|
|
||
|
INSTANCE2_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$INSTANCE2_NAME" --region $REGION --query 'Reservations[0].Instances[0].InstanceId' --output text)
|
||
|
echo "Found EC2 Instance 2 ID: $INSTANCE2_ID for Instance Name: $INSTANCE2_NAME"
|
||
|
|
||
|
# Create ALB
|
||
|
ALB_ARN=$(aws elbv2 create-load-balancer --name $ALB_NAME --subnets $PUBLIC_SUBNET1_ID $PUBLIC_SUBNET2_ID --security-groups $SG_ID --region $REGION --query 'LoadBalancers[0].LoadBalancerArn' --output text)
|
||
|
echo "Created ALB: $ALB_ARN with name $ALB_NAME"
|
||
|
|
||
|
# Create Target Group
|
||
|
TARGET_GROUP_ARN=$(aws elbv2 create-target-group --name $TARGET_GROUP_NAME --protocol HTTP --port 80 --vpc-id $VPC_ID --region $REGION --query 'TargetGroups[0].TargetGroupArn' --output text)
|
||
|
echo "Created Target Group: $TARGET_GROUP_ARN with name $TARGET_GROUP_NAME"
|
||
|
|
||
|
# Register Targets
|
||
|
aws elbv2 register-targets --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE1_ID Id=$INSTANCE2_ID --region $REGION
|
||
|
echo "Registered EC2 instances with Target Group"
|
||
|
|
||
|
# Create Listener
|
||
|
LISTENER_ARN=$(aws elbv2 create-listener --load-balancer-arn $ALB_ARN --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN --region $REGION --query 'Listeners[0].ListenerArn' --output text)
|
||
|
echo "Created Listener: $LISTENER_ARN on port 80"
|
||
|
|
||
|
echo "ALB setup complete"
|