如何覆盖Terraform模块中的资源?

Sno*_*ash 4 terraform

我有一个像这样的Terraform模块:

module "helloworld" {
  source = ../service"
}
Run Code Online (Sandbox Code Playgroud)

../service包含:

resource "aws_cloudwatch_metric_alarm" "cpu_max" {
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  ... etc
}
Run Code Online (Sandbox Code Playgroud)

如何覆盖service变量comparison_operatorevaluation_periods模块?

例如设置cpu_max4是否像aws_cloudwatch_metric_alarm .cpu_max.evaluation_periods = 4模块一样简单?

Era*_*hel 7

您必须使用variable带有默认值的a.

variable "evaluation_periods" {
    default = 4
}

resource "aws_cloudwatch_metric_alarm" "cpu_max" {
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
}
Run Code Online (Sandbox Code Playgroud)

在你的模块中

module "helloworld" {
  source = ../service"
  evaluation_periods = 2
}
Run Code Online (Sandbox Code Playgroud)