004 · Domain 6: Terraform state management · 13% of the exam
Task 6.3: Configure remote state using the backend block
The backend block: its limits (no variables), partial configuration with -backend-config, migrating state with init, the S3 backend's lock file, and the cloud block for HCP Terraform.
Study it
Remote backends: the backend block, partial configuration and migration
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 2
A team wants one configuration to use a different state bucket for each environment, and tries bucket = var.state_bucket in the backend block, which fails. Which two approaches work? (Choose TWO.)
AUse a local value for the bucket name instead of an input variable in the backend block
BSet TF_VAR_state_bucket, which backend blocks can read
CPut the bucket name in a .tfvars file, which init reads for the backend
DOmit bucket and pass it with init -backend-config="bucket=..."
EKeep settings in a file per environment and pass it with -backend-config
Show the answer and why
AUse a local value for the bucket name instead of an input variable in the backend block
Incorrect
A backend block cannot refer to any named values, including locals.
BSet TF_VAR_state_bucket, which backend blocks can read
Incorrect
Backend blocks cannot use input variables, however they are set.
CPut the bucket name in a .tfvars file, which init reads for the backend
Incorrect
Variable definition files set input variables, which backend blocks cannot use.
DOmit bucket and pass it with init -backend-config="bucket=..."
Correct
Omitting arguments gives a partial configuration; the remaining values can be passed to init as key/value pairs.
EKeep settings in a file per environment and pass it with -backend-config
Correct
A file of backend settings can be passed to init with -backend-config=PATH to complete a partial configuration.
Backend settings are read before variables exist; supply the varying parts at init time with partial configuration.
A project has used local state for months. The team adds an azurerm backend block to the configuration and wants the existing state to move into the new storage account. What should they run?
Aterraform init -migrate-state, then confirm the copy when prompted
Bterraform apply, which uploads local state the first time it runs
Cterraform state push terraform.tfstate before running init
Dterraform init -reconfigure, which copies the state while it reconfigures
Show the answer and why
Aterraform init -migrate-state, then confirm the copy when prompted
Correct
When the backend changes, init with -migrate-state copies the existing state to the new backend, with prompts to confirm.
Bterraform apply, which uploads local state the first time it runs
Incorrect
The backend must be initialized by init before any apply.
Cterraform state push terraform.tfstate before running init
Incorrect
The new backend must be initialized first, and init handles copying the state.
Dterraform init -reconfigure, which copies the state while it reconfigures
Incorrect
-reconfigure ignores the existing configuration and does not migrate state.
Change the backend block, then init -migrate-state. Back up the local state file first.
A team wants to store state in an in-house storage system and asks whether it can install a backend from the Terraform Registry as a plugin. What is the answer?
AYes; any provider can act as a backend
BNo; backends are built in and cannot be plugins
CYes; backends are downloaded and installed by init like providers are
DYes, but only in HCP Terraform
Show the answer and why
AYes; any provider can act as a backend
Incorrect
Providers manage resources; they are not backends.
BNo; backends are built in and cannot be plugins
Correct
Terraform ships with several built-in backend types, and you cannot load additional backends as plugins.
CYes; backends are downloaded and installed by init like providers are
Incorrect
init configures built-in backends; it does not download them.
DYes, but only in HCP Terraform
Incorrect
HCP Terraform manages state itself, and backends are never plugins.
Choose among built-in backends; custom storage goes behind one of them.