# Create VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16
# Create Subnets (one for each availability zone)
aws ec2 create-subnet --vpc-id <VPC_ID> --cidr-block 10.0.1.0/24 --availability-zone <AZ1>
aws ec2 create-subnet --vpc-id <VPC_ID> --cidr-block 10.0.2.0/24 --availability-zone <AZ2>
# Create Internet Gateway and attach to VPC
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --vpc-id <VPC_ID> --internet-gateway-id <IGW_ID>
# Create Route Table and associate it with subnets
aws ec2 create-route-table --vpc-id <VPC_ID>
aws ec2 create-route --route-table-id <RT_ID> --destination-cidr-block 0.0.0.0/0 --gateway-id <IGW_ID>
aws ec2 associate-route-table --subnet-id <SUBNET_ID> --route-table-id <RT_ID>
###Create security group
# Create security group
aws ec2 create-security-group --group-name ELBSG --description "Allow Traffic For Loadbaalncer HTTP and SSH" --vpc-id vpc-0a79c4c8f0741c409
aws ec2 authorize-security-group-ingress --group-id sg-069a86ebf3f159ea6 --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-069a86ebf3f159ea6 --protocol tcp --port 80 --cidr 0.0.0.0/0
#Create ec2 machines
# Launch EC2 instances with User Data for website
#aws ec2 run-instances --image-id <AMI_ID> --count 2 --instance-type t2.micro --key-name <KeyPair> --security-group-ids <SG_ID> --subnet-id <SUBNET_ID> --user-data '#!/bin/bash
aws ec2 run-instances --image-id ami-08718895af4dfa033 --count 2 --instance-type t2.micro --key-name india --security-group-ids sg-069a86ebf3f159ea6 --subnet-id subnet-01c192bd850cce746 --user-data '#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "We are testing the APP from $(hostname -f)" > /var/www/html/index.html' --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=loadbalncer}]'
#create a target group
# Create Target Group for ALB
aws elbv2 create-target-group --name ALB-TargetGroup1 --protocol HTTP --port 80 --vpc-id vpc-0a79c4c8f0741c409
# Create Target Group for NLB
aws elbv2 create-target-group --name NLB-TargetGroup --protocol TCP --port 80 --vpc-id <VPC_ID>
# Register EC2 instances to the ALB target group
aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:ap-south-1:820242933469:targetgroup/ALB-TargetGroup1/5c44f2fd1cdec6df --targets Id=i-04b292a37ea0510d5
# Create an ALB
aws elbv2 create-load-balancer --name MyApplicationLoadBalancer --subnets subnet-01c192bd850cce746 subnet-034585682385a6bba --security-groups sg-069a86ebf3f159ea6 --scheme internet-facing --type application
# Create a listener for ALB
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:ap-south-1:820242933469:loadbalancer/app/MyApplicationLoadBalancer/fdc5562103874b5b --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-south-1:820242933469:targetgroup/ALB-TargetGroup1/5c44f2fd1cdec6df
#Creating loadbalancer temple
# Create a Network Load Balancer
aws elbv2 create-load-balancer --name MyNetworkLoadBalancer --subnets <SUBNET1_ID> <SUBNET2_ID> --scheme internet-facing --type network
# Register EC2 instances to the NLB target group
aws elbv2 register-targets --target-group-arn <NLB_TargetGroup_ARN> --targets Id=<Instance1_ID> Id=<Instance2_ID>
# Create a listener for NLB
aws elbv2 create-listener --load-balancer-arn <NLB_ARN> --protocol TCP --port 80 --default-actions Type=forward,TargetGroupArn=<NLB_TargetGroup_ARN>
#Verify loadbalancer
# Describe ALB
aws elbv2 describe-load-balancers --names MyApplicationLoadBalancer
# Describe NLB
aws elbv2 describe-load-balancers --names MyNetworkLoadBalancer
0 Comments