Do you use HashiCorp’s Terraform as your infrastructure-as-code tool, and want to get more out of it? You’ve come to the right place. Below, I discuss tips for optimizing Terraform performance.
For the record, I won’t be limiting this article to narrowly defined performance. I want to take a broader approach by discussing some best practices for using Terraform, as well as how you can diagnose and debug issues with Terraform if something isn’t behaving as it should be.
Even minor Terraform version changes can introduce significant differences to the way in which it works and performs. With this in mind, it’s worth ensuring we’re working with the same version of Terraform to avoid any confusion. The version I’m using at the time of this article is v0.11.8. You can download Terraform here: https://www.terraform.io/downloads.html. If you’re reading this in the future (when the version may have progressed), you can choose to download the version above by using the older versions link on their page, or by clicking here: https://releases.hashicorp.com/terraform/.
When you run Terraform against your local TF configuration file(s), it builds a dependency graph from that information and “walks” it to generate a plan and refresh the state file. By default, when “walking” the graph, Terraform is limited to processing 10 nodes within the graph concurrently, and this is perfectly fine for a majority of users. However, if you are working on or deploying a large infrastructure setup using Terraform, then you may find that increasing this number will speed up processing time and deployment. This is also dependant on your system and its performance.
To increase the amount of nodes that are processed within the graph concurrently, you can utilize the -parallelism flag as described in the Terraform documentation (see the resources section in the conclusion). This flag can be used when running the ‘plan’, ‘apply’ or ‘destroy’ commands. To utilize it, simply add it after one of the commands above, but before the directory or plan you’re executing. For instance, you could use it like this:
# terraform apply -parallelism=n infra.tf
In the above example, you would replace ‘n’ with the number of nodes you want to process during the operation. I would recommend incrementing this slowly to see how it affects your performance, and to determine whether or not it is beneficial in your environment.
API Rate Limiting
One thing to bear in mind when increasing the number of nodes to run in parallel is the ability of the API you’re accessing to keep up. Some providers may rate limit you below the point at which you’re trying to access their service with Terraform. Others (like AWS) are graceful and handle rate limiting in such a way that you shouldn’t see any issues within Terraform other than a potential lack of performance gain after a certain point.
I have used Terraform extensively over the last couple of years, and have been caught out more than once with its lack of detail regarding problems it encounters. You may find that the error message is vague, doesn’t make sense, or simply doesn’t get outputted at all.
If you’re familiar with the issue I mentioned above, then don’t worry—The best way I’ve found to debug Terraform is by using the ‘TF_LOG_DEBUG’ environment variable. When set, this will output much more information about any operation that is taking place, and in my case, it helped me to discover an error being handed back by the OpenStack API that I hadn’t previously observed. To set this, simply run the following at your console:
Or if you’re running Windows, use:
As mentioned previously, this will output a lot more information than you’ve seen before, and you will have to exercise some patience in scrolling through everything!
As you can see from the above, there are ways in which you can improve the speed of Terraform when performing a plan, apply or destroy operation. Likewise, you can really expand on the information Terraform outputs by enabling the debugging environment variable shown above.
Information on the Terraform resource graph: https://www.terraform.io/docs/internals/graph.html
Debugging when using Terraform: https://www.terraform.io/docs/internals/debugging.html
Terraform state file information: https://www.terraform.io/docs/state/index.html