INTRODUCTION
This guide walks you through creating a fully functional custom VPC (Virtual Private Cloud) on AWS using Terraform. The configuration includes a VPC, internet gateway, public route table, and dynamically created public subnets distributed across availability zones.

Prerequisites
Before you begin, ensure you have the following tools and configurations in place:

🛠️ Tools Installed

  1. Terraform (v1.0 or above recommended)

  2. AWS CLI (configured with appropriate credentials)

  3. Code Editor (e.g., VS Code, Sublime Text)

  4. AWS account

🔧 Provider Configuration

Image description

Initializes the AWS provider for Terraform, setting the region dynamically via a variable.

🌍 Fetching Availability Zones

Image description

Fetches the list of currently available Availability Zones in the region for resilient subnet placement.

🛠️ VPC Creation

Image description

🌐 Internet Gateway

Image description

Attaches an internet gateway to your VPC, enabling external internet access.

🗺️ Public Route Table

Image description

Creates a route table with an internet route (0.0.0.0/0) pointing to the IGW.

🧱 Dynamic Public Subnets
Dynamically provisions public subnets across multiple Availability Zones using CIDR subnetting.

Image description

🔁 Route Table Associations

Associates each public subnet with the public route table to ensure internet connectivity.

Image description

📦 Variables to Define
Add the following to your variables.tf file:

Image description

You can find and copy this code from my repository: https://github.com/heritageolaleye/terraform_demo

🎉 Conclusion
You now have a dynamic, production-ready AWS VPC setup that includes public subnets distributed across availability zones with internet access configured. This is a great foundation for hosting web applications, microservices, or any cloud-native infrastructure.