AWS-cli创建网络(VPC、subnet、route、igw、NAT)

#!/bin/bash
rm -rf subnet.txt natgateway.txt
CIDR=10.0.0.0/16
publicsubnets=("subnet-public1,10.0.0.0/24,eu-central-1a" "subnet-public2,10.0.1.0/24,eu-central-1b" "subnet-public3,10.0.2.0/24,eu-central-1c")
privatesubnets=("subnet-private1,10.0.3.0/24,eu-central-1a" "subnet-private2,10.0.4.0/24,eu-central-1b" "subnet-private3,10.0.5.0/24,eu-central-1c")
dbsubnets=("subnet-DB1,10.0.6.0/24,eu-central-1a" "subnet-DB2,10.0.7.0/24,eu-central-1b" "subnet-DB3,10.0.8.0/24,eu-central-1c")
#创建VPC
VPC_ID=`aws ec2 create-vpc --cidr-block $CIDR --tag-specifications "[{\"ResourceType\":\"vpc\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"testfirstvpc\"}]}]" --no-amazon-provided-ipv6-cidr-block --instance-tenancy default|jq -r .Vpc.VpcId`
 
#删除VPC aws ec2 delete-vpc --vpc-id vpc-0d3131529705b9a0b
#创建IGW
InternetGatewayId=`aws ec2 create-internet-gateway --tag-specifications "[{\"ResourceType\":\"internet-gateway\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"igw-testfirstvpc\"}]}]"|jq -r .InternetGateway.InternetGatewayId`
#删除igw aws ec2 delete-internet-gateway --internet-gateway-id igw-00144bccb5b7155c0
#将IGW附加到VPC
aws ec2 attach-internet-gateway --internet-gateway-id $InternetGatewayId --vpc-id $VPC_ID
#IGW解除绑定VPC aws ec2 detach-internet-gateway --internet-gateway-id $InternetGatewayId --vpc-id $VPC_ID
#创建igw路由表
igwRouteTableId=`aws ec2 create-route-table --vpc-id $VPC_ID|jq -r .RouteTable.RouteTableId`
#创建IGWnameTAG
aws ec2 create-tags --resources $igwRouteTableId --tags Key=Name,Value=igw-testvpc
#在igw路由表添加igw路由
aws ec2 create-route --route-table-id $igwRouteTableId --destination-cidr-block 0.0.0.0/0 --gateway-id $InternetGatewayId
#创建共有子网
for subnet in ${publicsubnets[@]}
do
 subnetname=`echo $subnet|awk -F"," '{print $1}'`
 subnetcidr=`echo $subnet|awk -F"," '{print $2}'`
 subnetAvailabilityZone=`echo $subnet|awk -F"," '{print $3}'`
 #aws ec2 associate-route-table --route-table-id $RouteTableId --subnet-id subnet-0748ef7a26aefc7cc
 subnetId=`aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block "$subnetcidr" --tag-specifications "[{\"ResourceType\":\"subnet\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"$subnetname\"}]}]" --availability-zone "$subnetAvailabilityZone"|jq -r .Subnet.SubnetId`
 #关联IGW路由表
 aws ec2 associate-route-table --route-table-id $igwRouteTableId --subnet-id $subnetId
 AllocationId=`aws ec2 allocate-address --domain vpc |jq -r .AllocationId`
 ngwgateway=`aws ec2 create-nat-gateway --subnet-id $subnetId --allocation-id $AllocationId --tag-specifications "[{\"ResourceType\":\"natgateway\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"ngw-$subnetname\"}]}]"|jq -r .NatGateway.NatGatewayId`
 echo $ngwgateway>>natgateway.txt
done
#创建私有子网
for subnet in ${privatesubnets[@]}
do
 subnetname=`echo $subnet|awk -F"," '{print $1}'`
 subnetcidr=`echo $subnet|awk -F"," '{print $2}'`
 subnetAvailabilityZone=`echo $subnet|awk -F"," '{print $3}'`
 subnetId=`aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block "$subnetcidr" --tag-specifications "[{\"ResourceType\":\"subnet\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"$subnetname\"}]}]" --availability-zone "$subnetAvailabilityZone"|jq -r .Subnet.SubnetId`
echo $subnetId>>subnet.txt
done
 
exec 3<"natgateway.txt"
exec 4<"subnet.txt"
while read natgateway<&3 && read subnetid<&4
do
 RouteTableId=`aws ec2 create-route-table --vpc-id $VPC_ID|jq -r .RouteTable.RouteTableId`
 aws ec2 create-tags --resources $RouteTableId --tags Key=Name,Value="ngw-$subnetid"
 aws ec2 create-route --route-table-id $RouteTableId --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $natgateway
 aws ec2 associate-route-table --route-table-id $RouteTableId --subnet-id $subnetid
done
 
#创建DB子网
for subnet in ${dbsubnets[@]}
do
 subnetname=`echo $subnet|awk -F"," '{print $1}'`
 subnetcidr=`echo $subnet|awk -F"," '{print $2}'`
 subnetAvailabilityZone=`echo $subnet|awk -F"," '{print $3}'`
 aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block "$subnetcidr" --tag-specifications "[{\"ResourceType\":\"subnet\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"$subnetname\"}]}]" --availability-zone "$subnetAvailabilityZone"
done

posted on 2021-12-31 10:40  无聊的时候看一下  阅读(376)  评论(0)    收藏  举报

导航