If you’ve just started working with Terraform you might be getting that familiar feeling in the back of your mind: “Am I doing this the right way?“.
Today we’re going to tackle your nagging feelings head on by discussing all the important best practices for Terraform, so that you have the confidence to go full steam ahead with your project.
By the end of this article you’ll understand 10 best practices to follow when implementing Terraform.
Terraform Best Practices (In Summary)
If you were looking for the quick checklist on best practices, here you go…
- Don’t commit the .tfstate file
- Configure a backend
- Back up your state files
- Keep your backends small
- Use one state per environment
- Setup backend state locking
- Execute Terraform in an automated build
- Manipulate state only through the commands
- Use variables (liberally)
- Use modules (only where necessary)
Let’s break some of these areas down…
What is Terraform?
Before we get to the best practices let’s discuss what Terraform is. Terraform is an open source, CLI based tool that helps with the creation, and management of cloud resources. In plain english Terraform is a tool that helps you create, update and delete your cloud infrastructure quickly and easily.
1. Don’t Commit The .tfState File
Let’s get straight down to the brass tacks: Don’t commit the
.tfstate is a file outputted by Terraform when you run commands that alter your state. Such as when running
terraform apply. The tfstate file stores information mappings between your created resource names and the real infrastructure (if you’re interested to know more about why the tfstate file is important, I’d suggest you check out the official Terraform state docs).
Ultimately Terraform cannot work without an up-to-date state file, so many engineers consider committing it to version control. But committing the state file comes with several risks… Firstly, you could be exposing secrets from your application configuration, such as passwords, database connection strings. And secondly, you risk executing Terraform against stale or old state that you forgot to pull down from version control.
But, not to fear, there is an alternative to committing the tfstate file, and that’s by configuring a feature called a Terraform “backend”. Backends are a fairly big topic that warrants it’s own section. Let’s discuss backends now…
2. Configure a Backend
A Terraform backend is configuration on how (and where) to store your Terraform state in a centralised, remote location. Backend setups will depend on which cloud provider / backend type you want to configure. You can configure a backend on S3, on artifactory or in a whole host of other ways.
Backends broadly speaking have two main features: state locking and remote state storage. Locking prevents two executions happening at the same time. And remote state storage allows you to put your state in a remote, yet accessible location.
By default, Terraform uses the “local” backend type which causes the output of the
.tfstate file we discussed before. But with a backend configured this state file is automatically pushed to a remote location.
Check out the backend types and follow the instructions to configure one.
3. Back Up Your State File
As your state file is the engine that drives your configuration it makes sense to ensure that the location where you store your state is backed up. Backed up state makes it easier to revert to a previous state if you make a mistake.
In AWS, for instance, versioning is very easy to configure as you just have to ensure that your bucket has versioning enabled. With versioning enabled rolling back to previous state is just a button click away.
So be sure to check out your cloud providers options for where you’re storing your backend and make sure versioning is enabled.
4. Keep Your Backends Small
When you start on a Terraform project you’ll likely have all your configuration in one place. However, over time with your infrastructure growing there will come a point where you want to break down your infrastructure configurations.
Keeping all your configurations together introduces risk, as you have the ability to introduce unwanted changes to other infrastructure when you apply your changes. To break down your infrastructure simply create new Terraform projects (and therefore new state).
To make your life easier, you can leverage the commands such as terraform import to move state between your Terraform configurations, and the remote_state block to pull in values from other Terraform remote states.
5. Use One State Per Environment
Environments are used to test changes before they are deployed to your live environment. Similar to the last idea of breaking down your state files it also makes sense to break down your terraform configurations and state files per environment. Again, breaking down by envioronment reduces risk when you apply changes.
6. Setup Backend State Locking
Terraform state comes in two parts: remote state, and state locking. State locking prevents two mutating commands, such as
terraform apply operating on the same state file at the same time.
To configure remote state, follow the instructions provided for your backend configuration option. For a backend configured with AWS, updating the backend configuration is simple as referencing a new Dynamo DB table name.
7. Execute Terraform In An Automated Build
Running code in an automated build tool has many advantages, which includes having a repeatable process, and a history of changes. The concept of builds is also very useful when applied to Terraform, ensuring that it’s more visible when and what has been executed against your infrastructure for auditing, debugging and collaborating purposes.
8. Don’t Perform State Surgery (Use the CLI)
As the state file is a representation of your infrastructure in the real world sometimes situations come up that require you to modify your state. For instance, if you want to rename a resource block, you’ll need to re-assign your terraform state to the new resource name.
Many when they need to move or rename state are tempted to dive into the state file itself and start hacking around. But beware, there’s a much safer way! The Terraform CLI gives you commands that allow you to remove, or move (
terraform state rm and
terraform state mv)
9. Use Variables, Liberally
Like in most regular programming languages, Terraform has variables. Variables allow you to store shared or repeating configuration values, which in turn keeps your code easy to manage and update. Terraform is no different.
To use variables in Terraform all you have to do is declare your variable somewhere in your configuration and later reference your variable in your resource configurations. You can then pass in values for your variables as environment variables or by passing in values from the Terraform CLI.
So be sure to go find any repeating values in your configuration, such as environment names, or prefixes and store them in variables.
10. Use Modules (Where Necessary)
Terraform modules allow you to break down infrastructure configurations into shared blothecks. You can think of modules in Terraform like regular programming functions. Modules take some inputs, do things (in our case, define infrastructure) and return outputs.
Modules help you take common infrastructure patterns and share them throughout your code, or business. But modules can sometimes be a bit of a pain to maintain, so make sure to apply them only when necessary. A good rule for when to reach for modules is when you’ve seen a pattern at least 3 times.
Go Forth, Implement Great Terraform.
And that’s a wrap on our 10 Terraform best practices for today!
Hopefully you learned some new terraform best practices and have now got some ideas for things that you can configure or change through to make your current Terraform setup faster, more secure and easier to maintain.
Speak soon Cloud Native Friend!
What best practices have you implemented to your own Terraform?
- Open Up The Cloud Newsletter #24 (May Recap 2021) - June 8, 2021
- Open Up The Cloud Newsletter #23 (April Recap 2021) - May 6, 2021
- Cloud Software Engineering Newsletter #22 (March Recap 2021) - April 7, 2021