Terraform Best Practices: 5 Pieces of Advice for DevOps

Raphael Socher
5 min readSep 22, 2020
Photo by Christopher Burns on Unsplash

Are you considering implementing Terraform in your DevOps environment, but hesitant to take the leap? Before diving into deployment, make sure to consider a few key points. The following Terraform best practices should help your DevOps team, whether you’re just getting started or looking to overhaul old methodologies.

In this post, we’ll cover:
-Why you should embrace DevOps before jumping into Terraform
-Vital Terraform best practices
-How to select a cloud provider
-When you should keep an eye on syntax
-The relationship between Infrastructure as Code and IT

1. Before you embrace Terraform, embrace DevOps

Terraform is a great Infrastructure as Code tool by Hashicorp — and it can completely change the way that you work. However, many teams rush to use tools before taking a step back to analyze the circumstances surrounding the change. In order to embrace Terraform best practices, it’s vital to embrace DevOps. Let’s be honest — DevOps is the new hot paradigm: it’s the most agile, leanest, most scrummy coding environment possible. In fact, DevOps’ tagline is “CI/CD” — continuous integration/continuous delivery. How much more agile could you possibly be?

Beyond vague wording surrounding DevOps and what it means to each organization, it’s important to realize that DevOps isn’t really a toolset at all. It’s about people, process, and how operational processes can be useful for developers. It’s obvious that failing to recognize an opportunity to reuse code can lead to less efficiency. Similarly, failing to use DevOps will lead to missed opportunities for operational efficiency, which will cost companies their most valuable resource: time.

To implement DevOps, the team must buy-in. This requires direct communication between team members on how to implement processes (like I said, DevOps= people + process). While it may not seem like it, Terraform best practices are reliant on buy-in first. All of the tooling in the world will do no good unless people understand the importance of dynamic operations processes.

2. Modularize your environment as a form of Terraform best practices

Terraform is, at its heart, an abstraction of the infrastructure layer of the OSI model. While there are numerous use cases, the most important thing to remember is that in abstraction, functionality is only as good as the subset of resources you are working on when clustered. If your resources are not defined in a granular enough manner, you won’t be able to provision those resources in the way you want. Failing to abstract enough and group resources appropriately will lead to an opposite burden, where the degree of customizability will be large, but your ability to work will be greatly hampered by the sprawl of resources. Therefore, modularizing your environment is a component of Terraform best practices as well.

Understanding the scale and scope of your resources and then setting up a strategy before beginning to implement Terraform is critical for best implementation. Do the legwork, prepare early on, and it will pay dividends in the long run.

3. Select the right infrastructure Cloud Provider

There is a constant discussion upon people moving to the cloud or who have already moved to the cloud around which provider to go to, and this discussion is still applicable to Terraform. While Terraform will allow you to define and better provision your cloud resources, choosing the appropriate cloud for application and hypervisor efficiencies is still important. While I won’t dive in-depth here on which cloud provider to choose, I would say the most important factor is how your roadmap and core competencies in the product align with the cloud provider of your choice. If there is a good fit, then forward-moving prospects will be positive.

4. Watch Terraform syntax rules closely

Terraform is a very syntax sensitive language compared to JSON or YAML. The next form of Terraform best practices focuses around Terraform’s sensitivity. This, coupled with the lack of easy debugging in Terraform, means getting your syntax right the first time is the best way to prevent major debugging stress further down the road.

The simplest way to get your syntax right is to obtain a strong foundational knowledge of Terraform and build from basics. HCL is not an easy language to use, and that’s why we recommend taking a look through our Git for some alternatives before immediately jumping deep into HCL.

5. Understand how Infrastructure as Code enhances your IT architecture

Infrastructure as Code as tooling is revolutionary in a modern, cloud-first, microservices oriented architecture. If you want to be on the cutting edge of technology, you’ve likely already made the leap. Infrastructure as Code can be included as an example of Terraform best practices, if you’re willing to look towards the future of infrastructure.

But, why do people choose to move towards these architectures?

First, it links back to an earlier point made on the modularization of systems. By having clearly defined dependencies, debugging becomes a lot easier in CI/CD production environments. Second, orienting around operational tasks means that repeatable parts of codebases, combined with clearly defined use cases can be reused in a very intentional manner. Automating these repeatable tasks is aspirational for developer efficiency. Third, modular architectures fit in with the modularization of all other layers of the OSI stack, including microservices in the application layer and cloud providers in the infrastructure layer. It logically follows that abstracting down into individual virtualized models and then building bottom-up is advantageous because you get customizability where necessary, but maintain larger abstract systems when that’s easier to work with.

There is no “magic” solution for successfully implementing Terraform, but there are a few ways you can make your life easier. By following the above 5 Terraform best practices, you’ll ensure that you’re on your way towards success. In reality, Terraform best practices are a combination of building a strong DevOps environment and considering ancillary decisions, such as cloud providers.

If you’re interested in learning more about Terraform and Terraform best practices, join our Slack community to share ideas, get support, and be surrounded by a community of DevOps professionals.

--

--

Raphael Socher

Founder at InfraCode — customizable, reliable Infrastructure as Code tools. Simplifying the lives of DevOps professionals. www.infrastructurecode.io