2017-07-14 66 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 
}