Setting Up a VPC on AWS: A Guide with AWS CLI

Setting Up a VPC on AWS: A Guide with AWS CLI

When setting up infrastructure on AWS, the first step is often the creation of a Virtual Private Cloud (VPC). Essentially, a VPC allows users to define a virtual network in which AWS resources can be launched. This is similar to a traditional on-premises network, but with the flexibility and scalability that AWS offers.

Visualizing a Simple VPC Architecture

Within VPCs, there's a spectrum of complexity. The architecture we're discussing here is a simple, yet effective, VPC design suitable for many general use cases. A standard simple VPC includes a mix of public and private subnets, an internet gateway, and associated route tables.

Visualizing a Simple VPC Architecture - public an private subnets

Having a visual representation of the architecture aids in design, planning, and communication, especially when presenting configurations to stakeholders.

Multiple Approaches to Creating a VPC

There's no one-size-fits-all when it comes to VPC creation. AWS offers multiple methods, each tailored to different scenarios:

  1. AWS Management Console: A web interface that offers an easy-to-use, graphical representation of your AWS environment.

  2. AWS Command Line Interface (CLI): For those who prefer command-line tools or need to script their AWS operations.

  3. AWS SDKs: Integrates AWS services directly into your application with the help of various language-specific libraries.

  4. AWS CloudFormation: A service that helps you model and set up your Amazon Web Services resources using Infrastructure as Code (IaC).

  5. Third-party tools: Tools like Terraform also offer the capability to define and provide data center infrastructure using declarative configuration files.

Quick VPC Setups with AWS CLI

For rapid development, testing, or proof of concept (POC) purposes, the AWS CLI can be invaluable. It provides a swift method to instantiate resources without the overhead of graphical interfaces or extensive configuration files.

Here's a sample script that uses the AWS CLI to set up a VPC, including public and private subnets:

#!/bin/bash

### Step 1: Configuration and Initialization ###

# Retrieve the default AWS region or set manually
REGION=$(aws configure get region) # Get default region
# REGION="us-west-1"  # Uncomment and update this value as needed if you want to set region manually

### Step 2: Create a Virtual Private Cloud (VPC) ###

VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --region $REGION | jq -r '.Vpc.VpcId')
echo "Created VPC with ID: $VPC_ID"

### Step 3: Create Public Subnets ###

SUBNET_ID_1=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.1.0/24 --availability-zone ${REGION}a --region $REGION | jq -r '.Subnet.SubnetId')
echo "Created public subnet with ID: $SUBNET_ID_1 in ${REGION}a"

SUBNET_ID_2=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.2.0/24 --availability-zone ${REGION}b --region $REGION | jq -r '.Subnet.SubnetId')
echo "Created public subnet with ID: $SUBNET_ID_2 in ${REGION}b"

### Step 4: Create Private Subnets ###

SUBNET_ID_3=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.3.0/24 --availability-zone ${REGION}a --region $REGION | jq -r '.Subnet.SubnetId')
echo "Created private subnet with ID: $SUBNET_ID_3 in ${REGION}a"

SUBNET_ID_4=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.4.0/24 --availability-zone ${REGION}b --region $REGION | jq -r '.Subnet.SubnetId')
echo "Created private subnet with ID: $SUBNET_ID_4 in ${REGION}b"

### Step 5: Create and Attach an Internet Gateway ###

IGW_ID=$(aws ec2 create-internet-gateway --region $REGION | jq -r '.InternetGateway.InternetGatewayId')
echo "Created Internet Gateway with ID: $IGW_ID"

aws ec2 attach-internet-gateway --vpc-id $VPC_ID --internet-gateway-id $IGW_ID
echo "Attached Internet Gateway to VPC: $VPC_ID"

### Step 6: Setup Public Route Table ###

PUBLIC_ROUTE_TABLE_ID=$(aws ec2 create-route-table --vpc-id $VPC_ID --region $REGION | jq -r '.RouteTable.RouteTableId')
echo "Created Public Route Table with ID: $PUBLIC_ROUTE_TABLE_ID"

# Add a route to the Internet Gateway
aws ec2 create-route --route-table-id $PUBLIC_ROUTE_TABLE_ID --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_ID
echo "Added route to the Public Route Table"

# Associate the public subnets with the route table
aws ec2 associate-route-table --subnet-id $SUBNET_ID_1 --route-table-id $PUBLIC_ROUTE_TABLE_ID
echo "Associated public subnet $SUBNET_ID_1 with Public Route Table"

aws ec2 associate-route-table --subnet-id $SUBNET_ID_2 --route-table-id $PUBLIC_ROUTE_TABLE_ID
echo "Associated public subnet $SUBNET_ID_2 with Public Route Table"

### Finalization ###
# Note: No need to create a separate private route table since a default main route table is created with the VPC."

echo "Setup complete!"

Note: This script assumes that each command succeeds. You would want to add error-checking and handling mechanisms.

Note: Before executing scripts, especially in production environments, ensure you fully understand each command and its implications, including any potential associated costs.

Before you run this script:

  1. Ensure you have jq installed. If not, you can get it using package managers like apt, yum, or brew.

  2. Make sure your AWS CLI is properly configured with the right credentials and default region.

  3. Make the script executable using chmod +x script_name.sh.

Run the script using ./script_name.sh

The Production Ideal: Infrastructure as Code (IaC)

For a production environment, it's best practice to use Infrastructure as Code (IaC). With IaC, you can ensure consistency, repeatability, and can version-control your infrastructure setup. This approach is especially valuable in a DevOps environment where infrastructure changes might be frequent, and you'd want an auditable trail of what changes were made, by whom, and why.

Tools like AWS CloudFormation or Terraform offer robust capabilities to define, deploy, and systematically manage AWS infrastructure.


Cleanup: Removing the AWS Resources

After setting up and experimenting with your VPC, it's essential to remove any resources you're no longer using. This helps avoid incurring unnecessary charges. Follow these steps to clean up the resources you've created:

1. Delete the Internet Gateway:

Before you can delete the VPC, you'll need to detach and remove the Internet Gateway.

# Detach the Internet Gateway from the VPC
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID --region $REGION

# Delete the Internet Gateway
aws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID --region $REGION

2. Delete the Subnets:

Before deleting the VPC, all associated subnets need to be removed.

aws ec2 delete-subnet --subnet-id $SUBNET_ID_1 --region $REGION
aws ec2 delete-subnet --subnet-id $SUBNET_ID_2 --region $REGION
aws ec2 delete-subnet --subnet-id $SUBNET_ID_3 --region $REGION
aws ec2 delete-subnet --subnet-id $SUBNET_ID_4 --region $REGION

3. Delete the Route Table:

The main route table that comes with the VPC doesn't get deleted, but if you've created any custom route tables, you need to delete them.

aws ec2 delete-route-table --route-table-id $PUBLIC_ROUTE_TABLE_ID --region $REGION

4. Delete the VPC:

Finally, you can delete the VPC:

aws ec2 delete-vpc --vpc-id $VPC_ID --region $REGION

Note: Ensure all associated resources within the VPC (like EC2 instances, security groups, etc.) have been terminated or disassociated before deleting the VPC.


Instead of running the above commands, you can alternatively use the AWS Management Console:

  1. Go to the VPC Dashboard.

  2. In the left navigation pane, click on 'Your VPCs'.

  3. Locate the VPC you want to delete, select it, and then click on 'Actions', and choose the 'Delete VPC' button.

  4. AWS will handle the deletion of associated resources like subnets, and the attached Internet Gateway, for that VPC.

  5. Ensure you review and confirm the deletion prompts.

It's always good to double-check that no stray resources remain to avoid incurring unnecessary costs.

In Conclusion

Creating a VPC in AWS, even a simple one, requires attention to detail. This guide offers a glimpse into a basic VPC architecture, but the AWS ecosystem provides much more depth and flexibility. As you explore further, prioritize understanding, accuracy, and best practices to ensure that your cloud infrastructure remains robust, scalable, and secure.