Tạo VPC 3-tier bằng AWS CLI
🎯 Mục tiêu: Tạo VPC với 3 tầng subnet (public/private-app/private-data), Internet Gateway, NAT Gateway, Route Table hoàn chỉnh bằng AWS CLI.
🧰 Công cụ / nền tảng: AWS CLI v2 (đã configure profile), jq, bash hoặc PowerShell.
📦 Chuẩn bị: Cài AWS CLI v2 (aws --version ≥ 2.x), chạy aws configure với Access Key + Secret Key + region ap-southeast-1. Cần quyền IAM: ec2:*.
▶️ Các bước (AWS CLI):
# 1. Tạo VPC
VPC_ID=$(aws ec2 create-vpc \
--cidr-block 10.0.0.0/16 \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=devops-vpc}]' \
--query 'Vpc.VpcId' --output text)
echo "VPC: $VPC_ID"
# 2. Bật DNS hostname
aws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-hostnames
# 3. Tạo Internet Gateway và attach
IGW_ID=$(aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=devops-igw}]' \
--query 'InternetGateway.InternetGatewayId' --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID
# 4. Tạo 3 subnet (public, private-app, private-data)
PUB_SN=$(aws ec2 create-subnet \
--vpc-id $VPC_ID --cidr-block 10.0.1.0/24 \
--availability-zone ap-southeast-1a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=public-1a}]' \
--query 'Subnet.SubnetId' --output text)
APP_SN=$(aws ec2 create-subnet \
--vpc-id $VPC_ID --cidr-block 10.0.11.0/24 \
--availability-zone ap-southeast-1a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=private-app-1a}]' \
--query 'Subnet.SubnetId' --output text)
DATA_SN=$(aws ec2 create-subnet \
--vpc-id $VPC_ID --cidr-block 10.0.21.0/24 \
--availability-zone ap-southeast-1a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=private-data-1a}]' \
--query 'Subnet.SubnetId' --output text)
# 5. Public Route Table → IGW
PUB_RT=$(aws ec2 create-route-table --vpc-id $VPC_ID \
--tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=rt-public}]' \
--query 'RouteTable.RouteTableId' --output text)
aws ec2 create-route --route-table-id $PUB_RT \
--destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_ID
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_SN
# 6. Tạo Elastic IP + NAT Gateway trong public subnet
EIP=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text)
NAT_ID=$(aws ec2 create-nat-gateway \
--subnet-id $PUB_SN --allocation-id $EIP \
--tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=devops-nat}]' \
--query 'NatGateway.NatGatewayId' --output text)
echo "Waiting for NAT Gateway to become available..."
aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT_ID
# 7. Private Route Table → NAT Gateway
PRIV_RT=$(aws ec2 create-route-table --vpc-id $VPC_ID \
--tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=rt-private}]' \
--query 'RouteTable.RouteTableId' --output text)
aws ec2 create-route --route-table-id $PRIV_RT \
--destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_ID
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $APP_SN
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $DATA_SN
echo "VPC=$VPC_ID PUB=$PUB_SN APP=$APP_SN DATA=$DATA_SN"
🖥️ Đối chiếu AWS Console:
VPC → Your VPCs: thấy devops-vpc CIDR 10.0.0.0/16. Subnets: 3 subnet đúng AZ và CIDR. Route Tables: rt-public có route 0.0.0.0/0 → igw-xxx; rt-private có route 0.0.0.0/0 → nat-xxx.
✅ Kết quả mong đợi: aws ec2 describe-vpcs --vpc-ids $VPC_ID --query 'Vpcs[0].State' trả về "available". Ba subnet tồn tại, 2 route table với route mặc định đúng target.
🧹 Cleanup:
# Xóa theo thứ tự ngược (NAT trước, rồi IGW, rồi VPC)
aws ec2 delete-nat-gateway --nat-gateway-id $NAT_ID
aws ec2 wait nat-gateway-deleted --nat-gateway-ids $NAT_ID
aws ec2 release-address --allocation-id $EIP
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID
aws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID
aws ec2 delete-subnet --subnet-id $PUB_SN
aws ec2 delete-subnet --subnet-id $APP_SN
aws ec2 delete-subnet --subnet-id $DATA_SN
aws ec2 delete-route-table --route-table-id $PUB_RT
aws ec2 delete-route-table --route-table-id $PRIV_RT
aws ec2 delete-vpc --vpc-id $VPC_ID