4.3. Backend State

Preparation

Create a new directory for this exercise:

mkdir -p $LAB_ROOT/intermediate/backend_state
cd $LAB_ROOT/intermediate/backend_state

Optional: Create empty files:

touch main.tf 

Step 4.3.1: Define a backend

Create a new file named main.tf and add the following content:

terraform {
  backend "local" {
    path = "foobar.tfstate"
  }
}

resource "random_password" "super_secret" {
  length = 16
}

Run the commands

terraform init
terraform apply

After the apply run:

ls -al

Now you should see a local file named foobar.tfstate containing the Terraform state.

Step 4.3.2: List all managed resources

Terraform has builtin commands to interact with the state.

Run the following command to list all managed resources:

terraform state list

Run the following command to show a specific resource in the state:

terraform state show random_password.super_secret

Advanced: Run the following command to fetch the raw JSON terraform state:

terraform state pull