WEBVTT

00:00.380 --> 00:04.240
In this lecture, we will learn
how to make use of an S3 bucket

00:04.240 --> 00:07.180
and a DynamoDB table
to configure a remote backend

00:07.180 --> 00:09.180
for our terraform configuration.

00:10.520 --> 00:12.380
For this, we need two things,

00:12.380 --> 00:16.120
an S3 bucket which will be used
to store the terraform state file

00:16.120 --> 00:19.840
and a DynamoDB table which will be used
to implement state locking

00:19.840 --> 00:21.840
and consistency checks.

00:22.360 --> 00:25.920
First, make sure that these two prerequisites
have been completed

00:25.920 --> 00:28.560
before beginning to configure
a remote backend.

00:29.480 --> 00:31.680
After these two services have been created,

00:31.680 --> 00:35.200
keep a note of the bucket name,
the key to be used, the region,

00:35.200 --> 00:37.300
and the name of the DynamoDB table.

00:38.340 --> 00:41.200
Now let's go over
to our configuration directory.

00:41.600 --> 00:45.340
We have a main.tf file
that creates a local file resource.

00:45.780 --> 00:48.560
When we run terraform apply,
the resource is created

00:48.560 --> 00:53.640
and as expected, terraform creates
a local state file called terraform.tfstate.

00:54.100 --> 00:56.380
To configure a remote backend in terraform,

00:56.380 --> 01:00.280
we must define additional settings
by making use of the terraform block.

01:01.000 --> 01:04.640
As a recap, we have already used
the terraform block once

01:04.640 --> 01:07.700
when we wanted a specific version
of the provider plugin

01:07.700 --> 01:09.700
to be used within our configuration.

01:10.140 --> 01:14.600
In this case, we want to configure
a remote backend for storing the state file

01:14.600 --> 01:17.980
and for that, the terraform block
should now look like this.

01:18.880 --> 01:23.140
Within the terraform block,
we specify another block called backend.

01:23.720 --> 01:28.440
Here, we provide the values we recorded
as part of the prerequisite step.

01:28.840 --> 01:32.560
The backend name specifies the type
of the backend that we want to use

01:32.560 --> 01:37.720
and for making use of an AWS S3 as the backend,
we use the name S3.

01:38.880 --> 01:42.120
The backend block with S3
expects three arguments.

01:42.200 --> 01:45.580
First is the name of the existing S3 bucket.

01:45.800 --> 01:51.440
Next is the key, which is an S3 object part
where the state file should be stored.

01:51.520 --> 01:56.360
In this case, we want to store the terraform
state file within a folder called finance,

01:56.940 --> 02:03.400
and this folder should exist within a bucket
called kodekloud-terraform-state-bucket01.

02:04.100 --> 02:07.280
The final argument that is required
is the region.

02:07.660 --> 02:10.680
This is the region
where the S3 bucket has been created

02:10.680 --> 02:14.380
and in this example, it is set to US-West-1.

02:15.180 --> 02:19.840
In order to achieve state locking,
we can optionally provide a DynamoDB table.

02:21.020 --> 02:25.480
This table should have a primary
or a hash key with the name lockID.

02:26.240 --> 02:30.980
We have already created a DynamoDB table
called state-locking for this purpose.

02:31.980 --> 02:36.600
As a standard practice, let's not store
the terraform block inside the main.tf file,

02:36.600 --> 02:40.940
instead, let's move it to a separate file
called terraform.tf.

02:42.240 --> 02:46.280
We now have the infrastructure configuration
and the backend configuration

02:46.280 --> 02:50.940
segregated into main.tf
and terraform.tf file respectively.

02:51.480 --> 02:55.740
The terraform state is now configured
to be stored in a remote S3 bucket.

02:56.440 --> 02:59.620
However, if we run the terraform
apply command now,

02:59.620 --> 03:03.640
we will see an error that says
backend reinitialization required.

03:04.540 --> 03:08.800
Running the terraform init command
will initialize the new backend to be used.

03:09.780 --> 03:13.440
Since we already have a local state file
in the configuration directory

03:13.440 --> 03:17.260
the init process gives us an option
to copy the terraform state file

03:17.260 --> 03:19.260
into the remote S3 backend.

03:19.940 --> 03:24.300
Giving a value of yes will copy
the state file to the S3 bucket.

03:25.100 --> 03:28.780
We can now delete the local state file
from the configuration directory.

03:30.120 --> 03:33.820
Running a terraform plan or apply now
will lock the state file

03:33.820 --> 03:36.820
and pull it down from the S3 bucket
into the memory.

03:37.480 --> 03:42.200
Any subsequent changes to the state will be
uploaded to the backend instantaneously

03:42.200 --> 03:45.480
and once the operation is complete,
the lock will be released.

03:46.060 --> 03:50.260
The state file will not be stored in
the local configuration directory anymore.

03:51.140 --> 03:52.620
That's it for this lecture.

03:52.620 --> 03:54.860
Let's head over to the hands-on labs

03:54.860 --> 03:57.660
and practice working with
remote backends in terraform.

