是否可以在资源本身内部引用资源名称

时间:2020-10-19 14:58:54

标签: terraform

我想在资源本身内部使用一个资源名称,以避免字符串重复和复制/粘贴错误。

resource "aws_instance" "bastion-euw3-infra-01" {
  ami           = "ami-078db6d55a16afc82"
  instance_type = "t2.micro"
  key_name      = "sylvain"

  user_data = templatefile("./scripts/cloudinit.yaml", {
    hostname          = "bastion-euw3-infra-01"
    tailscale_authkey = var.tailscale_authkey
  })

  network_interface {
    device_index         = 0
    network_interface_id = aws_network_interface.bastion-euw3-infra-01.id
  }

  tags = {
    Name        = "bastion-euw3-infra-01"
    Environment = "infra"
  }

  lifecycle {
    ignore_changes = [user_data]
  }
}

基本上,我想用某种var替换资源中的"bastion-euw3-infra-01",例如:

resource "aws_instance" "bastion-euw3-infra-01" {
  ...

  user_data = templatefile("./scripts/cloudinit.yaml", {
    hostname          = ___name___
    tailscale_authkey = var.tailscale_authkey
  })

  ...

  tags = {
    Name        = ___name___
    Environment = "infra"
  }

  ...
}

terraform提供了一种方法吗?

0 个答案:

没有答案
相关问题