控制 terraform 中的更新变量

Control update variables in terraform

我有以下问题,在 terraform 中如何仅在资源更改时更新变量标签?。例如,下面的代码有标签 UpdatedAt = timestamp(),每次使用 terraform apply 命令执行时间戳函数。我应该怎么做才能使标签仅在资源更改时更改?即,仅当资源 aws_instance 具有更新的

时才应执行 timestamp() 函数
resource "aws_instance" "ec2" {
  ami           = var.instance_ami
  instance_type = var.instance_size
  subnet_id = var.subnet_id
  key_name = var.ssh_key_name
  vpc_security_group_ids = var.security_group_id
  user_data                   = file(var.file_path)
  

  root_block_device {
    volume_size = var.instance_root_device_size
    volume_type = "gp3"
  }

  tags = {
    Name        = "${ec2_name}-${var.project_name}-${var.infra_env}"
    Project     = var.project_name
    Environment = var.infra_env
    ManagedBy   = "terraform"
    UpdatedBy   = var.developer_email
    UpdatedAt   = timestamp()
  }
} ```

你不能这样做。 TF 没有让您在 apply 过程中有条件地 apply/exclude 更改的功能。