-In this lecture,
we will see how to make use of specific provider versions in Terraform.
We saw earlier that providers use a plugin-based architecture
and that most of the popular ones are available in the public Terraform registry.
Without additional configuration,
the Terraform init command downloads the latest version of the provider plugins
that are needed by the configuration files.
However, this is not something that we may desire every day.
The functionality of a provider plugin
may vary drastically from one version to another.
Our Terraform configuration may not work as expected
when using a version different than the one it was written in.
Fortunately, we can make sure that a specific version of the provider
is used by Terraform when we are on the Terraform init command.
The instructions to you use a specific version of a provider
is available in the provider documentation in the registry.
For example, if we look up the local provider within the registry,
the default version is 2.0.0,
which is also the latest version as of this recording.
To you use a different version, click on the version tab
and it should open a drop down with all the older versions of the provider.
Let us select version 1.4.0.
To use this specific version of the local provider,
click on the use provider tab on the right.
This should open up the code block that we can copy and paste within our configuration.
Here, we are making use of a new block called Terraform,
which is used to configure settings related to Terraform itself.
To make use of a specific version of the provider,
we need to make use of another block called required providers
inside the Terraform block.
Inside the required providers block,
we can have multiple arguments for every provider that we want to use.
In this example, we have one argument with the key called local for the local provider.
The value for this argument is an object with the source address of the provider
and the exact version that we want to install, which in this case is 1.4.0.
With the Terraform block configured to use the version 1.4.0 of the local provider,
when we run a Terraform init, we should see a message like this.
Before we wrap up this lecture,
let us look at the syntax used to define version constraints in Terraform.
In the configuration for the local provider, we have specified version equal to 1.4.0.
This allows Terraform to find and download this exact version of the local provider.
However, there are other ways to use the version constraint.
If we use the not equal to symbol instead,
Terraform form will ensure that this specific version is not downloaded.
In this case, we have specifically asked Terraform to not use the version 2.0.0.
It downloads the previous version available, which is 1.4.0.
If we want Terraform to make use of a version lesser than a given version,
we can do that by making use of comparison operators like this,
and to make use of a version greater than a specific version,
we can make use of the greater than operator like this.
We can also combine the comparison operators like this
to make use of a specific version within a range.
In this example,
we want to make use of any version greater than 1.2.0, but lesser than 2.0.0,
but also not the version 1.4.0 specifically.
As a result, Terraform downloads the version 1.3.0, which is acceptable in this case.
Finally, we can also make use of pessimistic constraint operators.
This is defined by making use of the tilde greater than symbol like this.
This operator allows Terraform to download the specific version
or any available incremental version based on the value we provided.
For example, here, we have given the value of 1.2, following the tilde greater than symbol.
This means that Terraform can either download the version 1.2
or incremental version such as 1.3, 1.4, 1.5, all the way up until 1.9.
However, if we look at the provider documentation,
we do not have a version 1.5 or anything above.
The maximum version that we can make use of in this case is 1.4.0
and this is the version that is downloaded when we run Terraform init.
Let us make use of another value.
This time, 1.2.0, with the same pessimistic constraint operator.
This time, Terraform can download the version 1.2.0
or the version 1.2.1 or the version 1.2.2 all the way up until 1.2.9.
Again, we only have a maximum version of 1.2.2 in the registry,
and that's the version that will be downloaded when we run Terraform init.
That's it for this lecture,
head over to the hands-on labs
and practice working with version constraints in Terraform.