1. Overview
Terraform is an Infrastructure-as-Code (IaC) automation tool for provisioning and managing resources in the cloud. It uses providers to interact with cloud and Software-as-a-Service (SaaS) platforms. Providers in Terraform offer various resource types or data sources.
In this tutorial, we’ll discuss how to find the versions of all Terraform providers in a configuration. The version of Terraform we use is 1.9.3.
2. Example Configuration
We’ll use the following Terraform configuration, example.tf, which contains three providers:
$ cat example.tf
provider "kubernetes" {
}
resource "local_file" "hello" {
file_name = "hello.txt"
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
}
The first provider block explicitly declares the provider. The name kubernetes in the block header specifies the local name of the provider. The Kubernetes provider interacts with the resources supported by Kubernetes.
The other two resource blocks implicitly declare the providers. For example, the term local before the underscore character in the local_file resource defines the provider, and file after the underscore character defines the resource type. The local_file resource is used for generating a file whose contents can be specified.
Similarly, aws in the aws_instance resource indicates the provider. The aws_instance resource is for providing Amazon EC2 instances and resources.
3. Using terraform version
We can use the terraform version command to list the versions of providers within a configuration. Let’s run terraform version before initializing the workspace:
$ terraform version
Terraform v1.9.3
on linux_amd64
There aren’t any providers yet, so terraform version displays only the version of Terraform.
Let’s now initialize the configuration using terraform init:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Finding latest version of hashicorp/aws...
- Finding latest version of hashicorp/kubernetes...
- Installing hashicorp/local v2.5.1...
- Installed hashicorp/local v2.5.1 (signed by HashiCorp)
- Installing hashicorp/aws v5.61.0...
- Installed hashicorp/aws v5.61.0 (signed by HashiCorp)
- Installing hashicorp/kubernetes v2.31.0...
- Installed hashicorp/kubernetes v2.31.0 (signed by HashiCorp)
...
Terraform downloads and installs the necessary provider plugins used in the configuration files using the terraform init command. As we see in the output, it has installed the local, aws, and kubernetes providers listed in the example.tf configuration file.
Let’s run terraform version once more after initialization:
$ terraform version
Terraform v1.9.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.61.0
+ provider registry.terraform.io/hashicorp/kubernetes v2.31.0
+ provider registry.terraform.io/hashicorp/local v2.5.1
The output contains the list of providers together with their versions, as expected.
It’s also possible to display the output as a JSON object:
$ terraform version -json
{
"terraform_version": "1.9.3",
"platform": "linux_amd64",
"provider_selections": {
"registry.terraform.io/hashicorp/aws": "5.61.0",
"registry.terraform.io/hashicorp/kubernetes": "2.31.0",
"registry.terraform.io/hashicorp/local": "2.5.1"
},
"terraform_outdated": false
}
We need to pass the -json option to terraform version for displaying the output as a JSON object.
4. Using the .terraform Directory
There’s a hidden directory, .terraform, in a Terraform working directory. Terraform creates this directory after we run terraform init. Besides keeping the record of which workspace is active and the last known configuration, it contains the provider plugins. Therefore, we can use it to check the providers:
$ ls -l .terraform/providers/registry.terraform.io/hashicorp/
total 12
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:57 aws
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:57 kubernetes
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:56 local
As is apparent from the output, each provider has a corresponding subdirectory in .terraform/providers/r**egistry.terraform.io/hashicorp. Each subdirectory further contains a subdirectory having the version number as its name:
$ ls -l .terraform/providers/registry.terraform.io/hashicorp/aws
total 4
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:57 5.61.0
$ ls -l .terraform/providers/registry.terraform.io/hashicorp/kubernetes
total 4
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:57 2.31.0
$ ls -l .terraform/providers/registry.terraform.io/hashicorp/local
total 4
drwxr-xr-x 3 ubuntu ubuntu 4096 Aug 4 14:56 2.5.1
For example, the version of the aws provider is 5.61.0, and the corresponding subdirectory’s name is 5.61.0.
5. Conclusion
In this article, we discussed how to find the versions of all Terraform providers in a configuration.
First, we saw how to use the terraform version command. It can print the providers and their versions in a standard format, and it can also output this information as a JSON object.
Then, we learned that the .terraform hidden directory in a Terraform workspace contains the provider plugins together with subdirectories having the version numbers as their names.