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?
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:
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.
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.
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.
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
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With