Terraform null_resource trigger when file missing

Viewed 50

I am using null_resource provisioner to copy source files into build folder and run package installation. Then I use archive_file to create a zip that is used for a serverless function deployment. The current trigger is a output_sha of a zip file I create only for the purpose of detecting a change to the source. All works well except when the copy of source gets deleted from the build folder. At that point Terraform assumes that the source has not changed, the null_resource is not replaced and therefore no files end up in the build folder.

Is there a way to to trigger the null_resource when the file is missing as well when the source changes?

Terraform version 1.2.8

Here is a sample of what is happening:

data "archive_file" "change_check" {
  type = "zip"

  source_dir  = local.src_dir
  output_path = "${local.change_check_path}/${var.lambda_name}.zip"
}

resource "null_resource" "dependencies" {
  triggers = {
    src_hash = data.archive_file.change_check.output_sha
  }

  provisioner "local-exec" {
    command = <<EOT
      rm -rf ${local.build_dir};
      cp -R ${local.src_dir} ${local.build_dir};
    EOT
  }

  provisioner "local-exec" {
    command = "pip install -r ${local.build_dir}/requirements.txt -t ${local.build_dir}/python --upgrade"
  }
}
0 Answers
Related