How to "override" some of the resource parameters when using terraform modules?

Viewed 2546

i am trying to develop generic terraform modules to support data-dog monitors and let the user of the modules to append resources and/or override resources in side generic modules.

terraform overrides feature works fine without modules, But not working when using modules.

how to override some of the resource parameters inside modules?

Requirements:

  1. /modules/datadog/monitors.tf contains list of resources, each resource represents a generic datadog monitor with default parameter values. Each individual application may choose to override one or more parameters inside each resource.

  2. /application-1/monitors.tf contains module with source as /modules/datadog/ , some more monitors that are not covered in generic monitors and some variables.

/application-1/monitors.tf

module "datadog" {
  source  = "/modules/datadog/"
}

/modules/datadog/monitors.tf

# Generic Datadog monitor to monitor cpu 
resource "datadog_monitor" "foo" {
  name               = "Name for monitor foo"
  type               = "metric alert"
  message            = "Monitor triggered. Notify: @hipchat-channel"
  escalation_message = "Escalation message @pagerduty"

  query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 4"

  thresholds {
    ok                = 0
    warning           = 2
    warning_recovery  = 1
    critical          = 4
    critical_recovery = 3
  }

  notify_no_data    = false
  renotify_interval = 60

  notify_audit = false
  timeout_h    = 60
  include_tags = true

  silenced {
    "*" = 0
  }

  tags = ["foo:bar", "baz"]
}

Solution 1: Add overrides.tf to /modules/datadog Directory. terraform override feature merges content in overrides.tf to configuration defined in monitors.tf.
But the problem with this solution is each application specific overrides.tf needs to be copied over to /modules/datadog Directory before running apply command.

overrides.tf

resource "datadog_monitor" "foo" {
      escalation_message = "Escalation message @pagerduty1"

      thresholds {
        ok                = 0
        warning           = 20
        warning_recovery  = 10
        critical          = 40
        critical_recovery = 35
      }

      notify_no_data    = false

    }

Solution 2: can i use overrides with modules? i tried to override resource parameters by copying overrides.tf to /application-1/ Directory, But terraform is not overriding resources, instead it is considering both as different resources.

1 Answers

You can use a conditional with count to override if a resource is to be created. The example below will only create the resource when the variable environment is not = production. If Count = 0 then the resource won't be created,

Regards,

resource "azurerm_network_security_rule" "web_server_nsg_rule_rdp" {
  name                        = "RDP Inbound"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "3389"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = "${azurerm_resource_group.web_server_rg.name}"
  network_security_group_name = "${azurerm_network_security_group.web_server_nsg.name}"
  count                       = "${var.environment == "production" ? 0 : 1}"  
}
Related