4.1. Versions
Preparation
Finish the Data Sources exercise and copy the directory:
mkdir -p $LAB_ROOT/intermediate/
cp -r $LAB_ROOT/basics/data_sources $LAB_ROOT/intermediate/versions
cd $LAB_ROOT/intermediate/versions
Step 4.1.1: Create versions.tf
Create a new file named versions.tf and add the following content:
terraform {
required_version = "= 1.11.2"
required_providers {
random = {
source = "hashicorp/random"
version = "= 3.7.1"
}
local = {
source = "hashicorp/local"
version = "= 2.5.2"
}
}
}
Pin the required_version to the Terraform version you are using localy!
Explanation
With multiple engineers working on the same infrastructure code base, it is inevitable to have different versions of the Terraform CLI installed.
Furthermore, are Terraform providers under heavy development and have new features added daily. This rapid development can lead to incompatibilities and trigger regressions; neither are desirable in a production environment
It is best practice to lock the Terraform CLI and provider versions to a specific release. This ensures a controlled version management and planned upgrades.
Step 4.1.2: Init Terraform
Now delete the existing terraform providers and lock files (optional), init the stack and apply it by running:
rm -r .terraform/ .terraform.lock.hcl
terraform init
terraform apply
Error
If you see any error because on “Unsupported Terraform Core version”, please update the version.tf with the installed verion.
terraform version
Step 4.1.3: Terraform code formatting
Terraform offers a command to format all files according to HashiCorp guidelines by running the following command:
terraform fmt -recursive -diff
Note
Most IDEs offer HCL formatting but it differs from the HashiCorp guidelines. It is recommended to use theterraform fmt command for compliance.Note
You can use the fmt command of Terraform in CI/CD pipelines to check if the code has been formatted correctly.
Use the following command in the root folder of your Terraform code base:
terraform fmt -recursive -check