Skip to main content

Batch Script to Create Custom VPC in AWS using AWS CLI commands

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.txt
for /f  "delims=" %%i in ('findstr /L  /C:VpcId aws_output.txt') do set vpcid=%%i
set vpcid=%vpcid:*: "=%
set vpcid=%vpcid:~0,-2%
SET CIDRPublic=10.0.0.0/25
SET CIDRPrivate=10.0.0.128/25
aws ec2 create-subnet --vpc-id %vpcid% --cidr-block %CIDRPublic% > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:SubnetId aws_output.txt') do set pubsubnetid=%%i
set pubsubnetid=%pubsubnetid:*: "=%
set pubsubnetid=%pubsubnetid:~0,-2%
aws ec2 create-subnet --vpc-id %vpcid% --cidr-block %CIDRPrivate% > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:SubnetId aws_output.txt') do set privsubnetid=%%i
set privsubnetid=%privsubnetid:*: "=%
set privsubnetid=%privsubnetid:~0,-2%
aws ec2 create-internet-gateway > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:InternetGatewayId aws_output.txt') do set IGW=%%i
set 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.txt
for /f  "delims=" %%i in ('findstr /L  /C:RouteTableId aws_output.txt') do set RoutePublic=%%i
set RoutePublic=%RoutePublic:*: "=%
set RoutePublic=%RoutePublic:~0,-2%
aws ec2 create-route-table --vpc-id %vpcid% > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:RouteTableId aws_output.txt') do set RoutePrivate=%%i
set 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-launch
aws ec2 create-route --route-table-id %RoutePublic% --destination-cidr-block 0.0.0.0/0 --gateway-id %IGW%
aws ec2 allocate-address  > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:AllocationId aws_output.txt') do set EIP=%%i
set EIP=%EIP:*: "=%
set EIP=%EIP:~0,-2%
aws ec2 create-nat-gateway --subnet-id %privsubnetid% --allocation-id %EIP% > aws_output.txt
for /f  "delims=" %%i in ('findstr /L  /C:NatGatewayId aws_output.txt') do set NAT=%%i
set NAT=%NAT:*: "=%
set NAT=%NAT:~0,-2%
:NATCHECK
echo Waiting for the availability of NAT Gateway...
TIMEOUT 30
aws ec2 describe-nat-gateways > aws_output1.txt
for /f  "delims=" %%i in ('findstr /L  /C:State aws_output1.txt') do set state=%%i
set state=%state:*: "=%
set state=%state:~0,-2%
if "%state%"=="pending" GOTO NATCHECK
aws 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%

Comments

Post a Comment

Popular posts from this blog

Configure Oracle ASM Disks on AIX

Configure Oracle ASM Disks on AIX You can use below steps to configure the new disks for ASM after the raw disks are added to your AIX server by your System/Infrastructure team experts: # /usr/sbin/lsdev -Cc disk The output from this command is similar to the following: hdisk9 Available 02-T1-01 PURE MPIO Drive (Fibre) hdisk10 Available 02-T1-01 PURE MPIO Drive (Fibre) If the new disks are not listed as available, then use the below command to configure the new disks. # /usr/sbin/cfgmgr Enter the following command to identify the device names for the physical disks that you want to use: # /usr/sbin/lspv | grep -i none This command displays information similar to the following for each disk that is not configured in a volume group: hdisk9     0000014652369872   None In the above example hdisk9 is the device name and  0000014652369872  is the physical volume ID (PVID). The disks that you want to use may have a PVID, but they must not belong to a volu...

Adding New Disks to Existing ASM Disk Group

Add Disks to Existing ASM Disk Group In this blog I will show how to add new disks to an existing ASM Disk group. This also contains the steps to perform the migration from existing to the new storage system. In order to add the disk to the ASM disk group, you will first need to configure these disk using the operating system commands. I have provided the steps to configure the disks on AIX system in my blog " Configure Oracle ASM Disks on AIX" Adding New Disks to DATA Disk Group (Storage Migration for DATA Disk Group) Login to your ASM instance $ sqlplus / as sysasm If the name of the new disk is in different format from the existing disk, the modify the asm_diskstring parameter to identify the new disks. In my below example /dev/ora_data* is the format of the existing disks and /dev/new_disk* is the naming format of the newly configured disks. You should not modify this parameter unless the naming format changes. SQL> alter system set asm_diskstring = '/dev/ora_data*...

Gitlab installation steps on Redhat Linux

In this blog we will see the steps to install Gitlab on Redhat Enterprise Linux 6. I will be using the virtual machine "gitserver" that I have created on Google Cloud. You can use any server or VM running RHEL 6 and follow these steps. Follow the below steps to install gitlab. Run these steps as root user. # yum install -y curl policycoreutils-python openssh-server cronie # lokkit -s http -s ssh  # yum install postfix  # service postfix start  # chkconfig postfix on  # curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash  # EXTERNAL_URL="http://34.69.44.142" yum -y install gitlab-ee  You will see a screen similar to below, once your gitlab installation is successful. You can now access the gitlab console using the http or https url that you provided during the installation, i.e., http://<ip/server_name> http://gitserver.localdomain.com or  http://34.69.44.142 When you open the c...