WEBVTT

00:00.780 --> 00:05.860
In this lecture, we will learn how to list
and manipulate the Terraform state

00:05.860 --> 00:08.180
using the Terraform state command.

00:09.260 --> 00:11.200
In one of the previous lectures,

00:11.200 --> 00:15.440
we saw that the Terraform state file
is stored in JSON format.

00:16.440 --> 00:20.880
It should not be edited manually
by opening it with a text editor.

00:21.580 --> 00:27.300
Terraform provides a set of commands that
allows us to list, pull and manipulate state.

00:27.520 --> 00:31.540
These should be used instead of
manually editing these files.

00:32.720 --> 00:36.700
The Terraform state command
uses a syntax like this,

00:37.280 --> 00:40.100
the command to be used is Terraform state.

00:40.100 --> 00:47.320
This is followed by a subcommand such as
list, move, pull, remove, show, et cetera.

00:48.100 --> 00:51.020
We will take a look at each of these next.

00:51.820 --> 00:56.160
The first command that we are going to take
a look at is the list command.

00:56.960 --> 01:01.120
This will list all the resources recorded
within the Terraform state file.

01:02.420 --> 01:06.380
As you can see from the output,
it will only print the resource address

01:06.380 --> 01:08.580
and no other details about the resource.

01:10.140 --> 01:13.220
We can also pass in an additional argument
to this command

01:13.220 --> 01:15.740
for a matching resource address like this.

01:16.600 --> 01:21.480
If available, the list command will return
the matching resource address from the state.

01:22.600 --> 01:26.280
To get detailed information about
a resource from the state file,

01:26.280 --> 01:29.560
we can make use of the Terraform state
show command.

01:30.560 --> 01:34.580
This command will show the attributes
of a single resource in the state file

01:34.580 --> 01:36.580
that matches the given address.

01:37.020 --> 01:40.880
For example, to print
all the attributes for an S3 bucket

01:40.880 --> 01:45.060
with the resource name finance-2020922,

01:45.160 --> 01:48.160
use the Terraform state
show command like this.

01:49.380 --> 01:53.580
Next, let's take a look at
the Terraform state move command.

01:54.220 --> 01:58.040
This command is used to move items
in a Terraform state file.

01:58.920 --> 02:02.500
The syntax of this command
has a source and a destination

02:02.500 --> 02:05.600
following the move or MV sub command.

02:06.600 --> 02:09.580
The items can be moved
within the same state file,

02:09.580 --> 02:14.060
which would mean moving a resource
from its current resource address to another,

02:14.200 --> 02:16.960
which essentially means renaming a resource,

02:17.280 --> 02:21.740
or it can move items from one state file
to another state file

02:21.740 --> 02:25.020
maintained by a different configuration
directory completely.

02:26.180 --> 02:29.740
For example, let's consider that
we have a configuration file

02:29.740 --> 02:33.920
that we have used to create
a dynamo-db table called state locking.

02:34.820 --> 02:39.580
Sure enough, the state file also has
the managed source under the same name.

02:41.620 --> 02:47.260
If you want to rename the resource name
from state-locking to, say, state-locking-db,

02:47.420 --> 02:50.760
one way to do it without having
to recreate the table

02:50.760 --> 02:54.240
would be to use the Terraform state
move command like this.

02:55.200 --> 03:01.080
Specify the source name as state-locking
and the target name as state-locking-db.

03:02.040 --> 03:06.980
Once this is done, the name of the resource
and the state file changes to the new name.

03:07.720 --> 03:12.320
Next, we must manually rename the resource name
in the configuration file.

03:13.000 --> 03:18.700
Now we run a Terraform apply, there should be
no changes to be applied for this resource.

03:19.820 --> 03:22.840
We saw earlier that when using remote state,

03:22.840 --> 03:26.460
the state file is no longer stored
in the configuration directory

03:26.460 --> 03:28.800
under the name terraform.tfstate.

03:29.160 --> 03:32.880
Then how do we view the contents
of this remote state?

03:33.720 --> 03:37.600
For this, we can make use of
the Terraform state pull command.

03:38.540 --> 03:43.760
With this command, we can download and display
the remote state on the screen like this.

03:44.800 --> 03:49.420
The output of this command can then
be passed to Json query tools like JQ

03:49.420 --> 03:51.420
to filter the required data.

03:51.800 --> 03:56.000
For example, to filter the hash key
used by the dynamodb table

03:56.000 --> 04:00.420
with the name state-locking-db,
we can make use of a query like this.

04:01.160 --> 04:04.740
This returns the value of lock ID as expected.

04:05.880 --> 04:10.360
The next state command is
the Terraform state rm or remove command.

04:10.980 --> 04:14.340
It is used to delete items
from the Terraform state file.

04:15.460 --> 04:19.860
This command is used when you no longer
wish to manage one or more resources

04:19.860 --> 04:22.960
via the current Terraform
configuration and state.

04:23.540 --> 04:27.020
The syntax of this command
is Terraform state rm

04:27.020 --> 04:29.020
followed by the resource address.

04:29.700 --> 04:31.760
Once it is removed from the state file,

04:31.760 --> 04:34.200
make sure to remove
the associated resource block

04:34.200 --> 04:36.200
from the configuration file as well.

04:37.280 --> 04:40.040
Please note that the resource
removed from a state file

04:40.040 --> 04:43.300
are not actually destroyed from
the real world infrastructure.

04:43.480 --> 04:46.500
It is only removed from
the Terraform management.

04:47.360 --> 04:48.920
That's it for this lecture.

04:48.920 --> 04:51.300
Now, let's head over to the hands-on labs

04:51.300 --> 04:54.140
and practice working with
Terraform state commands.

