C
Coder.com13mo ago
Conor

How does the coder_script terraform resource work?

I have a script that I need to run when the workspace has stopped/deleted to delete some resources from another k8s cluster. I add it as a resource to the template and set “run_on_stop” to true and set the agent id to the one of the workspace but when I stop/delete a workspace it doesn’t run. Am I missing something in the setup?
30 Replies
Codercord
Codercord13mo ago
<#1210530942246985740>
Category
Help needed
Product
Coder OSS (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
Atif
Atif13mo ago
This only runs on stop transition. i.e. when you press the stop button and not on deleting it. Each start/stop/update/create are actually terraform apply with the difference of data.coder_workspace.me.start_count being 0 for the stop case. And the delete operation is terraform destroy So it will just delete all resources. You should be able to run a script at stop transition when you use run_at_stop = true
Conor
ConorOP13mo ago
So I have the script with run_on_stop set to true and when I press stop on the workspace it doesn’t run. Also would there be a way to run a script before the deletion of a workspace?
kyle
kyle13mo ago
For that I'd recommend using the local-exec resource instead.
Codercord
Codercord13mo ago
@Phorcys closed the thread.
mingfang
mingfang2mo ago
I have the same use case; I need to manually stop some external processes on shutdown. How can local-exec help in this case?
Phorcys
Phorcys2mo ago
hey @mingfang, local-exec runs a command on the provisioner (not within the workspace), you can use it to run a script on workspace deletion you can do something like this
resource "coder_agent" "dev" {
arch = "amd64"
os = "linux"
dir = "/workspace"

provisioner "local-exec" {
when = "destroy"
command = "<do something>"
}
}
resource "coder_agent" "dev" {
arch = "amd64"
os = "linux"
dir = "/workspace"

provisioner "local-exec" {
when = "destroy"
command = "<do something>"
}
}
see https://developer.hashicorp.com/terraform/language/resources/provisioners/local-exec#example-usage for more info let me know if you need anything else
mingfang
mingfang2mo ago
@Phorcys thanks for the explanation. for my use case I need the script to run on the workspace. i was hoping that I can use coder_script https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script#run_on_stop-1 but i have not been able to get a simple echo hello to work
Phorcys
Phorcys2mo ago
sadly you can't have the script run on deletion yoh can have it run on start or stop only if a workspace is not started up and gets deleted, it only gets deleted and not started up, so your scripts can't run inside of it may I as what the use-case is?
Atif
Atif2mo ago
This is a good feature request, we can invetsigate to add to the provider. Coul you open a feature request issue on GitHub?
Phorcys
Phorcys2mo ago
is it technically feasible though? a delete script won't be able to run inside the workspace because the workspace would have to be running for it to work
Atif
Atif2mo ago
We can run it just before deleting. Similar to how a script can run on stop. Take for example the case of docker template where the compute(container) gets deleted on stop but can still run the script before getting destroyed. Makes sense?
Phorcys
Phorcys2mo ago
i know but what if the workspace is deleted when stopped
mingfang
mingfang2mo ago
Sorry I was not being clear. I'm trying to run a script on stop and not on delete. I've tried these and they are not working.
resource "coder_agent" "pod" {
shutdown_script = <<-EOT
echo "this does not get run on stop"
EOT
}
resource "coder_agent" "pod" {
shutdown_script = <<-EOT
echo "this does not get run on stop"
EOT
}
and
resource "coder_script" "shutdown" {
agent_id = coder_agent.pod.id
run_on_stop = true
script = <<-EOF
echo "this does not get run on stop"
EOF
}
resource "coder_script" "shutdown" {
agent_id = coder_agent.pod.id
run_on_stop = true
script = <<-EOF
echo "this does not get run on stop"
EOF
}
Question is...why is this not working?
Phorcys
Phorcys2mo ago
good question, could you send your full template over please?
mingfang
mingfang2mo ago
My template is made up of several Terraform files. https://github.com/mingfang/coder-templates/tree/master/catalog The shutdow script is here https://github.com/mingfang/coder-templates/blob/master/catalog/main.tf#L108 The purpose of this template is to provide a catalog of services to try out. The service should start and shutdown along of the workspace. You can click on the link to the service(e.g. Kestra) from github to try it yourself.
GitHub
coder-templates/catalog at master · mingfang/coder-templates
Contribute to mingfang/coder-templates development by creating an account on GitHub.
GitHub
coder-templates/catalog/main.tf at master · mingfang/coder-templates
Contribute to mingfang/coder-templates development by creating an account on GitHub.
Phorcys
Phorcys2mo ago
can you check the version of the coder/coder Terraform provider? it should be in .terraform.lock.hcl or maybe you can find it via the TF CLI
mingfang
mingfang2mo ago
https://github.com/mingfang/coder-templates/blob/master/catalog/providers.tf I left it blank so it should be the "latest", correct?
GitHub
coder-templates/catalog/providers.tf at master · mingfang/coder-tem...
Contribute to mingfang/coder-templates development by creating an account on GitHub.
Phorcys
Phorcys2mo ago
I saw this, if you do not have a .terraform.lock.hcl lockfile then yes, it'll be latest
mingfang
mingfang2mo ago
I don't have a lock file. Here is the build log
Initializing provider plugins...
- Finding latest version of hashicorp/kubernetes...
- Finding coder/coder versions matching ">= 0.17.0"...
- Installing hashicorp/kubernetes v2.35.1...
- Installed hashicorp/kubernetes v2.35.1 (signed by HashiCorp)
- Installing coder/coder v2.1.2...
- Installed coder/coder v2.1.2 (signed by a HashiCorp partner, key ID 93C75807601AA0EC)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
Initializing provider plugins...
- Finding latest version of hashicorp/kubernetes...
- Finding coder/coder versions matching ">= 0.17.0"...
- Installing hashicorp/kubernetes v2.35.1...
- Installed hashicorp/kubernetes v2.35.1 (signed by HashiCorp)
- Installing coder/coder v2.1.2...
- Installed coder/coder v2.1.2 (signed by a HashiCorp partner, key ID 93C75807601AA0EC)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
Phorcys
Phorcys2mo ago
thanks this is indeed the latest i will take a deeper look in a moment, this behavior from the provider is pretty weird
mingfang
mingfang2mo ago
It turns out the shutdown script is running but it didn't get a chance to finish. I'm guessing it is because the ssh server got killed before the script gets a chance to finish. Here is the part of the agent log that shows the shutdown sequence.
2025-01-31 09:21:29.701 [info] shutting down agent
2025-01-31 09:21:29.701 [debu] set lifecycle state current={"state":"shutting_down","changed_at":"2025-01-31T09:21:29.701491Z"} last={"state":"ready","changed_at":"2025-01-31T09:18:49.701169Z"}
2025-01-31 09:21:29.701 [info] ssh-server: stopped serving listener listen_addr={} ...
2025-01-31 09:21:29.701 [info] running agent script log_source_id=ac47a7e3-34a9-44e6-8fdd-b94a9ec8cc5d log_path=/home/coder/shutdow.log script_data_dir=/tmp/coder-script-data/ac47a7e3-34a9-44e6-8fdd-b94a9ec8cc5d ...
script= #!/bin/sh
cd "/home/coder/repo/examples/postgrest"
terraform destroy -auto-approve
echo "bye bye bye"
2025-01-31 09:21:30.196 [info] connecting to coderd
2025-01-31 09:21:30.200 [warn] run exited with error ...
2025-01-31 09:21:29.701 [info] shutting down agent
2025-01-31 09:21:29.701 [debu] set lifecycle state current={"state":"shutting_down","changed_at":"2025-01-31T09:21:29.701491Z"} last={"state":"ready","changed_at":"2025-01-31T09:18:49.701169Z"}
2025-01-31 09:21:29.701 [info] ssh-server: stopped serving listener listen_addr={} ...
2025-01-31 09:21:29.701 [info] running agent script log_source_id=ac47a7e3-34a9-44e6-8fdd-b94a9ec8cc5d log_path=/home/coder/shutdow.log script_data_dir=/tmp/coder-script-data/ac47a7e3-34a9-44e6-8fdd-b94a9ec8cc5d ...
script= #!/bin/sh
cd "/home/coder/repo/examples/postgrest"
terraform destroy -auto-approve
echo "bye bye bye"
2025-01-31 09:21:30.196 [info] connecting to coderd
2025-01-31 09:21:30.200 [warn] run exited with error ...
Phorcys
Phorcys2mo ago
oh yeah that makes sense what if you add start_blocks_login = true to the coder_script?
mingfang
mingfang2mo ago
that caused a validation error
Error: "start_blocks_login" can only be set if "run_on_start" is "true"
Error: "start_blocks_login" can only be set if "run_on_start" is "true"
mingfang
mingfang2mo ago
GitHub
Blaming coder/agent/agent.go at f6e990ed875f7e7f273b8a6f5c03c3475a7...
Provision remote development environments via Terraform - Blaming coder/agent/agent.go at f6e990ed875f7e7f273b8a6f5c03c3475a7bb026 · coder/coder
Phorcys
Phorcys2mo ago
hey @mingfang, have you figured this out, or is this issue still happening?
mingfang
mingfang2mo ago
it's still a problem. I think the coder above may be the problem; i think the stop scripts should run before the ssh server is shutdown
Phorcys
Phorcys7d ago
@mingfang apologies for not getting back to you sooner, would you be able to open up an issue on our GitHub?
Phorcys
Phorcys7d ago
GitHub
Issues · coder/coder
Provision remote development environments via Terraform - Issues · coder/coder
Phorcys
Phorcys7d ago
make sure to mention the Coder version and your terraform provider version as well as the analysis you've provided

Did you find this page helpful?