#!/bin/bash
###################################
#Created on 27-Jun-2018
#Purpose : This script will Create a VPC/Subnet/Routetable/Internetgateway/Natgateway and associate them to the corresponding subnets
#Modified on : 13-Sep-2018
####################################
vpcName="My-VPC"
vpcCidrBlock="10.0.0.0/16"
PubsubNetCidrBlock="10.0.1.0/24"
PrvsubNetCidrBlock="10.0.2.0/24"
pubAvailabilityZone="ap-northeast-1a"
prvAvailabilityZone="ap-northeast-1c"
pubSubnetName="PublicSubnet-My"
prvSubnetName="PrivateSubnet-My"
PubRouteTableName="MyPublicRoute"
PrvRouteTableName="MyPrivateRoute"
destinationCidrBlock="0.0.0.0/0"
#Create a VPC with a 10.0.0.0/16 CIDR block.
aws_response=$(aws ec2 create-vpc --cidr-block "$vpcCidrBlock" --output json)
vpcId=$(echo -e "$aws_response" | /usr/bin/jq '.Vpc.VpcId' | tr -d '"')
#name the vpc
aws ec2 create-tags --resources "$vpcId" --tags Key=Name,Value="$vpcName"
#create internet gateway
gateway_response=$(aws ec2 create-internet-gateway --output json)
gatewayId=$(echo -e "$gateway_response" | /usr/bin/jq '.InternetGateway.InternetGatewayId' | tr -d '"')
#name the internet gateway
aws ec2 create-tags --resources "$gatewayId" --tags Key=Name,Value=My-Gateway
#attach gateway to vpc
attach_response=$(aws ec2 attach-internet-gateway --internet-gateway-id "$gatewayId" --vpc-id "$vpcId")
#create Public subnet for vpc with /24 cidr block
pub_subnet_response=$(aws ec2 create-subnet --cidr-block "$PubsubNetCidrBlock" --availability-zone "$pubAvailabilityZone" --vpc-id "$vpcId" --output json)
pubsubnetId=$(echo -e "$pub_subnet_response" | /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')
#name the Public subnet
aws ec2 create-tags --resources "$pubsubnetId" --tags Key=Name,Value="$pubSubnetName"
#enable public ip on public subnet
modify_response=$(aws ec2 modify-subnet-attribute --subnet-id "$pubsubnetId" --map-public-ip-on-launch)
#create Private subnet for vpc with /24 cidr block
prv_subnet_response=$(aws ec2 create-subnet --cidr-block "$PrvsubNetCidrBlock" --availability-zone "$prvAvailabilityZone" --vpc-id "$vpcId" --output json)
prvsubnetId=$(echo -e "$prv_subnet_response" | /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')
#name the Private subnet
aws ec2 create-tags --resources "$prvsubnetId" --tags Key=Name,Value="$prvSubnetName"
#create public route table for vpc
route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
pubrouteTableId=$(echo -e "$route_table_response" | /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')
#name the public route table
aws ec2 create-tags --resources "$pubrouteTableId" --tags Key=Name,Value="$PubRouteTableName"
#add route for the internet gateway
route_response=$(aws ec2 create-route --route-table-id "$pubrouteTableId" --destination-cidr-block "$destinationCidrBlock" --gateway-id "$gatewayId")
#Associate public subnet to public route table
associate_response=$(aws ec2 associate-route-table --subnet-id "$pubsubnetId" --route-table-id "$pubrouteTableId")
#create private route table for vpc
prv_route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
prvrouteTableId=$(echo -e "$prv_route_table_response" | /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')
#name the public route table
aws ec2 create-tags --resources "$prvrouteTableId" --tags Key=Name,Value="$PrvRouteTableName"
#Associate private subnet to private route table
prv_associate_response=$(aws ec2 associate-route-table --subnet-id "$prvsubnetId" --route-table-id "$prvrouteTableId")
#Allocate Elastic ip for NatGateway.
aws ec2 allocate-address --domain vpc
#Create Nategateway with association to the above created Elastic IP
aws ec2 create-nat-gateway --subnet-id "$prvsubnetId" --allocation-id eipalloc-04f4f6fdff6cefdc8
###################################
#Created on 27-Jun-2018
#Purpose : This script will Create a VPC/Subnet/Routetable/Internetgateway/Natgateway and associate them to the corresponding subnets
#Modified on : 13-Sep-2018
####################################
vpcName="My-VPC"
vpcCidrBlock="10.0.0.0/16"
PubsubNetCidrBlock="10.0.1.0/24"
PrvsubNetCidrBlock="10.0.2.0/24"
pubAvailabilityZone="ap-northeast-1a"
prvAvailabilityZone="ap-northeast-1c"
pubSubnetName="PublicSubnet-My"
prvSubnetName="PrivateSubnet-My"
PubRouteTableName="MyPublicRoute"
PrvRouteTableName="MyPrivateRoute"
destinationCidrBlock="0.0.0.0/0"
#Create a VPC with a 10.0.0.0/16 CIDR block.
aws_response=$(aws ec2 create-vpc --cidr-block "$vpcCidrBlock" --output json)
vpcId=$(echo -e "$aws_response" | /usr/bin/jq '.Vpc.VpcId' | tr -d '"')
#name the vpc
aws ec2 create-tags --resources "$vpcId" --tags Key=Name,Value="$vpcName"
#create internet gateway
gateway_response=$(aws ec2 create-internet-gateway --output json)
gatewayId=$(echo -e "$gateway_response" | /usr/bin/jq '.InternetGateway.InternetGatewayId' | tr -d '"')
#name the internet gateway
aws ec2 create-tags --resources "$gatewayId" --tags Key=Name,Value=My-Gateway
#attach gateway to vpc
attach_response=$(aws ec2 attach-internet-gateway --internet-gateway-id "$gatewayId" --vpc-id "$vpcId")
#create Public subnet for vpc with /24 cidr block
pub_subnet_response=$(aws ec2 create-subnet --cidr-block "$PubsubNetCidrBlock" --availability-zone "$pubAvailabilityZone" --vpc-id "$vpcId" --output json)
pubsubnetId=$(echo -e "$pub_subnet_response" | /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')
#name the Public subnet
aws ec2 create-tags --resources "$pubsubnetId" --tags Key=Name,Value="$pubSubnetName"
#enable public ip on public subnet
modify_response=$(aws ec2 modify-subnet-attribute --subnet-id "$pubsubnetId" --map-public-ip-on-launch)
#create Private subnet for vpc with /24 cidr block
prv_subnet_response=$(aws ec2 create-subnet --cidr-block "$PrvsubNetCidrBlock" --availability-zone "$prvAvailabilityZone" --vpc-id "$vpcId" --output json)
prvsubnetId=$(echo -e "$prv_subnet_response" | /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')
#name the Private subnet
aws ec2 create-tags --resources "$prvsubnetId" --tags Key=Name,Value="$prvSubnetName"
#create public route table for vpc
route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
pubrouteTableId=$(echo -e "$route_table_response" | /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')
#name the public route table
aws ec2 create-tags --resources "$pubrouteTableId" --tags Key=Name,Value="$PubRouteTableName"
#add route for the internet gateway
route_response=$(aws ec2 create-route --route-table-id "$pubrouteTableId" --destination-cidr-block "$destinationCidrBlock" --gateway-id "$gatewayId")
#Associate public subnet to public route table
associate_response=$(aws ec2 associate-route-table --subnet-id "$pubsubnetId" --route-table-id "$pubrouteTableId")
#create private route table for vpc
prv_route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
prvrouteTableId=$(echo -e "$prv_route_table_response" | /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')
#name the public route table
aws ec2 create-tags --resources "$prvrouteTableId" --tags Key=Name,Value="$PrvRouteTableName"
#Associate private subnet to private route table
prv_associate_response=$(aws ec2 associate-route-table --subnet-id "$prvsubnetId" --route-table-id "$prvrouteTableId")
#Allocate Elastic ip for NatGateway.
aws ec2 allocate-address --domain vpc
#Create Nategateway with association to the above created Elastic IP
aws ec2 create-nat-gateway --subnet-id "$prvsubnetId" --allocation-id eipalloc-04f4f6fdff6cefdc8
Comments