4 tips to manage multiple Terraform versions
27 de maio de 2020
0 minutos de leituraEditor'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}
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.3”
4}
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.
Infraestrutura segura desde o código-fonte
A Snyk automatiza a segurança e a conformidade da IaC em fluxos de trabalho e detecta recursos descompassados e ausentes.