This blog demonstrate how we can create a Virtual Private Cloud (VPC) in AWS cloud using a bash script. In this blog, we will break down the steps with its explanation. So, lets go through it.
- An AWS account
- To create an AWS account click here.
- Install AWS CLI
- Install Command-line JSON processor (jq)
- To install jq click here (only for Linux).
2. Let’s create a VPC
VPC is a secure and isolated cloud similar to traditional networks hosted within a public cloud environment.
Step 1: VPC creation
To create a VPC, use the following command:
Here, we use create-vpc command of ec2 service with the option –cidr-block use to define Classless Inter-Domain Routing range which is stored in the global variable VPC_CIDR_BLOCK, –-region is used to define the region where we want our VPC to be created and is stored in the global variable REGION and option –output is for formatting style for command output.
Finally, lets store its output in variable “awsResponse”, which will be used to get the vpc_id using JSON pre-processor (jq) and stores the VpcId in “vpcId” variable. This VpcId will be used later to link our resource.
Step 2: Tag the VPC
Let’s tag our VPC, to easily identify it once we start having multiple VPCs, to do this run the following command:
Here, we use create-tags command of ec2 service with the options –resources used to refer to our vpcId variable containing our Vpc Id,–tags have two fields first one is the Key which will have the key of the tag and Value which will carry the value of the tag stored in the global variable VPC_TAG_NAME and–region is same as before.
Step 3: Describe the VPC
Let Describe this VPC, to get the information about the created VPC using following command:
Here, we use describe-vpcs command of ec2 service with the options –vpc-ids used to refer to our vpcId variable containing our Vpc Id and –region is same as before. The output of which is stored in “vpcDescription” and later parsed with JSON pre-processor (jq) to get the below description.
Output of describe-vpc :
In the above image, you can get the information like “CidrBlock”, “State”, “VpcId”, “Tags” etc. about our created VPC.
Step 4: Let’s up it altogether in Bash script
#!/bin/bash # Define global variables for VPC creation REGION='ap-south-1' VPC_TAG_NAME='aws_Cli_VPC_tagName' VPC_CIDR_BLOCK='10.1.0.0/16' # VPC creating message echo "Creating VPC..." # creating vpc and storing response awsResponse=$(aws ec2 create-vpc \ --cidr-block "$VPC_CIDR_BLOCK" \ --region "$REGION" \ --output json) # getting vpcId vpcId=$(echo -e "$awsResponse" | \ /usr/bin/jq '.Vpc.VpcId' | \ tr -d '"') sleep 2 # tagging vpc echo "Tagging vpc..." aws ec2 create-tags \ --resources "$vpcId" \ --tags Key=Name,Value="$VPC_TAG_NAME" \ --region "$REGION" # describe vpcId echo "VPC description:" vpcDescription=$(aws ec2 describe-vpcs \ --vpc-ids "$vpcId" \ --region "$REGION") echo $vpcDescription | /usr/bin/jq
To run this script, give its executable permission and type ./<your_script_name>.sh in your terminal. This is what it looks in AWS Console.
So, In this blog we went through the process of AWS VPC creation using AWS CLI. We can also add public subnets, private subnets, internet gateway, elastic IP for NAT gateway and many more. Thanks for being with us till the end. If you find this blog helpful do share with your friends.