首页 > 解决方案 > 如何将更改反向传播到父节点?

问题描述

我有一个null_resource.gateway_setup我无法使幂等的 - 它只能运行一次。尝试多次运行它会导致错误。

因此,如果我对我的块进行更改gateway_setup,我还想terraform apply触发aws_instance.gateway块刷新。

这可能吗?如果是这样,我该怎么做?

resource "aws_instance" "gateway" {
  ...
}

resource "null_resource" "gateway_setup" {
  depends_on = [ "aws_instance.gateway" ]
  ...
  provisioner "remote-exec" {
      connection {
        type  = "ssh"
        user  = "centos"
        host  = "${aws_instance.gateway.public_ip}"
      }
      inline = [ 
         # throw an error if gateway setup had already run on this instance
         # preferrably, instead of erroring, the instance should be destroyed and recreated
         "[ -f /etc/tf__null_resource__gateway_setup_complete ] && exit 1",

         # make some non-idempotent changes
         "./configure_gateway.sh",

         # flag that this instance is dirty
         "touch /etc/tf__null_resource__gateway_setup_complete",
      ]
  }
}

如果我terraform apply在新环境中运行,“null_resource.gateway_setup”应该可以正常运行。如果我对网关设置逻辑进行更改并terraform apply再次运行,在运行“null_resource.gateway_setup”之前,我希望实例被销毁并重新创建。

标签: terraform

解决方案


推荐阅读