@@ -390,7 +390,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
390390 }
391391 }
392392
393- agents [resource .Address ] = agent
393+ agents [convertAddressToLabel ( resource .Address ) ] = agent
394394 }
395395
396396 for _ , resource := range tfResources {
@@ -400,11 +400,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
400400 if resource .Type == "coder_agent" || resource .Type == "coder_agent_instance" {
401401 continue
402402 }
403- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
404403 resources = append (resources , & proto.Resource {
405404 Name : resource .Name ,
406405 Type : resource .Type ,
407- Agents : findAgents (resourceDependencies , agents , resourceKey ),
406+ Agents : findAgents (resourceDependencies , agents , convertAddressToLabel ( resource . Address ) ),
408407 })
409408 }
410409
@@ -485,8 +484,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
485484 default :
486485 agent .Auth = & proto.Agent_InstanceId {}
487486 }
488- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
489- agents [resourceKey ] = agent
487+ agents [convertAddressToLabel (resource .Address )] = agent
490488 }
491489
492490 // Manually associate agents with instance IDs.
@@ -529,8 +527,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
529527 if resource .Type == "coder_agent" || resource .Type == "coder_agent_instance" {
530528 continue
531529 }
532- resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
533- resourceAgents := findAgents (resourceDependencies , agents , resourceKey )
530+ resourceAgents := findAgents (resourceDependencies , agents , convertAddressToLabel (resource .Address ))
534531 for _ , agent := range resourceAgents {
535532 // Didn't use instance identity.
536533 if agent .GetToken () != "" {
@@ -696,8 +693,8 @@ func findDependenciesWithLabels(graph *gographviz.Graph, nodeName string) []stri
696693// findAgents recursively searches through resource dependencies
697694// to find associated agents. Nested is required for indirect
698695// dependency matching.
699- func findAgents (resourceDependencies map [string ][]string , agents map [string ]* proto.Agent , resourceKey string ) []* proto.Agent {
700- resourceNode , exists := resourceDependencies [resourceKey ]
696+ func findAgents (resourceDependencies map [string ][]string , agents map [string ]* proto.Agent , resourceLabel string ) []* proto.Agent {
697+ resourceNode , exists := resourceDependencies [resourceLabel ]
701698 if ! exists {
702699 return []* proto.Agent {}
703700 }
@@ -714,3 +711,9 @@ func findAgents(resourceDependencies map[string][]string, agents map[string]*pro
714711 }
715712 return resourceAgents
716713}
714+
715+ // convertAddressToLabel returns the Terraform address without the count
716+ // specifier. eg. "module.ec2_dev.ec2_instance.dev[0]" becomes "module.ec2_dev.ec2_instance.dev"
717+ func convertAddressToLabel (address string ) string {
718+ return strings .Split (address , "[" )[0 ]
719+ }
0 commit comments