我有这个 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)或者这样做的正确方法是什么?