How can I override a resource in a Terraform module?

Viewed 10406

I've got a Terraform module like this:

module "helloworld" {
  source = "../service"
}

and ../service contains:

resource "aws_cloudwatch_metric_alarm" "cpu_max" {
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  ... etc
}

How do you override the service variables comparison_operator and evaluation_periods in your module?

E.g. to set cpu_max to 4 is it as simple as aws_cloudwatch_metric_alarm .cpu_max.evaluation_periods = 4 in your module?

3 Answers

In addition to the other answers using variables:

If you want to override the whole resource or just do a merge of configuration values, you can also use the overriding behaviour from Terraform:

Using this feature you could have a file named service_override.tf with the content:

resource "aws_cloudwatch_metric_alarm" "cpu_max" {
   comparison_operator = "LessThanThreshold"
   evaluation_periods  = "4"
   ... etc
}
Related