Terraform Version Requirement
Mar 30, 2017
2 minute read

The new configuration block

Overview

Recently I found myself needing to upgrade terraform. I’d been on version 0.7.11 for awhile and due to how rapidly terraform releases new versions I quickly feel behind on versions. Additionally there were a few new shinies I wanted to make use of so I decided to upgrade.

The one feature in particular I’m going to cover is terraform’s new configuration block which will be used for all meta-configuration (i.e, configuration that configures Terraform itself). Initially this meta-config ships with only one config: required_version. This allows you to set some rules for which terraform version or version range is required to work with the corresponding .tf files. A real world issue I’ve run into is using terraform across a team and trying to ensure everyone is on the same version.

Configuration

After following my stow tutorial I to upgrade terraform to version 0.9.2 I then modify my globals.tf file to include the following snippet at the top which will ensure only terraform 0.9.2 is allowed this config:

terraform {
    required_version = "0.9.2"
}

Note: You can place the above config in any .tf since terraform loads all configuration files within the directory specified in alphabetical order. For more info check out: Load Order & Semantics

Errors

Running even as much as a terraform plan using a version that’s outside of the required_version range will result in an error similar to:

The currently running version of Terraform doesn't meet the
version requirements explicitly specified by the configuration.
Please use the required version or update the configuration.
Note that version requirements are usually set for a reason, so
we recommend verifying with whoever set the version requirements
prior to making any manual changes.

  Module: root
  Required version: 0.9.1
  Current version: 0.9.2

Additionally running a terraform plan prior to terraform version 0.8.0 will also error since those older versions of terraform doesn’t understand the new configuration block. This is the error I received after trying to terraform plan with 0.7.11:

module root: 1 error(s) occurred:

* Unknown root level key: terraform

I hope this sheds some insight into how to work with terraform in a sane manner!