4 tips to manage multiple Terraform versions

Written by:
Stephane Jourdan
Stephane Jourdan
wordpress-sync/feature-synk-iac-terraform-green

May 27, 2020

0 mins read

Editor's note: This post originally appeared on CloudSkiff.com. CloudSkiff joined Snyk in October 2021.

There are a lot of reasons why you may need to use multiple Terraform versions locally, like having different environments (structured deployments) with separate versions, working on several projects for different customers, ensuring backward compatibility of your code by running it with several versions, etc. Here are a few very basic tips for keeping multiple Terraform versions clean and organized.

Start by using locally an environment / Terraform version manager

Using a version manager makes it way less painful to deal with multiple Terraform versions locally, and will make sure that:

  • Switching between projects is quick

  • The development environment is the closest possible to production.

tfenv is a good one, inspired by rbenv. Basically, tfenv will allow you to install a specific version of Terraform and switch to it by default. It will also enable you to list and keep track of all Terraform versions installed on your laptop. Plus it has a few convenient commands, such as tfenv install min-required that will recursively go through your terraform files to determine the minimally required version.

Explicitly set your Terraform versions

When they happen, Terraform upgrades can hurt. They hurt, even more, when you have a random exception that you don’t understand and discover that they are happening because you are running the wrong Terraform version on that specific machine.

By explicitly setting the required Terraform version, you will get a clean, explicit error if you try to run an apply with the wrong environment.

1terraform {
2required_version = "0.12.10"
3}
wordpress-sync/blog-multiple-terraform-cli-error

Set module versions as well

Moving parts are one of the programmer’s worst enemies, and that applies to Infrastructure as Code too. Terraform modules bring a lot of reusability and composability, but they can also become messy to handle.

Setting module versions makes it easier to control when what part of your infrastructure will change if there is a module upgrade. You get a guarantee that for a given commit in your codebase, you always deploy the same thing.

1module “blabla” {
2source = “module_name”
3version = “1.2.34}

It also enables to progressively roll out module upgrades: you can have the old version in production, create a dev branch where you upgrade the module, test in your dev environment, then merge on to the master if it passes testing.

The more modules you use, the more likely it is that one of them causes an issue at some point — so this becomes more and more critical as your codebase grows.

Prevent local apply / deter commits on master

Don't forget your workflow! Running apply locally can be dangerous as soon as two users collaborate on the same Terraform file: if two updates happen shortly one after the user, the first one may be overridden by the second one. Besides, you never know what can happen on your laptop, and we’ve heard stories of big state files getting messed up by a lost WiFi connection.

This is why a more production-ready workflow encourages running apply only from a central, versioned system such as through a CI/CD.

The next step, though, is to build that behavior into the process. For example, a user’s access rights will not enable them to make changes directly in production. Rather, only the CI/CD keys will have the rights to do that.

Similarly, it is very tempting to merge to master, even though we know it’s not a best practice. Locking the master will encourage users to go through the better, safer, pull-request then review process. Unless you are superhuman, and always read the Terraform plan and never make a mistake.

Some teams add additional steps there, such as linting or checking for docs updates that are a small investment in keeping the code clean in the long run.

Securing your Terraform code

Snyk IaC secures your Terraform configs (and Kubernetes, CloudFormation, and ARM templates!) as you code, with guided fixes so you can merge and move on. You can test as you write, monitor for changes in your git repositories, and automate testing in your build pipelines before deployment. Getting started with a Free plan takes minutes, while a breach from an IaC misconfiguration can cause damage to last a lifetime. Sign up and start securing your configs below.

Patch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo Segment

Snyk is a developer security platform. Integrating directly into development tools, workflows, and automation pipelines, Snyk makes it easy for teams to find, prioritize, and fix security vulnerabilities in code, dependencies, containers, and infrastructure as code. Supported by industry-leading application and security intelligence, Snyk puts security expertise in any developer’s toolkit.

Start freeBook a live demo

© 2024 Snyk Limited
Registered in England and Wales

logo-devseccon