This article provides you a windows batch script which creates following resources under your AWS account. You can download this script from the link "Batch Script to Create Custom VPC"
AWS Resources Created by the script
- VPC with CIDR prefix 10.0.0.0/24
- Public Subnet with CIDR prefix 10.0.0.0/25
- Private Subnets with CIDR prefix 10.0.0.128/25
- Internet Gateway
- Routing Tables for public and private subnets
- NAT Gateway
- Elastic IP
- EC2 instance 1 of type t2.micro in private subnet
- EC2 instance 2 of type t2.micro in public subnet
Please ensure to delete these resources when you don't need them or else you will be charged by Amazon as per the pricing policy for these services.
Pre-requisites:
This program uses AWS command line interface. If you do not have it already, then install AWS CLI before running this script. You can download and install AWS CLI using the AWS CLI Installer Link.
Follow my previous blog post How to install and configure AWS CLI to follow the steps to configure AWS CLI on windows.
Windows Batch Script Code:
- Copy the below code and put it in a text file. Save the text file with .bat extension.
- If you want your VPC in different CDR range, then modify the CIDR prefixes at line# 1,6 & 7.
- During the EC2 instance creation in last 2 lines, change the name of the private key file. This script uses the private key file name as "myec2key"
Contents of batch script VPC_Setup.bat
SET CIDR=10.0.0.0/24
aws ec2 create-vpc --cidr-block %CIDR% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:VpcId aws_output.txt') do set vpcid=%%iset vpcid=%vpcid:*: "=%set vpcid=%vpcid:~0,-2%SET CIDRPublic=10.0.0.0/25SET CIDRPrivate=10.0.0.128/25aws ec2 create-subnet --vpc-id %vpcid% --cidr-block %CIDRPublic% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:SubnetId aws_output.txt') do set pubsubnetid=%%iset pubsubnetid=%pubsubnetid:*: "=%set pubsubnetid=%pubsubnetid:~0,-2%aws ec2 create-subnet --vpc-id %vpcid% --cidr-block %CIDRPrivate% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:SubnetId aws_output.txt') do set privsubnetid=%%iset privsubnetid=%privsubnetid:*: "=%set privsubnetid=%privsubnetid:~0,-2%aws ec2 create-internet-gateway > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:InternetGatewayId aws_output.txt') do set IGW=%%iset IGW=%IGW:*: "=%set IGW=%IGW:~0,-2%aws ec2 attach-internet-gateway --vpc-id %vpcid% --internet-gateway-id %IGW%aws ec2 create-route-table --vpc-id %vpcid% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:RouteTableId aws_output.txt') do set RoutePublic=%%iset RoutePublic=%RoutePublic:*: "=%set RoutePublic=%RoutePublic:~0,-2%aws ec2 create-route-table --vpc-id %vpcid% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:RouteTableId aws_output.txt') do set RoutePrivate=%%iset RoutePrivate=%RoutePrivate:*: "=%set RoutePrivate=%RoutePrivate:~0,-2%aws ec2 associate-route-table --subnet-id %pubsubnetid% --route-table-id %RoutePublic%aws ec2 associate-route-table --subnet-id %privsubnetid% --route-table-id %RoutePrivate%aws ec2 modify-subnet-attribute --subnet-id %privsubnetid% --map-public-ip-on-launchaws ec2 create-route --route-table-id %RoutePublic% --destination-cidr-block 0.0.0.0/0 --gateway-id %IGW%aws ec2 allocate-address > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:AllocationId aws_output.txt') do set EIP=%%iset EIP=%EIP:*: "=%set EIP=%EIP:~0,-2%aws ec2 create-nat-gateway --subnet-id %privsubnetid% --allocation-id %EIP% > aws_output.txtfor /f "delims=" %%i in ('findstr /L /C:NatGatewayId aws_output.txt') do set NAT=%%iset NAT=%NAT:*: "=%set NAT=%NAT:~0,-2%:NATCHECKecho Waiting for the availability of NAT Gateway...TIMEOUT 30aws ec2 describe-nat-gateways > aws_output1.txtfor /f "delims=" %%i in ('findstr /L /C:State aws_output1.txt') do set state=%%iset state=%state:*: "=%set state=%state:~0,-2%if "%state%"=="pending" GOTO NATCHECKaws ec2 create-route --route-table-id %RoutePrivate% --destination-cidr-block 0.0.0.0/0 --gateway-id %NAT%aws ec2 run-instances --image-id ami-0810abbfb78d37cdf --count 1 --instance-type t2.micro --key-name myec2key --subnet-id %pubsubnetid%aws ec2 run-instances --image-id ami-0810abbfb78d37cdf --count 1 --instance-type t2.micro --key-name myec2key --subnet-id %privsubnetid%
Awesome !!
ReplyDelete