Infrastructure as Code: Building a Custom Network & EC2 with user data script with Terraform.
As a Cloud Solution Architect trainee, learning Infrastructure as Code (IaC) became a key priority for scaling and automating cloud environments. This week, I built and deployed a custom AWS network using Terraform—all from my local machine with VS Code. In this post, I’ll walk you through how I created a VPC, public subnet, internet gateway, route table, and launched an EC2 instance that served a static website using a user data script. Whether you're just starting with Terraform or need a clear beginner use case, this is a friendly walkthrough of using Terraform and VS Code to launch infrastructure on AWS. Let’s dive in! Project Summary I used Terraform to automate: Building a custom VPC Creating a public subnet Launching an EC2 instance with a user data script Hosting a static website with Apache Prerequisites Before diving into this project, you should have: Basic knowledge of AWS services (EC2, VPC, Subnets, Security Groups) Terraform installed and configured AWS CLI installed and configured with IAM credentials A code editor like VS Code(with Terraform extension) Familiarity with Linux commands AWS account The main components are: A VPC with public and private subnets A Security Group to allow HTTP/SSH access An EC2 Instance with Apache and a custom HTML page deployed using user_data Architecture Overview Folder Structure terraform-project/ ├── main.tf ├── provider.tf ├── user_data.sh ├── terraform.tfstate ├── terraform.lock.hcl Configure AWS provider in provider.tf Build resources in main.tf: VPC, subnet, IGW, route table, EC2 instance Use the user data script in user_data.sh to automate Apache installation. Deploy with: terraform init terraform plan terraform apply Components VPC: A custom Virtual Private Cloud to logically isolate my resources. Public Subnet: One subnet within the VPC, configured to host public-facing resources. Internet Gateway (IGW): To allow internet access into the VPC. Route Table: Associated with the subnet to enable traffic routing through the Internet Gateway. EC2 Instance: A virtual machine launched into the subnet, configured via a user data script to automatically install Apache and serve a static website. How It All Connects The VPC is the main network boundary. Inside it, I created a public subnet. An Internet Gateway is attached to the VPC. A Route Table is created with a default route (0.0.0.0/0) pointing to the Internet Gateway and associated with the subnet. An EC2 instance is launched in the public subnet. A user data script runs on the instance at launch, installing Apache and copying my static website files. The instance is reachable via its **public IP **because: It’s in a public subnet. It has a public IP association enabled. The Security Group allows HTTP access on port 80. Troubleshooting Errors: I got so many errors from my scripts, but I didn't give up, I kept trying I checked the console, and my instance was running. did the same when I built a custom network. And here's the result Cleaning Up If you want to avoid AWS charges: terraform destroy What I Learned How to set up infrastructure using code How to work with user data scripts for automation How to break a project into modular files How powerful and addictive Infrastructure as Code can be. What’s Next? I'll expand this by: Creating a private subnet. Launching EC2 in a private network. Creating and associating a NAT Gateway. Using Elastic IP for NAT. Automating more with modules. Conclusion This was my first hands-on Terraform project, and it gave me real insight into the power of infrastructure as code (IaC). Being able to provision a complete environment with a few files and commands felt like magic and was less intimidating. Every challenge helped me grow, and I can’t wait to complete the rest of the architecture. If you're just starting, try building a similar setup and share your experience. Feel free to drop questions or thoughts in the comments!" Let's Connect I'm learning Cloud Solutions Architecture and sharing every step. Follow me here and on https://www.linkedin.com/in/glory-ugochukwu-customer-support-speacialist/ for more beginner-friendly content!

As a Cloud Solution Architect trainee, learning Infrastructure as Code (IaC) became a key priority for scaling and automating cloud environments. This week, I built and deployed a custom AWS network using Terraform—all from my local machine with VS Code. In this post, I’ll walk you through how I created a VPC, public subnet, internet gateway, route table, and launched an EC2 instance that served a static website using a user data script.
Whether you're just starting with Terraform or need a clear beginner use case, this is a friendly walkthrough of using Terraform and VS Code to launch infrastructure on AWS. Let’s dive in!
Project Summary
I used Terraform to automate:
- Building a custom VPC
- Creating a public subnet
- Launching an EC2 instance with a user data script
- Hosting a static website with Apache
Prerequisites
Before diving into this project, you should have:
Basic knowledge of AWS services (EC2, VPC, Subnets, Security Groups)
- Terraform installed and configured
- AWS CLI installed and configured with IAM credentials
- A code editor like VS Code(with Terraform extension)
- Familiarity with Linux commands
- AWS account
The main components are:
- A VPC with public and private subnets
- A Security Group to allow HTTP/SSH access
- An EC2 Instance with Apache and a custom HTML page deployed using user_data
Architecture Overview
Folder Structure
terraform-project/
├── main.tf
├── provider.tf
├── user_data.sh
├── terraform.tfstate
├── terraform.lock.hcl
- Configure AWS provider in provider.
tf
- Build resources in main.
tf
: VPC, subnet, IGW, route table, EC2 instance - Use the user data script in user_data.sh to automate Apache installation.
Deploy with:
terraform init
terraform plan
terraform apply
Components
- VPC: A custom Virtual Private Cloud to logically isolate my resources.
-
Public Subnet: One subnet within the VPC, configured to host public-facing resources.
- Internet Gateway (IGW): To allow internet access into the VPC.
- Route Table: Associated with the subnet to enable traffic routing through the Internet Gateway.
- EC2 Instance: A virtual machine launched into the subnet, configured via a user data script to automatically install Apache and serve a static website.
How It All Connects
- The VPC is the main network boundary.
- Inside it, I created a public subnet.
- An Internet Gateway is attached to the VPC.
- A Route Table is created with a default route (0.0.0.0/0) pointing to the Internet Gateway and associated with the subnet.
- An EC2 instance is launched in the public subnet.
- A user data script runs on the instance at launch, installing Apache and copying my static website files.
- The instance is reachable via its **public IP **because:
- It’s in a public subnet.
- It has a public IP association enabled.
- The Security Group allows HTTP access on port 80.
Troubleshooting Errors:
I got so many errors from my scripts, but I didn't give up, I kept trying
I checked the console, and my instance was running. did the same when I built a custom network.
And here's the result
Cleaning Up
If you want to avoid AWS charges:
terraform destroy
What I Learned
- How to set up infrastructure using code
- How to work with user data scripts for automation
- How to break a project into modular files
- How powerful and addictive Infrastructure as Code can be.
What’s Next?
I'll expand this by:
- Creating a private subnet.
- Launching EC2 in a private network.
- Creating and associating a NAT Gateway.
- Using Elastic IP for NAT.
- Automating more with modules.
Conclusion
This was my first hands-on Terraform project, and it gave me real insight into the power of infrastructure as code (IaC). Being able to provision a complete environment with a few files and commands felt like magic and was less intimidating. Every challenge helped me grow, and I can’t wait to complete the rest of the architecture.
If you're just starting, try building a similar setup and share your experience. Feel free to drop questions or thoughts in the comments!"
Let's Connect
I'm learning Cloud Solutions Architecture and sharing every step.
Follow me here and on https://www.linkedin.com/in/glory-ugochukwu-customer-support-speacialist/ for more beginner-friendly content!