002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam
Task 3.4: Import existing infrastructure to an infrastructure project
Bringing existing objects under management at scale: import blocks with for_each, generated configuration and its review, and the import command for one-off work.
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
Forty existing buckets must come under a module that each team calls
through one module block:
module "team" {
for_each = local.teams
source = "./team-buckets"
# inside: resource "aws_s3_bucket" "this" { for_each = ... }
}
local.inventory is a list of objects with team, name and bucket_id. The
import must happen in one reviewed plan and apply. What should the team
add to the root module?
AA script that runs terraform import once per bucket with the module address and bucket ID
BAn import block with to = module.team.aws_s3_bucket.this and id = local.inventory[*].bucket_id
COne import block with for_each = local.inventory, id = each.value.bucket_id and to = module.team[each.value.team].aws_s3_bucket.this[each.value.name]
DImport blocks inside ./team-buckets with for_each = local.inventory, one module per team
Show the answer and why
AA script that runs terraform import once per bucket with the module address and bucket ID
Incorrect
terraform import changes state one object at a time outside the plan, so the forty imports are never reviewed together in one plan.
BAn import block with to = module.team.aws_s3_bucket.this and id = local.inventory[*].bucket_id
Incorrect
to must name one instance address and id must be a single string, so the module and resource keys cannot be left out.
COne import block with for_each = local.inventory, id = each.value.bucket_id and to = module.team[each.value.team].aws_s3_bucket.this[each.value.name]
Correct
An import block's for_each can loop over a list of objects, and the to address can use each.value for both the module key and the resource key.
DImport blocks inside ./team-buckets with for_each = local.inventory, one module per team
Incorrect
local.inventory is defined in the root module, and local values are available only in the module that defines them, so the shared module cannot iterate over it.
for_each in import blocks lets one block import many objects into module instances and resource instances alike, all visible in a single plan before anything is written to state.
A weekly job runs terraform query -generate-config-out=generated.tf to collect import candidates. The first run works; the next week's run fails before writing anything. What is the cause?
Agenerated.tf already exists, and the command will not overwrite it
BIn HCP Terraform workspaces the file is written remotely instead
CThe flag works only once per directory until terraform init runs again
DGenerated configuration needs a name ending in .tfquery.hcl
Show the answer and why
Agenerated.tf already exists, and the command will not overwrite it
Correct
Rerunning the command when the file already exists at that path results in an error, so remove the file before each run.
BIn HCP Terraform workspaces the file is written remotely instead
Incorrect
Even when connected to HCP Terraform, the command writes generated.tf on the local workstation.
CThe flag works only once per directory until terraform init runs again
Incorrect
No such limit exists; the existing file is the problem.
DGenerated configuration needs a name ending in .tfquery.hcl
Incorrect
.tfquery.hcl is for query files; the generated configuration is an ordinary .tf file.
The generated file holds resource and import blocks, including resource identities. Copy what you need into the configuration, then delete the file before the next run.