AWS VPC With One Subnet

Sign Up to Build

About this Architecture

Here is some information about this architecture.

How to Build This Solution

Here are the steps you can follow to build this solution on your own.

Intro

In this lab you'll learn how to create a VPC with a single subnet. You'll also learn about how to configure the VPC with the correct network addressing space. Let's go!

Intro to VPC

A Virtual Private Cloud (VPC) in AWS provides a private, isolated section of the Amazon Web Services (AWS) Cloud where you can launch AWS resources within a defined virtual network. It provides advanced security features and allows you to have control over your virtual networking environment, including the selection of your IP address range, creation of subnets, and configuration of route tables and network gateways.

CIDR Notation

VPC and subnets are defined using CIDR (Classless Inter-Domain Routing) notation. CIDR allows for flexible IP address grouping. In the context of VPC, it allows you to specify an IP address range for the whole VPC, and then subnets within that range.

For instance, if you use 10.0.0.0/16 as your VPC CIDR, it means that you have a range of 10.0.0.0 to 10.0.255.255. Within this range, you can create subnets.

Subnets

A subnet is a range within the CIDR block of your VPC. Subnets allow you to segment your network, and you can place network rules around them for better isolation and control.

If you decide to use 10.0.1.0/24 for your subnet, it must fall within the CIDR block of the VPC (10.0.0.0/16), and it means this subnet will have IP addresses ranging from 10.0.1.0 to 10.0.1.255.

Now, let's create a VPC with one subnet in the us-west-2 region using Terraform!

Lab: Create a VPC with a Single Subnet

Get Your AWS Credentials

If you're using the Skillmix Labs feature, open the lab settings (the beaker icon) on the right side of the code editor. Then, click the Start Lab button to start hte lab environment.

Wait for the credentials to load. Then run this in the terminal:

$ aws configure --profile smx-lab
AWS Access Key ID [None]: AKIA3E3W34P42CSHXDH5
AWS Secret Access Key [None]: vTmqpOqefgJfse8i6QwzgpjgswPjHZ6h/oiQq4zf
Default region name [None]: us-west-2
Default output format [None]: json

Be sure to name your credentials profile 'smx-lab'.

Note: If you're using your own AWS account you'll need to ensure that you've created and configured a named AWS CLI profile named smx-lab.

Create the main.tf FIle

We'll do all of our work in a file called main.tf. Create that file in your root project directory now and open it for editing.

Create the Terraform Config

First, let's create the Terraform config. This config will set the required Terraform version, and require the AWS Provider at a certain version.

Add this to the main.tf file.

terraform {
  required_version = ">= 1.5.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

Explanation:

  • required_version = ">= 1.5.0" ensures that the version of Terraform used to apply this configuration is at least 1.5.0. If a lower version is used, Terraform will return an error.

  • required_providers is used to specify the required provider plugins and their versions. In this case, it is specifying that the AWS provider from HashiCorp must be used, and the version should be at least 3.0 (or a compatible version). The tilde and greater-than symbol (~>) allows for updates that do not include breaking changes according to semantic versioning.

Create the AWS Provider Configuration

Next we'll create the AWS Provider config. Append the following code to the main.tf file.

provider "aws" {
  profile = "smx-lab"
  region = "us-west-2"
}

Explanation: This block sets up the AWS provider and specifies the region where the resources will be created. By setting region = "us-west-2", we are telling Terraform to create the resources in the Oregon region of AWS.

Create the VPC Definition

Next, we'll create the VPC config. Append this code to the main.tf file.

resource "aws_vpc" "my_vpc" {
  cidr_block       = "10.0.0.0/16"
  tags = {
    Name = "my_vpc"
  }
}

Explanation: This code creates a VPC (Virtual Private Cloud) with a CIDR block of 10.0.0.0/16. The CIDR block specifies the IP range for the entire VPC. The tag Name helps in identifying the VPC within the AWS console or when querying via CLI/API.

Create the Subnet Definition

Next we'll create the Subnet resource. Append this code to the main.tf file.

resource "aws_subnet" "my_subnet" {
  vpc_id                  = aws_vpc.my_vpc.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-west-2a"
  tags = {
    Name = "my_subnet"
  }
}

Explanation: This block creates a subnet within the VPC defined earlier. The vpc_id attribute links this subnet to the VPC, and cidr_block defines the IP range for the subnet, which falls within the range of the VPC. The availability_zone attribute specifies in which availability zone the subnet will reside (us-west-2a in this case). Similar to the VPC, a Name tag is added for identification purposes.

That's it! You've successfully created a VPC with a subnet in the us-west-2 region using Terraform. This lab should give you a practical understanding of VPCs, subnets, and how CIDR notation is used to define them within AWS.

Feel free to modify the CIDR blocks to create different network designs, and don't forget to tear down the resources when you're done experimenting to avoid unnecessary charges.

Summary

We've covered the foundational concepts of AWS VPC, including CIDR notation and subnet creation. By using Terraform, you can manage these resources as code, providing repeatability and scalability in your cloud infrastructure. Happy coding!