Stop an LLM Job After a Terraform CI Gate Fails
In this article
When a Terraform CI gate fails, any large language model (LLM) job that depends on that gate may keep running, wasting compute and potentially producing stale or invalid outputs. This guide shows you how to automatically stop an LLM job the moment a Terraform check fails, using common CI/CD tools and a few shell commands. You'll need access to your CI pipeline configuration (e.g., GitHub Actions, GitLab CI, Jenkins) and the ability to run shell commands.
What You'll Need
- A CI/CD pipeline that runs Terraform plan or apply as a gate.
- The LLM job launched either in the same pipeline or as a separate process (e.g., a script, an API call, or a job in the same CI system).
- Access to the CI system's environment variables (e.g.,
CI_JOB_ID,GITHUB_RUN_ID) and the ability to add shell commands. - If the LLM job runs on a remote server, you'll need SSH access or an API endpoint to stop it.
Step-by-Step
- Identify the gate and the LLM job. In your CI pipeline, locate the Terraform step that acts as a gate (e.g.,
terraform planwith a check for changes). Also identify how the LLM job is started—is it a separate job in the same pipeline, a background process, or an external service? - Make the Terraform gate fail explicitly. Ensure your Terraform step exits with a non-zero exit code when the gate fails. For example, in a script, run
terraform plan -detailed-exitcodeand treat exit code 2 as a failure. If you useterraform planwithout-detailed-exitcode, it exits 0 even if there are changes, so the gate won't trigger. - Capture the LLM job's process ID (PID). If the LLM job is a background process in the same shell, store its PID when you launch it:
llm_run & LLM_PID=$!. If it's a separate CI job, note its job ID or use a unique identifier from the CI environment (e.g.,CI_JOB_ID). - Add a trap to kill the LLM job on failure. In the same script that runs Terraform, add a trap that executes when the script exits with a non-zero status. For example:
Place this after launching the LLM job and before running Terraform. This ensures that if Terraform fails, the LLM process is terminated.trap 'if [ $? -ne 0 ]; then kill $LLM_PID 2>/dev/null; fi' EXIT - If the LLM job is a separate CI job, use conditional dependencies. In GitHub Actions, set
needs: terraform-gateon the LLM job and addif: success()to ensure it only runs after the gate passes. If the gate fails, the LLM job is skipped automatically—no extra work needed. - For GitLab CI, use
needswithallow_failure: false. In GitLab, the LLM job can be set toneeds: ["terraform-gate"]and it will not start until the gate completes successfully. If the gate fails, the LLM job is marked as skipped. - If the LLM job runs on a remote server, send a stop signal. From the CI script, after a failed Terraform gate, use SSH to kill the remote process:
ssh user@server "kill $(pgrep -f 'llm_job_script')". Ensure you have the correct process name or PID. - Use an API call to cancel the LLM job. If the LLM job is triggered via an API (e.g., a cloud ML service), call the cancel endpoint in the failure branch. For example, with curl:
curl -X POST https://api.llm-service.com/v1/jobs/$JOB_ID/cancel. - Test the failure path. Intentionally make the Terraform gate fail (e.g., by adding a dummy change) and confirm the LLM job stops. Check logs for the kill signal or cancellation message.
- Document the behavior. Add comments in your CI configuration explaining that the LLM job is intentionally stopped on gate failure to save resources.
Common Mistakes
- Forgetting the
-detailed-exitcodeflag. Without it, Terraform exits 0 even when there are changes, so the gate never fails. - Killing the wrong process. Using a generic pattern like
pkill pythonmight kill other jobs. Use a unique script name or PID. - Placing the trap after the Terraform command. The trap must be set before Terraform runs, otherwise it won't catch the failure.
- Assuming separate CI jobs are automatically cancelled. In many CI systems, a failed job does not cancel other running jobs unless explicitly configured.
- Not handling the case where the LLM job already finished. The kill command may fail silently—that's fine, but ensure the script doesn't error out on a missing PID.
Tips
- Use a unique environment variable for the LLM job ID, like
LLM_JOB_ID, to make it easy to reference in kill commands. - In GitHub Actions, you can use the
cancellationcontext to detect if a job is cancelled and run cleanup steps. - For long-running LLM jobs, consider adding a timeout to the job itself as a safety net.
- Log the PID and job ID at the start of the LLM job so you can manually stop it if the automatic mechanism fails.
- Use
set -ein your script to exit on any error, but be aware it may exit before you can run cleanup—combine with traps carefully.
Troubleshooting
- The LLM job still runs after the gate fails. Check that the trap is set and that the exit code from Terraform is indeed non-zero. Run the script manually with a forced failure to see if the trap fires.
- The kill command fails with "no such process". The LLM job may have already completed or the PID is incorrect. Verify the PID by echoing it to the log and use
psto check. - In GitHub Actions, the LLM job runs even though the gate failed. Ensure the LLM job has
if: success()and that the gate job is listed inneeds. Also check that the gate job's exit code is not being overridden by a later step. - The trap kills the LLM job even when Terraform succeeds. This can happen if the script exits with a non-zero code for another reason. Check the exit code of the last command before the trap fires, and ensure
set -eisn't causing an early exit. - SSH to remote server times out. The server may be unreachable or the SSH key not set up. Test the SSH connection separately and ensure the user has permission to kill the process.
Frequently Asked Questions
Can I stop an LLM job that runs in a separate pipeline?
Yes, but you need to trigger the cancellation via an API or a shared file/flag. For example, the Terraform gate can write a failure marker to a shared storage, and the LLM job checks for it periodically.
What if the LLM job is a cloud service like OpenAI's batch API?
Most cloud services have a cancel endpoint. You'll need to store the job ID from the API response and call the cancel endpoint when the gate fails.
Does this work with Terraform Cloud?
Yes, but the gate is in Terraform Cloud's run. You can use the Terraform Cloud API to check the run status and then cancel the LLM job from a CI script.
Is it better to use a separate job or a background process?
Separate jobs are cleaner because CI systems handle dependencies and cancellation natively. Background processes are harder to manage but can work in a single script.
What if I want to stop the LLM job only on certain types of failures?
You can inspect the exit code or error message from Terraform and conditionally kill the LLM job. For example, only kill if the exit code is 2 (changes present) but not for 1 (error).
Can I prevent the LLM job from starting in the first place?
Yes, by using conditional dependencies as described, the LLM job won't start until the gate passes. That's the most efficient approach.