Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Import, state inspection, and logging

Terraform 004 · Lesson 16 of 18 · 1:59:53

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: the import block, generating configuration for imported resources, inspecting the state with terraform state list, show, and pull, and verbose logging with TF_LOG. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Bring in, look inside

Bluefin's photo bucket and its first servers were created by hand, long before Terraform. Now the team wants Terraform to manage them without creating anything again, and to look inside the state when something goes wrong. This lesson covers importing existing infrastructure, inspecting the state, and verbose logging.

The import block

Since Terraform 1.5, an import block brings an existing object under the management of Terraform. It has a to argument, the address of the resource in your configuration, and an id argument, the identifier of the object in the cloud, like the name of a bucket. You run a plan to review the import, then apply it, so import becomes part of the normal workflow, and it works in pipelines. Since 1.7, an import block also accepts for each, to import many objects at once. Each remote object must be imported to only one address, and you can keep the import blocks in the configuration, as a history of what was imported.

Generating configuration

An import block needs a resource block at its to address. You can write it by hand, or let Terraform write it: a plan with the generate config out option and a new file name writes the configuration for every import block that has no resource block, a feature still marked experimental. Treat the generated file as a draft: review it, remove arguments that you do not need, and fix anything that the plan reports. The older terraform import command imports one object at a time, without a plan, and only when its resource block already exists.

Inspecting the state

The terraform state list command prints the address of every resource in the state, and you can filter it by address. Terraform state show prints the attributes of one resource, like the ID or the ARN of a bucket. Terraform show prints the whole state, or a saved plan, in a readable form, and its json option gives a version for tools, sensitive values included. Terraform state pull prints the raw state, and the commands that change the state, like state mv and state R M, write a backup first. Never edit the state file by hand: these commands, and the moved and removed blocks, exist for that.

Verbose logging

When Terraform behaves in an unexpected way, or a provider returns an error that you do not understand, turn on verbose logging with the T F log environment variable. Its levels, from the most detailed, are trace, debug, info, warn, and error, and the value json gives trace-level logs in JSON. Logs go to standard error, and the T F log path variable appends them to a file, as long as T F log is set too. T F log core and T F log provider split the logs of Terraform itself from those of the providers. And logs can hold secrets, so check them before you attach them to a bug report.

Recap

Let's recap. An import block, with a to address and an ID, imports existing objects through a reviewed plan and apply, with for each since 1.7. The generate config out option writes the missing resource blocks as drafts to review, while the old import command works one object at a time, without a plan. State list, state show, and terraform show let you inspect the state without editing it by hand. And T F log, from trace to error, with T F log path, gives verbose logs for a bug report, once you check them for secrets.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Bluefin adds an import block for its existing photo bucket, with the right address and ID, and writes the matching resource block. What should the engineer run next?

    • terraform import, then terraform apply
    • terraform state push with the bucket
    • terraform plan, then terraform apply
    • terraform init with an import option
    Show the answer

    terraform plan, then terraform apply

    An import block goes through the normal workflow, so the plan shows the import, and the apply writes the object to the state. “terraform import, then terraform apply” is the older way, without a plan, and the import block makes it unnecessary. “terraform state push with the bucket” is wrong, because pushing a state replaces the whole state, which is risky, and is not an import. And “terraform init with an import option” is wrong, because init has no import option.

  2. An engineer needs the ARN of the photo bucket that Terraform manages, without opening the console. Which command prints it?

    • terraform state show aws_s3_bucket.photos
    • terraform state list aws_s3_bucket
    • terraform output aws_s3_bucket.photos
    • terraform plan -refresh-only
    Show the answer

    terraform state show aws_s3_bucket.photos

    State show prints all the attributes of one resource in the state, its ARN included. “terraform state list aws_s3_bucket” only lists addresses, not attributes. “terraform output aws_s3_bucket.photos” is wrong, because terraform output prints the outputs of the root module, not the attributes of resources. And “terraform plan -refresh-only” is wrong, because a refresh-only plan shows the changes made outside Terraform, not the attributes of one resource.

  3. A provider fails with an unclear error in a pipeline. Bluefin wants the most detailed logs in a file, for a support case. Which settings work?

    • TF_LOG_PATH=terraform.log alone
    • TF_LOG=ERROR and TF_LOG_PATH=terraform.log
    • The -verbose option on terraform apply
    • TF_LOG=TRACE and TF_LOG_PATH=terraform.log
    Show the answer

    TF_LOG=TRACE and TF_LOG_PATH=terraform.log

    Trace is the most detailed level, and the log path variable writes the logs to that file. “TF_LOG_PATH=terraform.log alone” is wrong, because the log path is used only when T F log is set. “TF_LOG=ERROR and TF_LOG_PATH=terraform.log” is wrong, because error is the least detailed level. And “The -verbose option on terraform apply” is wrong, because apply has no verbose option: logging comes from environment variables.