Assume the following terraform_data resource running a shell script.
The shell script “test.sh” has some time-consuming cleanup code which is expected to run when Terraform execution is interrupted with “Ctrl+C” key.
resource "terraform_data" "main" {
triggers_replace = timestamp()
provisioner "local-exec" {
command = "./test.sh"
}
}
test.sh
#!/usr/bin/env bash
set -euo pipefail
set -x
cleanup() {
set -x
echo "cleanup start"
# Some cleanup code here
sleep 3
echo "cleanup finish"
}
trap "cleanup" INT
# Some commands here
sleep 100
echo "script succeeded."
When I run “terraform apply” and press Ctrl+C once (for graceful shutdown), I see the following message.
╷
│ Error: execution halted
│
│
╵
╷
│ Error: execution halted
│
│
╵
╷
│ Error: local-exec provisioner error
│
│ with terraform_data.deploy,
│ on test.tf line 3, in resource "terraform_data" "deploy":
│ 3: provisioner "local-exec" {
│
│ Error running command './test.sh': signal: killed. Output: + trap cleanup INT
│ + sleep 100
│ ++ cleanup
│ ++ set -x
│ ++ echo 'cleanup start'
│ cleanup start
│ ++ sleep 3
│
╵
The script is killed while it is running cleanup process. It seems there is some timeout for graceful shutdown. Can we change this behavior?