[ VPC ]Setting Up a VPC on AWS
Project Architecture
1. VPC Setup
1.1 Create VPC
CIDR Block:
10.10.0.0/16
Command:
vpc_id=$(aws ec2 create-vpc --cidr-block 10.10.0.0/16 --query 'Vpc.VpcId' --output text) echo "VPC ID: $vpc_id"
1.2 Internet Gateway Configuration
Purpose: Enable VPC to communicate with the internet.
Commands:
igw_id=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text) aws ec2 attach-internet-gateway --vpc-id $vpc_id --internet-gateway-id $igw_id echo "New Internet Gateway ID: $igw_id"
1.3 Create Subnets
Public Subnets:
public_subnet_id_1=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block 10.10.0.0/28 --availability-zone ap-northeast-2a --query 'Subnet.SubnetId' --output text) public_subnet_id_2=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block 10.10.0.16/28 --availability-zone ap-northeast-2b --query 'Subnet.SubnetId' --output text) echo "Public Subnet 1 ID: $public_subnet_id_1" echo "Public Subnet 2 ID: $public_subnet_id_2"
Private Subnets:
private_subnet_id_1=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block 10.10.1.0/24 --availability-zone ap-northeast-2a --query 'Subnet.SubnetId' --output text) private_subnet_id_2=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block 10.10.2.0/24 --availability-zone ap-northeast-2b --query 'Subnet.SubnetId' --output text) echo "Private Subnet 1 ID: $private_subnet_id_1" echo "Private Subnet 2 ID: $private_subnet_id_2"
Tagging Subnets: -> FOR EKS CLUSTER LATER
REF AWS DOC <- CHECK!!!
aws ec2 create-tags --resources $public_subnet_id_1 $public_subnet_id_2 --tags Key=kubernetes.io/cluster/spoid-cluster,Value=shared aws ec2 create-tags --resources $public_subnet_id_1 $public_subnet_id_2 --tags Key=kubernetes.io/role/elb,Value=1 aws ec2 create-tags --resources $private_subnet_id_1 $private_subnet_id_2 --tags Key=kubernetes.io/cluster/spoid-cluster,Value=shared aws ec2 create-tags --resources $private_subnet_id_1 $private_subnet_id_2 --tags Key=kubernetes.io/role/internal-elb,Value=1
1.4 NAT Gateway Setup
Purpose: Allow private subnets to access the internet.
Commands:
eip_allocation_id=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text) nat_gateway_id=$(aws ec2 create-nat-gateway --subnet-id $public_subnet_id_1 --allocation-id $eip_allocation_id --query 'NatGateway.NatGatewayId' --output text) echo "NAT Gateway ID: $nat_gateway_id"
1.5 Routing Table Configuration
Public Subnet Routing Table:
public_route_table_id=$(aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpc_id" --query 'RouteTables[0].RouteTableId' --output text) aws ec2 create-route --route-table-id $public_route_table_id --destination-cidr-block 0.0.0.0/0 --gateway-id $igw_id
Private Subnet Routing Tables:
private_route_table_id_1=$(aws ec2 create-route-table --vpc-id $vpc_id --query 'RouteTable.RouteTableId' --output text) private_route_table_id_2=$(aws ec2 create-route-table --vpc-id $vpc_id --query 'RouteTable.RouteTableId' --output text) aws ec2 associate-route-table --route-table-id $public_route_table_id --subnet-id $public_subnet_id_1 aws ec2 associate-route-table --route-table-id $public_route_table_id --subnet-id $public_subnet_id_2 aws ec2 associate-route-table --route-table-id $private_route_table_id_1 --subnet-id $private_subnet_id_1 aws ec2 associate-route-table --route-table-id $private_route_table_id_2 --subnet-id $private_subnet_id_2 echo "Public Route Table ID: $public_route_table_id" echo "Private Route Table 1 ID: $private_route_table_id_1" echo "Private Route Table 2 ID: $private_route_table_id_2"
NAT Gateway Route for Private Subnets:
aws ec2 create-route --route-table-id $private_route_table_id_1 --destination-cidr-block 0.0.0.0/0 --gateway-id $nat_gateway_id aws ec2 create-route --route-table-id $private_route_table_id_2 --destination-cidr-block 0.0.0.0/0 --gateway-id $nat_gateway_id
Next Post
In the next post, we'll address a CIDR range issue encountered during the Load Balancer setup and how to troubleshoot it effectively.