liveBook · Manning

Semantic versioning

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A semantic version is a version identifier following major, minor, and patch precedence rules.

Semantic versioning labels releases as vMajor.Minor.Patch so users can estimate upgrade risk. Patch releases fix bugs, minor releases add backward-compatible features, and major releases indicate backward-incompatible change. section 8.1.1

Apply the version fields as follows:

  • Start a stable module at v1.0.0. section 8.1.1
  • Increment Patch for bug fixes or documentation improvements, such as v1.0.1 and v1.0.2. section 8.1.1
  • Increment Minor and reset Patch when adding features, such as v1.1.0. section 8.1.1
  • Increment Major and reset the other fields when backward compatibility breaks, such as v2.0.0. section 8.1.1

Constrain Terraform module upgrades

A module can pin one exact release with version = "1.1.1", allow minor and patch updates below 2.0.0 with version = ">= 1.1.0, < 2.0.0", or exclude a known-bad release with version = ">= 1.1.0, < 2.0.0, != 1.3.2". The pessimistic constraint "~> 1.1.0" permits bug fixes within the 1.1 line, while "~> 1.1" permits minor and patch updates below 2.0.0. section 8.1.1

Table 8.2 Example constraints with their allowed versions (view table figure)

Constraint

Allowed versions

= 1.1.1

Only v1.1.1 is allowed.

>= 1.1.0, < 2.0.0

The minimum version is v1.1.0, and any Minor or Patch versions are allowed. Upgrading the Major version is not allowed.

>= 1.1.0, < 2.0.0, != 1.3.2

The same as above, except we’re also forbidding version v1.3.2 from being used. Excluding specific versions is useful when bugs are introduced to a specific version and then fixed.

~> 1.1.0

The pessimistic constraint operator allows only the rightmost field to be upgraded. In this case, that means v1.1 and any bug fixes are allowed, but Minor and Major upgrades are excluded.

~> 1.1

This sets the floor at v1.1, but since the rightmost value is the Minor field it allows Minor and Patch upgrades. It is the exact same as >= 1.1.0, <2.0.0 but is more concise.

Compare exact, bounded, exclusion, and pessimistic constraints with their allowed versions. section 8.1.1

Table 8.1 Example changelog and semantic versions (view table figure)

Change

Field to increment

Version

First stable release

v1.0.0

Fix error in variable validation

Patch  

v1.0.1

Improve documentation

Patch  

v1.0.2

Expose additional resource parameters as variables

Minor  

v1.1.0

Fix TFLint rule violation

Patch  

v1.1.1

Refactor input variable names

Major  

v2.0.0

Map representative module changes to the version field they increment. section 8.1.1

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains version fields, change categories, constraint operators, and upgrade boundaries through configuration examples.
Examples
Uses module changelogs, Terraform version expressions, registry tags, and provider releases built by GitHub Actions.
What it assumes
Assumes familiarity with Terraform modules, releases, and Git repositories while defining the version model from its fields.
What you can do after
Apply Terraform version constraints and publish module or provider releases with semantic Git tags.
How it connects
Connects Git tags, Terraform Module Registry Protocol, Terraform modules, Terraform providers, and version constraints.

Also explained in

Compare how 8 books explain semantic versioning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.