Skip to content

Commit a2ba69d

Browse files
authored
fix: Parse resources from Terraform Modules (#1501)
Fixes when Terraform modules are used to primariy provision infrastructure!
1 parent 9b1ef29 commit a2ba69d

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

provisioner/terraform/provision.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,17 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
290290
resources := make([]*proto.Resource, 0)
291291
agents := map[string]*proto.Agent{}
292292

293+
tfResources := plan.Config.RootModule.Resources
294+
var appendResources func(mod *tfjson.ConfigModule)
295+
appendResources = func(mod *tfjson.ConfigModule) {
296+
for _, module := range mod.ModuleCalls {
297+
appendResources(module.Module)
298+
}
299+
tfResources = append(tfResources, mod.Resources...)
300+
}
301+
293302
// Store all agents inside the maps!
294-
for _, resource := range plan.Config.RootModule.Resources {
303+
for _, resource := range tfResources {
295304
if resource.Type != "coder_agent" {
296305
continue
297306
}
@@ -340,7 +349,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
340349
agents[resource.Address] = agent
341350
}
342351

343-
for _, resource := range plan.PlannedValues.RootModule.Resources {
352+
for _, resource := range tfResources {
344353
if resource.Mode == tfjson.DataResourceMode {
345354
continue
346355
}
@@ -407,8 +416,17 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
407416
}
408417
agents := map[string]*proto.Agent{}
409418

419+
tfResources := state.Values.RootModule.Resources
420+
var appendResources func(resource *tfjson.StateModule)
421+
appendResources = func(mod *tfjson.StateModule) {
422+
for _, module := range mod.ChildModules {
423+
appendResources(module)
424+
}
425+
tfResources = append(tfResources, mod.Resources...)
426+
}
427+
410428
// Store all agents inside the maps!
411-
for _, resource := range state.Values.RootModule.Resources {
429+
for _, resource := range tfResources {
412430
if resource.Type != "coder_agent" {
413431
continue
414432
}
@@ -439,7 +457,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
439457
}
440458

441459
// Manually associate agents with instance IDs.
442-
for _, resource := range state.Values.RootModule.Resources {
460+
for _, resource := range tfResources {
443461
if resource.Type != "coder_agent_instance" {
444462
continue
445463
}
@@ -471,7 +489,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
471489
}
472490
}
473491

474-
for _, resource := range state.Values.RootModule.Resources {
492+
for _, resource := range tfResources {
475493
if resource.Mode == tfjson.DataResourceMode {
476494
continue
477495
}

0 commit comments

Comments
 (0)