Pranit Vedpathak
Pranit Vedpathak

Reputation: 1

how to resolve Terraform error Invalid count argument?

I'm trying to add New Relic One Synthetic moniter using common module "monitor" we use in terraform, where i also want to attach new alert condition policy. which is working fine if i create resources one by one but as i want to commit all changes it showing me error as below.

Error: Invalid count argument

  on .terraform/modules/monitor/modules/synthetics/syn_alert.tf line 11, in resource "newrelic_alert_policy" "policy":
  11:   count               = var.policy_id != null ? 0 : var.create_alerts == true ? 1 : var.create_multilocation_alerts == true ? 1 : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

i expect this should work accurately as i tried stepwise, even i did tryed to look for solutions as resource dependencies so i also did added depends_on with required resources like

depends_on = [newrelic_alert_policy.harvester_ping_failure_alert_policy,newrelic_alert_channel.slack_channel]

but still not working as expected.

Upvotes: -1

Views: 2138

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74289

This error suggests that one of the input variables you included here has a value that won't be known until the apply step:

  • var.policy_id
  • var.create_alerts
  • var.create_multilocation_alerts

You didn't show how exactly how you're defining those variables in the calling module block, but I'm guessing that policy_id is probably the problematic one of these, because you've probably assigned an attribute from a managed resource instance in the parent module and the remote object corresponding to that resource instance hasn't been created yet, and so its ID isn't known yet.

If that's true, you'll need to define this differently so that the choice about whether to declare this object is made as a separate value from the ID itself, and then make sure the choice about whether to declare is not based on the outcome of any managed resource elsewhere in the configuration.

One way to do that would be like this:

variable "project" {
  type = object({
    id = string
  })
  default = null
}

This means that the decision about whether or not to set this object can be represented by the "nullness" of the entire object, even though the id attribute inside a non-null object might be unknown.

module "monitor" {
  # ...

  project = {
    id = whatever_resource_type.name.id
  }
}

If the object whose ID you're passing in here is itself a resource instance with an id attribute, as I showed above, then you can also make this more concise by assigning the whole object at once:

module "monitor" {
  # ...

  project = whatever_resource_type.name
}

Terraform will check to make sure that whatever_resource_type.name has an id attribute, and if so it will use it to populate the id attribute of the variable inside the module.

Upvotes: 2

Related Questions