smoonin
smoonin

Reputation: 141

Loop through list of resources to create new resources terraform

I have tried to get this to work, but Terraform keeps yelling at me. I am trying to use a loop create a bunch of new resources that need to reference an existing resource id. However, terraform seems to not want to create the resource.

note this uses the terraform pagerduty provider

My list of "sites":

locals {
  sites = [
    "site1",
    "site2"
  ]
}

My resource block:

resource "pagerduty_service_event_rule" "rule" {
  for_each = toset(local.sites)
  service  = "pagerduty_service.${each.key}.id"

... rest of block

When I run a apply it looks like it's going to work, but it times out (eventually giving me a 403 error).

I've determined that the issue is because the service resource attribute for the service id is invalid because it is in double quotes.

How can I properly iterate through this list to create these resources properly?

Thx

Upvotes: 0

Views: 4074

Answers (1)

smoonin
smoonin

Reputation: 141

so i managed to figure this out and it was much more simple than I expected.. I just needed to change the local sites list to a map and not use quotes on the values.

locals {
  sites = [
    site1 = pagerduty_service.site1.id
    site2 = pagerduty_service.site2.id
  ]
}

resource "pagerduty_service_event_rule" "rule" {
  for_each = local.sites
  service  = each.value

... rest of block

Upvotes: 0

Related Questions