2017-07-14 82 views
2

我有这样的Terraform模块:如何覆盖Terraform模块中的资源?

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

../service包含:

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

你如何重写service变量comparison_operatorevaluation_periods你的模块?

E.g.将cpu_max设置为4是否与模块中的aws_cloudwatch_metric_alarm .cpu_max.evaluation_periods = 4一样简单?

回答

3

您必须使用具有默认值的variable

variable "evaluation_periods" { 
    default = 4 
} 

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

而且你的模块

module "helloworld" { 
    source = ../service" 
    evaluation_periods = 2 
} 
1

你有你的模块定义变量英寸你的模块是:

variable "eval_period" {default = 2} # this becomes the input parameter of the module 

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

,你会使用它像:

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