Lessons I Learned The Hard Way When Using Terraform

If you’ve spent time working with Terraform or any Infrastructure-as-Code (IaC) tool, you’ve probably learned that what looks simple on day one can quickly get complicated. Over the years, I’ve refined a few habits that make cloud infrastructure cleaner, more maintainable, and less painful to manage.

Here are some of the best lessons I’ve picked up along the way:

1. Be selective with third-party modules

Only use modules that are popular and well-maintained by big providers. If you grab random ones, you’ll likely spend more time fixing them than building.

2. Keep variables organized

Group all variables in locals and reference locals in your resources. It keeps configuration differences visible and easy to manage.

3. Version your modules

Always tag your module versions in Git. It makes rollback, tracking, and collaboration much easier.

4. Prefer for_each over count

Older examples still use count, but for_each is cleaner, more readable, and avoids index confusion.

5. Skip Terragrunt (for small teams)

Terragrunt has its place, but if your setup isn’t massive, it usually adds more complexity than it’s worth.

 

6. Use provider-level default tags

In AWS, defining default tags at the provider level saves time and ensures consistency across all resources.

7. Try OpenTofu

It’s an open-source alternative to Terraform that fixes several syntax quirks and limitations.

8. Favor HCL data structures over JSON

When defining IAM policies, use native HCL data where possible. It’s cleaner and easier to read than JSON-in-HCL.

9. Always plan before apply

Never skip terraform plan. Reviewing your changes before execution prevents surprises and accidental misconfigurations.

10. Use outputs generously

Output full resources or modules and reference them via terraform_remote_state. This keeps your setups modular, reusable, and easy to understand.

 

These small practices add up. They make it easier to collaborate, scale, and troubleshoot—especially as your infrastructure grows.

Leave a Reply

Your email address will not be published. Required fields are marked *