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
Terraform (v1.0 or above recommended)
AWS CLI (configured with appropriate credentials)
Code Editor (e.g., VS Code, Sublime Text)
AWS account
🔧 Provider Configuration
Initializes the AWS provider for Terraform, setting the region dynamically via a variable.
🌍 Fetching Availability Zones
Fetches the list of currently available Availability Zones in the region for resilient subnet placement.
🛠️ VPC Creation
🌐 Internet Gateway
Attaches an internet gateway to your VPC, enabling external internet access.
🗺️ Public Route Table
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.
🔁 Route Table Associations
Associates each public subnet with the public route table to ensure internet connectivity.
📦 Variables to Define
Add the following to your variables.tf file:
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.