Posts

Showing posts from August, 2025

Terraform Best Practice | Split Terraform Code into Multiple Files | EC2 with Variables & Outputs

Image
By Raees Qazi | DevOps Engineer | Learner | Mentor | Creator In the last blog, we created an EC2 instance in AWS using Terraform. At that time, we wrote the whole code in a single file. While this works, it’s not the best practice because keeping everything in one file becomes messy and harder to manage as your infrastructure grows. In today’s blog, we’ll improve our code by splitting it into separate files. This makes the code cleaner, more reusable, and easier to handle. 👉  Before we start, I assume: You already have the provider file created. Terraform is initialized. AWS CLI is configured with an IAM user. Why Split Terraform Files? Terraform doesn’t care how many  .tf  files you have. It looks at the whole directory as a single configuration. So instead of dumping everything in  main.tf  (or  ec2.tf ), we create multiple files like: ec2.tf  → EC2-related resources variables.tf  → Input variables output.tf  → Outputs like IPs or DNS...