Optional / Conditional creation of a single resource in a child (called) module

Hello,

I’m using New Relic provider and am implementing a newrelic_synthetics_monitor with the below code in the child module in the folder modules/synthetic_monitor/ let’s say. When the type = "SCRIPT_BROWSER" for the newrelic_synthetics_monitor (perhaps also when type = "SCRIPT_API" but not sure), the synthetics_monitor_script resource is also used for your script to mimic logging in and doing whatever actions you want checked.

My question is, how do I conditionally/optionally create that synthetics_monitor_script when using that modules/synthetic_monitor/ child module for only the scenario when the type = "SCRIPT_BROWSER" (or perhaps even "SCRIPT_API")?

It appears I could use the conditional ternary operator expression and/or depends_on somehow, but it isn’t quite clear what the best practice is.

Based on the properties of the newrelic_synthetics_monitor_script that I see in its doc, looks like I’ll also have to have the option of including or not including the monitor_id and text values in the root module’s vars.tfvars file, as well as the in the module "synthetics_monitor" { .. } block below.

Right now for the root module / calling module code I basically have,

# main.tf
module "synthetics_monitor" {
  source = "./modules/synthetic_monitor"
  newrelic_account_id = var.newrelic_account_id
  newrelic_api_key       = var.newrelic_api_key
  newrelic_api_region  = var.newrelic_api_region

  monitor_name  = var.monitor_name
  monitor_type    = var.monitor_type
  monitor_frequency = var.monitor_frequency
  monitor_status  = var.monitor_status
  monitor_locations = var.monitor_locations
  monitor_uri  var.monitor_uri  # required for `type`s of "SIMPLE" and "BROWSER" at least
}

and so far all I have in the child module modules/synthetic_monitor/(not including the New Relic provider code, and not showing the modules/synthetic_monitor/vars.tf file):

# modules/synthetic_monitor/main.tf
resource "newrelic_synthetics_monitor" "my_synthetics_monitor" {
  name  = var.monitor_name
  type    = var.monitor_type
  frequency = var.monitor_frequency
  status  = var.monitor_status
  locations = var.monitor_locations
  # required for only "SIMPLE" and "BROWSER" `type`s
  uri  var.monitor_uri 
 # optional for only "SIMPLE" and "BROWSER" `type`s
  validation_string = try(var.monitor_validation_string, null)
  verify_ssl = try(var.verify_ssl, null)
  # optional for "SIMPLE" `type` monitor
  bypass_head_request = try(var.bypass_head_request, null)
  treat_redirect_as_failure = try(var.treat_redirect_as_failure, null)
}

the trick will be how do we create the below script resource in modules/synthetic_monitor/main.tf when the monitor_type variable value is "SIMPLE" (and also perhaps "BROWSER", but have to verify),

# modules/synthetic_monitor/main.tf
resource "newrelic_synthetics_monitor_script" "foo_script" {
  monitor_id = newrelic_synthetics_monitor.foo.id
  text = file("${path.module}/foo_script.js")
  location {
    name = "YWJjZAo="
    hmac = "ZmFrZWxvY2F0aW9uc2NyaXB0ZmFrZQ=="
  }
}