Tech Mesha

Create AWS VPC with Subnets using Terraform - Infra As Code

· TechMesha

AWS-VPC-with-Subnets
In this article, I’ll walk you through the simple steps of creating an AWS Virtual Private Cloud (VPC) with both public and private subnets using Terraform, while ensuring we follow the best practices.

AWS VPC Component

Below is a list of components we are going to create:

  • One VPC
  • One private subnets
  • One public subnets
  • One internet gateway for public internet traffic
  • One route table
  • One route table association

Let’s Start!

1. Create your working folder

I have chosen AWS VPC - Terraform

2. Create your provider

Screenshot-from-2024-03-20-22-17-45

Let’s start by creating a file called “provider.tf”. This file will specify that all our infrastructure will be hosted on AWS. If you ever want to switch to a different cloud provider like Google Cloud Platform (GCP) or Microsoft Azure, you’ll need to update this file accordingly.

We are ready to init!

After initialization, your project folder should contain two files and one folder.

3. Create your vpc

resource "aws_vpc" "myvpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    name = "MyTerraformVPC"
  }
}

4. Create your public subnet

resource "aws_subnet" "PublicSubnet" {
  vpc_id     = aws_vpc.myvpc.id
  cidr_block = "10.0.1.0/24"
  tags = {
    Name = "subnet1"
  }
}

5. Create your private subnet

resource "aws_subnet" "PrivateSubnet" {
  vpc_id     = aws_vpc.myvpc.id
  cidr_block = "10.0.2.0/24"
  tags = {
    Name = "subnet2"
  }
}

6. Create internet gateway

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.myvpc.id
}

7. Create route table for public subnet

resource "aws_route_table" "PublicRT" {
  vpc_id = aws_vpc.myvpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
}

8. Create route table association for public subnet

resource "aws_route_table_association" "PublicRTassociation" {
  subnet_id      = aws_subnet.PublicSubnet.id
  route_table_id = aws_route_table.PublicRT.id
}

Screenshot-from-2024-03-20-22-17-29

Once you’ve completed the previous step, head over to your terminal and run the command “terraform plan” to ensure that everything is set up correctly and there are no issues.

After verifying the plan, you can proceed by executing “terraform apply” to implement the changes and create your AWS VPC with the specified configurations.

Screenshot-from-2024-03-20-22-18-22

Now go to your AWS Console

we can see here that our VPC is working successfuly

Screenshot-from-2024-03-20-22-14-19

Here, our 2 subnets are also working successfuly

Screenshot-from-2024-03-20-22-14-54

Route table

Screenshot-from-2024-03-20-22-15-54

And lastly your internet gateway

Screenshot-from-2024-03-20-22-16-22

Final Thoughts

In conclusion, setting up an AWS VPC with both public and private subnets using Terraform can be a straightforward process when following best practices and utilizing variables for flexibility. By carefully planning and executing each step, you can create a well-structured and efficient infrastructure environment tailored to your specific needs.. Happy Terraforming!