我有这个Terraform模块:
locals {
name = "${var.counter > 0 ? lower(format("%v-%d", var.name, var.counter+1)) : lower(format("%v", var.name))}"
}
resource "null_resource" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
triggers = {
id = "${lower(join(var.delimiter, compact(concat(list(var.namespace, var.stage, local.name, var.attributes))))}"
name = "${local.name}"
namespace = "${lower(format("%v", var.namespace))}"
stage = "${lower(format("%v", var.stage))}"
attributes = "${lower(format("%v", join(var.delimiter, compact(var.attributes))))}"
}
lifecycle {
create_before_destroy = true
}
}
我正在使用这个模块:
module "label" {
source = "../modules/tf-label"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
attributes = "${var.attributes}"
delimiter = "${var.delimiter}"
tags = "${merge(map("AZ", "${local.availability_zone}"), var.tags)}"
enabled = "${local.instance_count > 0 ? "true" : "false"}"
}
我使用以下资源:
resource "aws_instance" "default" {
count = "${var.instance_count}"
name = "${module.label.id}"
tags = "${module.label.tags}"
}
由于 aws_instance 资源可以大于1,如何将 count.index 值传递给标签模块(例如 var.counter ),以便我可以处理它并形成正确的标签(例如 namespace-stage-name-attributes-counter - > example-prod-app-nginx-1 )或者正确的方法是什么这样做?