Skip to content

Instantly share code, notes, and snippets.

@jkeam
Last active September 9, 2023 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeam/1d28a28111047c3d2825b6b649f61b81 to your computer and use it in GitHub Desktop.
Save jkeam/1d28a28111047c3d2825b6b649f61b81 to your computer and use it in GitHub Desktop.
Create AWS VPC and Subnet using AWS CLI and Bash
#!/bin/bash
# taken from: https://brad-simonin.medium.com/create-an-aws-vpc-and-subnet-using-the-aws-cli-and-bash-a92af4d2e54b
export AWS_ACCESS_KEY_ID=something
export AWS_SECRET_ACCESS_KEY=something
export AWS_DEFAULT_REGION=us-east-1
availabilityZone="us-east-1a"
name="Something"
vpcName="$name VPC"
subnetName="$name Subnet"
gatewayName="$name Gateway"
routeTableName="$name Route Table"
securityGroupName="$name Security Group"
vpcCidrBlock="10.0.0.0/16"
subNetCidrBlock="10.0.1.0/24"
port22CidrBlock="0.0.0.0/0"
destinationCidrBlock="0.0.0.0/0"
echo "Creating VPC..."
#create vpc with cidr block /16
aws_response=$(aws ec2 create-vpc \
--cidr-block "$vpcCidrBlock" \
--output json)
vpcId=$(echo -e "$aws_response" | jq '.Vpc.VpcId' | tr -d '"')
#name the vpc
aws ec2 create-tags \
--resources "$vpcId" \
--tags Key=Name,Value="$vpcName"
#add dns support
modify_response=$(aws ec2 modify-vpc-attribute \
--vpc-id "$vpcId" \
--enable-dns-support "{\"Value\":true}")
#add dns hostnames
modify_response=$(aws ec2 modify-vpc-attribute \
--vpc-id "$vpcId" \
--enable-dns-hostnames "{\"Value\":true}")
#create internet gateway
gateway_response=$(aws ec2 create-internet-gateway \
--output json)
gatewayId=$(echo -e "$gateway_response" | jq '.InternetGateway.InternetGatewayId' | tr -d '"')
#name the internet gateway
aws ec2 create-tags \
--resources "$gatewayId" \
--tags Key=Name,Value="$gatewayName"
#attach gateway to vpc
attach_response=$(aws ec2 attach-internet-gateway \
--internet-gateway-id "$gatewayId" \
--vpc-id "$vpcId")
#create subnet for vpc with /24 cidr block
subnet_response=$(aws ec2 create-subnet \
--cidr-block "$subNetCidrBlock" \
--availability-zone "$availabilityZone" \
--vpc-id "$vpcId" \
--output json)
subnetId=$(echo -e "$subnet_response" | jq '.Subnet.SubnetId' | tr -d '"')
#name the subnet
aws ec2 create-tags \
--resources "$subnetId" \
--tags Key=Name,Value="$subnetName"
#enable public ip on subnet
modify_response=$(aws ec2 modify-subnet-attribute \
--subnet-id "$subnetId" \
--map-public-ip-on-launch)
#create security group
security_response=$(aws ec2 create-security-group \
--group-name "$securityGroupName" \
--description "Private: $securityGroupName" \
--vpc-id "$vpcId" --output json)
groupId=$(echo -e "$security_response" | jq '.GroupId' | tr -d '"')
#name the security group
aws ec2 create-tags \
--resources "$groupId" \
--tags Key=Name,Value="$securityGroupName"
#enable port 22
security_response2=$(aws ec2 authorize-security-group-ingress \
--group-id "$groupId" \
--protocol tcp --port 22 \
--cidr "$port22CidrBlock")
#create route table for vpc
route_table_response=$(aws ec2 create-route-table \
--vpc-id "$vpcId" \
--output json)
routeTableId=$(echo -e "$route_table_response" | jq '.RouteTable.RouteTableId' | tr -d '"')
# update default route table for new vpc
# routeTableId=$(aws ec2 describe-route-tables | jq '.RouteTables[]' | jq --arg vpcId "$vpcId" 'select(.VpcId==$vpcId)' | jq '.RouteTableId' | tr -d '"')
#name the route table
aws ec2 create-tags \
--resources "$routeTableId" \
--tags Key=Name,Value="$routeTableName"
#add route for the internet gateway
route_response=$(aws ec2 create-route \
--route-table-id "$routeTableId" \
--destination-cidr-block "$destinationCidrBlock" \
--gateway-id "$gatewayId")
#add route to subnet
associate_response=$(aws ec2 associate-route-table \
--subnet-id "$subnetId" \
--route-table-id "$routeTableId")
echo " "
echo "VPC created:"
echo "Use subnet id $subnetId and security group id $groupId"
echo "To create your AWS instances"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment