WEBVTT

00:00.580 --> 00:01.640
-In this lecture,

00:01.640 --> 00:06.020
we will see how to make use of
specific provider versions in Terraform.

00:07.300 --> 00:11.060
We saw earlier that providers
use a plugin-based architecture

00:11.060 --> 00:16.020
and that most of the popular ones are
available in the public Terraform registry.

00:17.020 --> 00:18.900
Without additional configuration,

00:18.900 --> 00:23.020
the Terraform init command downloads
the latest version of the provider plugins

00:23.020 --> 00:25.060
that are needed by the configuration files.

00:25.940 --> 00:29.660
However, this is not something
that we may desire every day.

00:30.120 --> 00:32.280
The functionality of a provider plugin

00:32.280 --> 00:34.980
may vary drastically from
one version to another.

00:35.800 --> 00:39.220
Our Terraform configuration
may not work as expected

00:39.220 --> 00:42.260
when using a version different
than the one it was written in.

00:43.480 --> 00:47.540
Fortunately, we can make sure
that a specific version of the provider

00:47.540 --> 00:50.800
is used by Terraform when we are
on the Terraform init command.

00:52.040 --> 00:55.180
The instructions to you use
a specific version of a provider

00:55.180 --> 00:58.380
is available in the provider
documentation in the registry.

00:58.800 --> 01:03.200
For example, if we look up
the local provider within the registry,

01:03.200 --> 01:05.920
the default version is 2.0.0,

01:06.360 --> 01:09.120
which is also the latest
version as of this recording.

01:10.480 --> 01:13.800
To you use a different version,
click on the version tab

01:13.800 --> 01:17.720
and it should open a drop down with
all the older versions of the provider.

01:18.340 --> 01:21.340
Let us select version 1.4.0.

01:22.240 --> 01:24.880
To use this specific version
of the local provider,

01:24.880 --> 01:27.960
click on the use provider tab on the right.

01:28.400 --> 01:33.220
This should open up the code block that we
can copy and paste within our configuration.

01:34.260 --> 01:37.680
Here, we are making use of
a new block called Terraform,

01:37.680 --> 01:41.060
which is used to configure
settings related to Terraform itself.

01:42.240 --> 01:45.180
To make use of a specific
version of the provider,

01:45.180 --> 01:48.680
we need to make use of another
block called required providers

01:48.680 --> 01:50.220
inside the Terraform block.

01:51.260 --> 01:53.140
Inside the required providers block,

01:53.140 --> 01:57.240
we can have multiple arguments
for every provider that we want to use.

01:57.720 --> 02:03.380
In this example, we have one argument with
the key called local for the local provider.

02:04.440 --> 02:09.260
The value for this argument is an object
with the source address of the provider

02:09.260 --> 02:14.300
and the exact version that we want
to install, which in this case is 1.4.0.

02:15.660 --> 02:20.860
With the Terraform block configured to
use the version 1.4.0 of the local provider,

02:21.120 --> 02:24.840
when we run a Terraform init,
we should see a message like this.

02:25.540 --> 02:27.360
Before we wrap up this lecture,

02:27.360 --> 02:31.100
let us look at the syntax used to
define version constraints in Terraform.

02:32.240 --> 02:38.520
In the configuration for the local provider,
we have specified version equal to 1.4.0.

02:39.200 --> 02:43.840
This allows Terraform to find and download
this exact version of the local provider.

02:44.120 --> 02:47.600
However, there are other ways
to use the version constraint.

02:48.240 --> 02:50.740
If we use the not equal to symbol instead,

02:50.740 --> 02:54.180
Terraform form will ensure that this
specific version is not downloaded.

02:54.640 --> 03:00.200
In this case, we have specifically asked
Terraform to not use the version 2.0.0.

03:00.640 --> 03:04.900
It downloads the previous
version available, which is 1.4.0.

03:05.960 --> 03:09.960
If we want Terraform to make use
of a version lesser than a given version,

03:09.960 --> 03:13.740
we can do that by making use
of comparison operators like this,

03:14.860 --> 03:18.300
and to make use of a version
greater than a specific version,

03:18.300 --> 03:21.500
we can make use of the greater
than operator like this.

03:23.160 --> 03:26.380
We can also combine
the comparison operators like this

03:26.380 --> 03:29.240
to make use of a specific
version within a range.

03:29.700 --> 03:30.900
In this example,

03:30.900 --> 03:37.160
we want to make use of any version
greater than 1.2.0, but lesser than 2.0.0,

03:37.580 --> 03:42.000
but also not the version 1.4.0 specifically.

03:42.580 --> 03:47.740
As a result, Terraform downloads the version
1.3.0, which is acceptable in this case.

03:49.280 --> 03:53.480
Finally, we can also make use
of pessimistic constraint operators.

03:53.820 --> 03:58.340
This is defined by making use of
the tilde greater than symbol like this.

03:59.060 --> 04:02.640
This operator allows Terraform
to download the specific version

04:02.640 --> 04:06.620
or any available incremental version
based on the value we provided.

04:07.340 --> 04:13.860
For example, here, we have given the value of
1.2, following the tilde greater than symbol.

04:14.420 --> 04:17.860
This means that Terraform can
either download the version 1.2

04:17.860 --> 04:24.160
or incremental version such as 1.3,
1.4, 1.5, all the way up until 1.9.

04:24.820 --> 04:27.820
However, if we look at
the provider documentation,

04:27.820 --> 04:30.980
we do not have a version 1.5 or anything above.

04:31.500 --> 04:36.260
The maximum version that we can
make use of in this case is 1.4.0

04:36.260 --> 04:39.760
and this is the version that is
downloaded when we run Terraform init.

04:40.500 --> 04:42.320
Let us make use of another value.

04:42.320 --> 04:46.980
This time, 1.2.0, with the same
pessimistic constraint operator.

04:47.700 --> 04:51.460
This time, Terraform can
download the version 1.2.0

04:51.460 --> 04:57.980
or the version 1.2.1 or the version
1.2.2 all the way up until 1.2.9.

04:58.480 --> 05:03.340
Again, we only have a maximum
version of 1.2.2 in the registry,

05:03.340 --> 05:06.840
and that's the version that will be
downloaded when we run Terraform init.

05:07.660 --> 05:09.200
That's it for this lecture,

05:09.560 --> 05:11.120
head over to the hands-on labs

05:11.180 --> 05:14.140
and practice working with version
constraints in Terraform.

