Trying to use modules, getting Error: Unsupported argument... An argument named "nr_account_id" is not expected here

I have searched here and I’m not sure why my code isn’t working. In the main.tf root/parent module - the top terraform/ folder, if I put all my New Relic provider resources and data entity in the main.tf and run terraform plan and terraform apply without using module { } blocks and a local path for the module source, it all works and builds resources in my New Relic account (I have an existing APM for an app). When I try to modularize and use module { } blocks with those same New Relic resources and the data entity then things don’t work and I get an error such as these, and pretty much all variables

z@Mac-Users-Apple-Computer terraform % terraform plan

Error: Unsupported argument

  on main.tf line 26, in module "nr_alerts":
  26:   nr_account_id  = var.nr_account_id

An argument named "nr_account_id" is not expected here.


Error: Unsupported argument

  on main.tf line 27, in module "nr_alerts":
  27:   nr_api_key     = var.nr_api_key

An argument named "nr_api_key" is not expected here.


Error: Unsupported argument

  on main.tf line 28, in module "nr_alerts":
  28:   nr_api_region  = var.nr_api_region

An argument named "nr_api_region" is not expected here.

My file structure is this currently:

here are main.tf and variables.tf and terraform.tfvars from the root / top-level terraform/ folder shown:

# terraform/main.tf
terraform {
  required_version = ">= 0.13"
}

# https://registry.terraform.io/providers/newrelic/newrelic/latest/docs

terraform {
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "2.21.0"
    }
  }
}

provider "newrelic" {
  # Configuration options
  account_id = var.nr_account_id
  api_key    = var.nr_api_key
  region     = var.nr_api_region
}

module "nr_alerts" {
  source = "./modules/terraform-nr-alerting"

  nr_account_id  = var.nr_account_id
  nr_api_key     = var.nr_api_key
  nr_api_region  = var.nr_api_region
  nr_entity_name = var.nr_entity_name

  nr_alert_policy_name    = var.nr_alert_policy_name
  nr_alert_condition_name = var.nr_alert_condition_name
  nr_alert_channel        = var.nr_alert_channel
  nr_alert_channel_email  = var.nr_alert_channel_email
}

module "nr_dashboard" {
  source = "./modules/terraform-nr-dashboarding"

  nr_account_id  = var.nr_account_id
  nr_api_key     = var.nr_api_key
  nr_api_region  = var.nr_api_region
  nr_entity_name = var.nr_entity_name

  nr_dashboard_title = var.nr_dashboard_title
}
### terraform/variables.tf
variable "nr_account_id" {
  type        = string
  description = "Account ID for New Relic"
}

variable "nr_api_key" {
  type        = string
  description = "API Key for the NewRelic Account"
}

variable "nr_api_region" {
  type        = string
  description = "Region for NewRelic"
  default     = "US"
}

variable "nr_entity_name" {
  type        = string
  description = "Application name in NewRelic"
}

variable "nr_dashboard_title" {
  type        = string
  description = "Name of NewRelic dashboard"
}


variable "nr_alert_policy_name" {
  type        = string
  description = "Name of alert policy"
}

variable "nr_alert_channel" {
  type        = string
  description = "Name of the alert channel" # alert channel of type "email" in our case
}

variable "nr_alert_channel_email" {
  type        = string
  description = "Email address/es for email alerts"
}

variable "nr_alert_condition_name" {
  type        = string
  description = "Name of the Alert Condition"
}

here in my .tfvars file I try to set the values also, to pass to the modules in the modules { } blocks:

# terraform/terraform.tfvars
nr_account_id  = xxx
nr_api_key     = "NRAK-xxx"
nr_api_region  = "US"
nr_entity_name = "Rails Tutorial Sample Application (Development)"

nr_dashboard_title = "Rails Sample App Dashboard"

nr_alert_policy_name    = "Rails Tutorial Sample Application (Development) - Apdex below Critical Threshold"
nr_alert_condition_name = "Rails Tutorial Sample App (Development) - Poor Performance"
nr_alert_channel_email  = "xxx@gmail.com"
nr_alert_channel        = "Rails Tutorial Sample Application (Development) - Alert Channel"

In my /modules folder I have these files below.

For /modules/terraform-nr-alerting:

# /modules/terraform-nr-alerting/main.tf
# https://registry.terraform.io/providers/newrelic/newrelic/latest/docs

terraform {
  required_providers {
    newrelic = {
      source = "newrelic/newrelic"
      version = "2.21.0"
    }
  }
}

provider "newrelic" {
  # Configuration options
  account_id = var.nr_account_id
  api_key = var.nr_api_key
  region = var.nr_api_region
}

data "newrelic_entity" "my_application" {
  name = var.nr_entity_name
  type = "APPLICATION"
  domain = "APM"
}

# Source = https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/alert_policy 
resource "newrelic_alert_policy" "alert_policy" {
  name = var.nr_alert_policy_name
  incident_preference = "PER_POLICY" # PER_POLICY is default
}

resource "newrelic_alert_condition" "alert_condition" {
  policy_id = newrelic_alert_policy.alert_policy.id

  name        = var.nr_alert_condition_name
  type        = "apm_app_metric"
  entities    = [data.newrelic_entity.my_application.application_id]
  metric      = "apdex"
  condition_scope = "application"

  term {
    duration      = 5
    operator      = "below"
    priority      = "critical"
    threshold     = "0.75"
    time_function = "all"
  }
}

# Creates an email alert channel
resource "newrelic_alert_channel" "email_channel" {
  name = var.nr_alert_channel # aka nr_alert_policy_channel
  type = "email"

  config {
    recipients              = var.nr_alert_channel_email
    include_json_attachment = "true"
  }
}

# Applies the created channels above to the alert policy
# referenced at the top of the config.
resource "newrelic_alert_policy_channel" "alert_policy_channel" {
  policy_id  = newrelic_alert_policy.alert_policy.id
  channel_ids = [
    newrelic_alert_channel.email_channel.id,
  ]
}
# /modules/terraform-nr-alerting/variables.tf

variable "nr_account_id" {
  type = string
  description = "Account ID for New Relic"
}

variable "nr_api_key" {
  type = string
  description = "API Key for the NewRelic Account"
}

variable "nr_api_region" {
  type = string
  description = "Region for NewRelic"
  default = "US"
}

variable "nr_entity_name" {
  # this is existing APM in my personal NR account for this app
  type = string
  description = "Application name in NewRelic"
}

variable "nr_alert_policy_name" {
  type = string
  description = "Name of alert policy"
}

variable "nr_alert_channel" {
  type = string
  description = "Name of the alert channel"  # alert channel of type "email" in our case
}

variable "nr_alert_channel_email" {
  type = string
  description = "Email address/es for email alerts"
}

variable "nr_alert_condition_name" {
  type = string
  description = "Name of the Alert Condition"
}

For /modules/terraform-nr-dashboarding:

# terraform/modules/terraform-nr-dashboarding/main.tf
# https://registry.terraform.io/providers/newrelic/newrelic/latest/docs

terraform {
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "2.21.0"
    }
  }
}

provider "newrelic" {
  # Configuration options
  account_id = var.nr_account_id
  api_key    = var.nr_api_key
  region     = var.nr_api_region
}

data "newrelic_entity" "my_application" {
  name   = var.nr_entity_name
  type   = "APPLICATION"
  domain = "APM"
}

# below taken from: https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/dashboard
resource "newrelic_dashboard" "nr_dashboard" {
  title = var.nr_dashboard_title

  filter {
    event_types = [
      "Transaction"
    ]
    attributes = [
      "appName",
      "name"
    ]
  }

  widget {
    title         = "Requests per minute"
    visualization = "billboard"
    entity_ids    = [data.newrelic_entity.my_application.application_id]
    nrql          = "SELECT rate(count(*), 1 minute) FROM Transaction WHERE appName = '${var.nr_entity_name}'"
    row           = 1
    column        = 1
  }

  widget {
    title         = "Error rate"
    visualization = "gauge"
    nrql          = "SELECT percentage(count(*), WHERE error IS True) FROM Transaction  WHERE appName = '${var.nr_entity_name}'"
    threshold_red = 2.5
    row           = 1
    column        = 2
  }

  widget {
    title         = "Average transaction duration, by application"
    visualization = "facet_bar_chart"
    nrql          = "SELECT average(duration) FROM Transaction FACET appName WHERE appName = '${var.nr_entity_name}'"
    row           = 1
    column        = 3
  }

  widget {
    title         = "Apdex, top 5 by host"
    duration      = 1800000
    visualization = "metric_line_chart"
    entity_ids = [
      data.newrelic_entity.my_application.application_id,
    ]
    metric {
      name   = "Apdex"
      values = ["score"]
    }
    facet    = "host"
    limit    = 5
    order_by = "score"
    row      = 2
    column   = 1
  }

  widget {
    title         = "Requests per minute, by transaction"
    visualization = "facet_table"
    nrql          = "SELECT rate(count(*), 1 minute) FROM Transaction FACET name WHERE appName = '${var.nr_entity_name}'"
    row           = 2
    column        = 2
  }

  widget {
    title         = "Dashboard Note"
    visualization = "markdown"
    source        = "### Helpful Links\n\n* [New Relic One](https://one.newrelic.com)\n* [Developer Portal](https://developer.newrelic.com)"
    row           = 2
    column        = 3
  }
}
# terraform/modules/terraform-nr-dashboarding/variables.tf
variable "nr_account_id" {
  type        = string
  description = "Account ID for New Relic"
}

variable "nr_api_key" {
  type        = string
  description = "API Key for the NewRelic Account"
}

variable "nr_api_region" {
  type        = string
  description = "Region for NewRelic"
  default     = "US"
}

variable "nr_entity_name" {
  type        = string
  description = "Application name in NewRelic"
}

variable "nr_dashboard_title" {
  type        = string
  description = "Name of NewRelic dashboard"
}

Any help appreciated, not sure what gives.

Hello,
I have the same problem any idea about it ?

Thanks

Can it be because the variables are specified in the wrong folder? You pass argumnets from the main.tf to a module, inside the module block. Then in the module folder you have to specify those variables in a variables.tf. I think if you would move your variables there it might work

1 Like

Wow I have not visited this in so long I don’t know what happened to it. It could be @ProofOfPizza.

What I like to do is go to the root module and always do a rm -rf .terraform and also clean up any state files and terraform init all over, in case there were something old still causing errors.

But yeah otherwise not sure where this went, I’ve been doing other things.