#!/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_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`
InternetGatewayId=`aws ec2 create-internet-gateway --tag-specifications "[{\"ResourceType\":\"internet-gateway\",\"Tags\":[{\"Key\":\"Name\",\"Value\":\"igw-testfirstvpc\"}]}]"|jq -r .InternetGateway.InternetGatewayId`
aws ec2 attach-internet-gateway --internet-gateway-id $InternetGatewayId --vpc-id $VPC_ID
igwRouteTableId=`aws ec2 create-route-table --vpc-id $VPC_ID|jq -r .RouteTable.RouteTableId`
aws ec2 create-tags --resources $igwRouteTableId --tags Key=Name,Value=igw-testvpc
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}'`
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`
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
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