The Git Repo
As I deepened my understanding of DevOps and cloud fundamentals, I wanted to get hands-on with provisioning infrastructure the right way: using Infrastructure as Code (IaC). In this post, Iβll walk through how I built a reusable Terraform project to provision an EC2 instance, attach a load balancer, configure security groups, and get it all running on AWS.
This project was part of my journey in creating modular and production-oriented DevOps blueprints.
π§° Tools & Tech
- Terraform for IaC
- AWS EC2 for compute resources
- Application Load Balancer (ALB) for routing
- Security Groups for access control
- Remote backend support with Terraform state files
π¦ Project Structure
I broke the configuration into reusable, modular Terraform components to make the codebase scalable and production-ready. Here's how the structure looked:
.
βββ main.tf # Orchestrator: may call modules or glue everything together
βββ variables.tf # All variable declarations with types and descriptions
βββ outputs.tf # Output values (e.g., IPs, DNS names, ARNs)
βββ vpc.tf # VPC, subnets, internet gateway, etc.
βββ ec2.tf # EC2 instance(s), AMIs, key pairs, EBS volumes
βββ alb.tf # Application Load Balancer, listeners, target groups
βββ security.tf # Security groups, network ACLs, firewall rules
βββ README.md # Project documentation
βββ .gitignore # Files to exclude from Git (e.g., `.terraform`, `*.tfstate`)
Each module encapsulates a piece of the infrastructure (e.g., EC2, security group), keeping things clean and reusable.
ποΈ What It Provisions
When executed, the Terraform code provisions:
- A public subnet in a selected region
- An EC2 instance with user data for bootstrapping
- A Security Group that allows inbound traffic on ports 22 and 80
- An Application Load Balancer (ALB) that distributes HTTP traffic
- Target group + listener configuration for the EC2 instance
π§ͺ How to Use It
Clone the repo and run the following inside the root directory:
terraform init
terraform plan
terraform apply
Make sure your AWS credentials are set in your environment or shared credentials file.
Once applied, the EC2 instance and ALB will be up and running. Youβll get the public DNS of the load balancer in the Terraform output.
β Outcome
With a single command, I spun up a complete production-grade architecture using Terraform. Itβs scalable, reusable, and can easily be extended to include databases, autoscaling, and monitoring.
The architecture diagram is:
π Key Learnings
- Writing modular Terraform code is essential for maintainability
- ALBs are ideal for HTTP/HTTPS workloads with flexible routing
- Outputs and variables improve reusability and flexibility
- Infrastructure automation saves time and reduces errors
π§ Next Steps
Iβm planning to:
- Add support for private subnets and NAT gateways
- Integrate with RDS or DynamoDB
- Add Terraform Cloud remote backend
- Extend this into a full production deployment pipeline
π Resources
- GitHub Repo: Terraform AWS EC2 Load Balancer Deployment
βοΈ Let's Connect
If you're building something cloud-native or want help setting up secure AWS infrastructure, Iβd love to chat.
Top comments (0)