我正在 ELB 堆栈中创建一个 cloudwatch 警报,如下所示:
module "elb_sg" {
source = "[email protected]:terraform-aws-modules/terraform-aws-security-group.git"
name = "${local.name}-elb-sg"
description = "Allow internet inbound traffic"
vpc_id = "${data.terraform_remote_state.vpc.vpc_id}"
ingress_with_cidr_blocks = "${data.null_data_source.elb_sg_rules.*.inputs}"
tags = "${local.tags}"
# Open egress for all
egress_with_cidr_blocks = "${local.open_egress}"
}
# ELB
module "elb" {
source = "[email protected]:terraform-aws-modules/terraform-aws-elb.git"
name = "${local.name}"
subnets = ["${split(",",local.elb_subnets)}"]
internal = "${var.internal}"
security_groups = "${local.elb_security_group_ids}"
cross_zone_load_balancing = "${var.cross_zone_load_balancing}"
idle_timeout = "${var.idle_timeout}"
connection_draining = "${var.connection_draining}"
connection_draining_timeout = "${var.connection_draining_timeout}"
listener = ["${var.listener}"]
access_logs = ["${var.access_logs}"]
health_check = ["${var.health_check}"]
tags = "${local.tags}"
}
# Cloudwatch alarms
data "aws_elb" "classic_lb" {
count = "${module.elb.this_elb_name != "" ? 1 : 0}"
name = "${module.elb.this_elb_name}"
}
resource "aws_cloudwatch_metric_alarm" "low_healthy_host_count_alarm" {
count = "${var.create_alarm ? 1 : 0}"
# alarm_name = "${module.elb.this_elb_name}-HealthyHostCount"
alarm_name = "${data.aws_elb.classic_lb.name}-HealthyHostCount"
dimensions {
# LoadBalancerName = "${module.elb.this_elb_name}"
LoadBalancerName = "${data.aws_elb.classic_lb.name}"
}
...
}
但是,我尝试使用data source
直接模块访问${module.elb.this_elb_name}
来获取 AWS 上存在的 ELB 名称(而不是我本地的 ELB 名称,因为有时 AWS 会截断长名称)。但是,当两次仅针对警报时terraform plan -target=aws_cloudwatch_metric_alarm.low_healthy_host_count_alarm
,我也会将其他资源作为目标,而这并不是我想要的。
Terraform will perform the following actions:
+ aws_cloudwatch_metric_alarm.low_healthy_host_count_alarm
id: <computed>
actions_enabled: "true"
...
~ module.elb_sg.aws_security_group.this
...
~ module.elb.module.elb.aws_elb.this
access_logs.#: "0" => "1"
access_logs.0.bucket: "" => "test-logs"
access_logs.0.enabled: "" => "true"
access_logs.0.interval: "" => "60"
...
Plan: 1 to add, 2 to change, 0 to destroy.
local.name
当我在 cloudwatch 警报中的维度和警报名称中使用硬编码名称或 elb 时,问题就消失了。
有人能解释一下到底是什么原因导致了这种现象吗?谢谢。
答案1
该-target
选项是请求 Terraform 仅为选定的资源实例规划操作以及他们所依赖的一切。如果您的目标资源所依赖的某些内容有待更改,则无法跳过更新它们,因为这会导致不一致的结果。
使用常量值或不依赖于任何其他内容的本地值将进一步限制操作的范围,因为在这种情况下,资源不依赖于任何具有外部可见副作用的东西,因此由于依赖性原因而包含的更改将不会在计划中明确显示。
-target
仅应在 Terraform 错误消息建议您这样做或修复先前错误时的特定异常情况下使用。它并非旨在作为一种避免应用您已对配置所做的更改的机制。