How do I exec a script before destroy instance with terraform?
I'm need to exec a script into instance before delete it.
For example:
resource "linode_instance" "gitlab-runner" {
image = "linode/ubuntu22.04"
count = var.runners_count
label = "runner-${count.index}"
group = "Terraform"
region = "us-central"
private_ip = false
type = "g6-standard-2"
tags = ["cluster: runners", "docker", "terraform"]
provisioner "file" {
source = "script.sh"
destination = "/tmp/script.sh"
}
provisioner "remote-exec" {
when = destroy
inline = [
"chmod +x /tmp/script.sh",
"/tmp/script.sh args",
]
}
}
1 Reply
mjones
Linode Staff
Hi @vinicius_souza - I think the code you have as an example won't work for what you have in mind. There's an official guide on remote-exec provisioners in Terraform that may help:
https://developer.hashicorp.com/terraform/language/resources/provisioners/remote-exec
The code example in the guide looks identical to the one you provided, when it needs to be specific to things you've set up within your Linode in order to run as intended.