在Terraform中使用target选项时,为什么有时返回其他资源和目标资源?

时间:2020-04-30 15:50:58

标签: terraform

我正在ELB堆栈中创建一个cloudwatch警报,如下所示:

module "elb_sg" {
  source                   = "git@github.com: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                      = "git@github.com: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}这样的直接模块访问来获取ELB名称(因为它存在于AWS中)(而不是在我的本地名称中,因为有时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.

当我在cloudwatch警报中使用硬编码名称或local.name作为尺寸和elb_name的Elb时,问题就消失了。

有人可以确切解释造成这种现象的原因吗?谢谢。

1 个答案:

答案 0 :(得分:0)

根据此官方doc,当我们指定模块路径时,“目标”适用于指定模块中的所有资源以及指定模块的所有后代模块。

这就是为什么当对elb使用硬编码值时,我们不会使依赖关系成为目标。

但是我们使用的module path文档也提到:“如果在没有资源说明的情况下指定了模块路径,则该地址将应用于模块中的每个资源。”

相关问题