Movatterモバイル変換


[0]ホーム

URL:


HashiConf 2025Don't miss the live stream of HashiConf Day 2 happening now View live stream

This topic provides reference information about the version constraints syntax in Terraform configuration language.

Introduction

Terraform lets you specify a range of acceptable versions forcomponents you define in the configuration. Terraform expects a specially-formatted string to constrain the versions of the component. You can specify version constraints when configuring the following components:

Version constraint syntax

A version constraint is astring literalcontaining one or more conditions separated by commas.

Each condition consists of an operator and a version number.

Version numbers are a series of numbers separated by periods, for example1.2.0. It is optional, but you can include a suffix to indicate a beta release. Refer toSpecify a pre-release version for additional information.

Use the following syntax to specify version constraints:

version= "<operator> <version>"

In the following example, Terraform installs a versions1.2.0 and newer, as well as version older than2.0.0:

version= ">= 1.2.0, < 2.0.0"

Operators

The following table describes the operators you can use to configure version constraints:

OperatorDescription
=,
no operator
Allows only one exact version number. Cannot be combined with other conditions.
!=Excludes an exact version number.
>,
>=,
<,
<=
Compares to a specified version. Terraform allows versions that resolve totrue. The> and>= operators request newer versions. The< and<= operators request older versions.
~>Allows only the right-most version component to increment. Examples:
  • ~> 1.0.4: Allows Terraform to install1.0.5 and1.0.10 but not1.1.0.
  • ~> 1.1: Allows Terraform to install1.2 and1.10 but not2.0.

Version constraint behavior

Terraform uses versions that meet all applicable constraints.

Terraform consults version constraints to determine whether it has acceptableversions of itself, any required provider plugins, and any required modules. Forplugins and modules, Terraform uses the newest installed version that meets theapplicable constraints.

When Terraform does not have an acceptable version of a required plugin or module,it attempts to download the newest version that meets the applicableconstraints.

When Terraform is unable to obtain acceptable versions of external dependenciesor if it does not have an acceptable version of itself, then it does not proceed with anyterraform plan,terraform apply, orterraform state operations.

The root module and any child modules can constrain the Terraform version and any provider versions the modules use. Terraform considers these constraintsequal, and only proceeds if all are met.

Specify a pre-release version

A pre-release version is a version number that contains a suffix introduced bya dash, for example1.2.0-beta. To configure Terraform to select a pre-release version, set the exact version number using the= operator. You can also omit the operator and specify the exact pre-release version. Terraform does not match pre-release versions on>,>=,<,<=, or~> operators.

Best practices

We recommend implementing the following best practices when configuration version constraints.

Module versions

  • Require specific versions to ensure that updates only happen when convenient to you when your infrastructure depends on third-party modules.

  • Specify version ranges when your organization consistently uses semantic versioning for modules it maintains.

  • Specify version ranges when your organization follows a well-defined release process that avoids unwanted updates.

Terraform core and provider versions

  • Reusable modules should constrain only their minimum allowed versions ofTerraform and providers, such as>= 0.12.0. This helps avoid knownincompatibilities, while allowing the user of the module flexibility toupgrade to newer versions of Terraform without altering the module.

  • Root modules should use a~> constraint to set both a lower and upper boundon versions for each provider they depend on.

Edit this page on GitHub

[8]ページ先頭

©2009-2025 Movatter.jp