Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override a resource in a Terraform module?

Tags:

terraform

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?

like image 296
Snowcrash Avatar asked Jul 14 '17 07:07

Snowcrash


People also ask

How to override the configuration of a resource in TerraForm?

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: Version 0.12 and later (as of 19/10/2020). Version 0.11 and earlier. Using this feature you could have a file named service_override.tf with the content:

What is a Terraform Module?

A Terraform module allows you to create logical abstraction on the top of some resource set. In other words, a module allows you to group resources together and reuse this group later, possibly many times. Let's assume we have a virtual server with some features hosted in the cloud.

How do I Pass providers from one Terraform Module to another?

Provider configurations can be defined only in a root Terraform module. Providers can be passed down to descendent modules in two ways: either implicitly through inheritance, or explicitly via the providers argument within a module block. These two options are discussed in more detail in the following sections.

What is resource encapsulation in TerraForm?

This is where encapsulation comes in. Encapsulation in Terraform consists of two basic concepts: module scope and explicit resource exposure. All resource instances, names, and therefore, resource visibility, are isolated in a module's scope. For example, module "A" can't see and does not know about resources in module "B" by default.


2 Answers

You have to use a variable with a default value.

variable "evaluation_periods" {
    default = 4
}

resource "aws_cloudwatch_metric_alarm" "cpu_max" {
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
}

And in your module

module "helloworld" {
  source = "../service"
  evaluation_periods = 2
}
like image 120
Erazihel Avatar answered Oct 10 '22 06:10

Erazihel


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:

  • Version 0.12 and later (as of 19/10/2020).
  • Version 0.11 and earlier.

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
}
like image 23
froblesmartin Avatar answered Oct 10 '22 07:10

froblesmartin