In this lecture, we will learn how to list and manipulate the Terraform state
using the Terraform state command.
In one of the previous lectures,
we saw that the Terraform state file is stored in JSON format.
It should not be edited manually by opening it with a text editor.
Terraform provides a set of commands that allows us to list, pull and manipulate state.
These should be used instead of manually editing these files.
The Terraform state command uses a syntax like this,
the command to be used is Terraform state.
This is followed by a subcommand such as list, move, pull, remove, show, et cetera.
We will take a look at each of these next.
The first command that we are going to take a look at is the list command.
This will list all the resources recorded within the Terraform state file.
As you can see from the output, it will only print the resource address
and no other details about the resource.
We can also pass in an additional argument to this command
for a matching resource address like this.
If available, the list command will return the matching resource address from the state.
To get detailed information about a resource from the state file,
we can make use of the Terraform state show command.
This command will show the attributes of a single resource in the state file
that matches the given address.
For example, to print all the attributes for an S3 bucket
with the resource name finance-2020922,
use the Terraform state show command like this.
Next, let's take a look at the Terraform state move command.
This command is used to move items in a Terraform state file.
The syntax of this command has a source and a destination
following the move or MV sub command.
The items can be moved within the same state file,
which would mean moving a resource from its current resource address to another,
which essentially means renaming a resource,
or it can move items from one state file to another state file
maintained by a different configuration directory completely.
For example, let's consider that we have a configuration file
that we have used to create a dynamo-db table called state locking.
Sure enough, the state file also has the managed source under the same name.
If you want to rename the resource name from state-locking to, say, state-locking-db,
one way to do it without having to recreate the table
would be to use the Terraform state move command like this.
Specify the source name as state-locking and the target name as state-locking-db.
Once this is done, the name of the resource and the state file changes to the new name.
Next, we must manually rename the resource name in the configuration file.
Now we run a Terraform apply, there should be no changes to be applied for this resource.
We saw earlier that when using remote state,
the state file is no longer stored in the configuration directory
under the name terraform.tfstate.
Then how do we view the contents of this remote state?
For this, we can make use of the Terraform state pull command.
With this command, we can download and display the remote state on the screen like this.
The output of this command can then be passed to Json query tools like JQ
to filter the required data.
For example, to filter the hash key used by the dynamodb table
with the name state-locking-db, we can make use of a query like this.
This returns the value of lock ID as expected.
The next state command is the Terraform state rm or remove command.
It is used to delete items from the Terraform state file.
This command is used when you no longer wish to manage one or more resources
via the current Terraform configuration and state.
The syntax of this command is Terraform state rm
followed by the resource address.
Once it is removed from the state file,
make sure to remove the associated resource block
from the configuration file as well.
Please note that the resource removed from a state file
are not actually destroyed from the real world infrastructure.
It is only removed from the Terraform management.
That's it for this lecture.
Now, let's head over to the hands-on labs
and practice working with Terraform state commands.