在删除local_file之前运行Destroy-Time Provisioner

时间:2019-05-29 16:16:48

标签: terraform

我有一个Terraform脚本,该脚本创建一个config.json文件,然后运行使用该config.json的命令:

resource "local_file" "config" {
  # Output vars to config
  filename = "config.json"
  content  = "..."

  # Deploy using config
  provisioner "local-exec" {
    command     = "deploy"
  }
}

这一切都很好,但是当我运行terraform destroy时,我想运行另一个命令-我尝试通过添加以下内容来在null_resource中使用销毁时间供应器来执行此操作:

resource "null_resource" "test" {
    provisioner "local-exec" {
        when        = "destroy"
        command     = "delete"
    }
}

脚本已运行,但是在删除配置文件后运行-它出错,因为它需要该配置文件存在才能知道要删除什么。

我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:0)

我将销毁时间供应商移至原始资源,并且效果很好:

resource "local_file" "config" {
  # Output vars to config
  filename = "config.json"
  content  = "..."

  # Deploy using config
  provisioner "local-exec" {
    command     = "deploy"
  }

  # Delete on_destroy
  provisioner "local-exec" {
        when        = "destroy"
        command     = "delete"
  }
}