Tips for collapsed sections
You can add text within a collapsed section.
You can add an image or a code block, too.
puts "Hello World"This repository contains a Terraform configuration designed to help debug Terraform Enterprise (TFE) agents.
Its sole purpose is to run a job on a TFE agent and then pause the agent by initiating a sleep command. This keeps the agent's container alive for a set period, allowing an administrator to connect to it (e.g., via docker exec, kubectl exec, or SSH) to inspect its state, environment variables, and filesystem.
This configuration uses a null_resource to run a local-exec provisioner.
null_resource: This is an empty resource that acts as a container for provisioners.triggers: Atimestamp()trigger is used to ensure this resource runs on everyterraform apply.local-exec: This provisioner runs a shell script directly on the TFE agent container.trapCommand: The script uses atrap '...' EXITcommand. This is a shell feature that guarantees the code inside the trap will run when the script exits, even if the main job fails.final_sleepFunction: This function, called by the trap, prints a final message and then executessleep ${var.sleep_duration_seconds}, pausing the run and keeping the agent container alive.var.sleep_duration_seconds: The sleep time is controlled by a Terraform variable, allowing you to set it from the TFE UI without changing code.
Configuration Sleep Duration You can control the sleep duration by setting a variable in your TFE workspace.
Variable Name: sleep_duration_seconds
Type: Terraform Variable
Value: The number of seconds you want the agent to sleep (e.g., 3600 for 1 hour).
Default: If not set, the default is 360 (6 minutes), as defined in variables.tf.
- Connect to TFE: Configure this GitLab repository as the VCS provider for a workspace in your TFE instance.
- Set Sleep Time (Optional): In the TFE workspace UI, go to Variables. Add a Terraform Variable with the key
sleep_duration_secondsand set the value to your desired time in seconds (e.g.,1800for 30 minutes). If you don't set this, it will use the default (360 seconds / 6 minutes). - Queue Plan: Start a new plan in the TFE workspace. Note the Run ID from the TFE UI (e.g.,
run-vCQbYPYy7K2sfR7Z). - Run Apply: Approve the plan and run the apply.
- Connect to Agent: Once the TFE UI shows the run is "Applying" and logging the "Sleeping for..." message, proceed to the "How to Connect to the Agent" section below.
When the run is paused, you can find and access the specific agent container on your host machine using the Run ID from the TFE UI.
Open a terminal on the host machine where your TFE agents are running. Use the podman ps command to filter by the run_id label.
Replace run-vCQbYPYy7K2sfR7Z with your actual Run ID.
podman ps --filter "label=run_id=run-vCQbYPYy7K2sfR7Z"This will give you an output like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4e9b1d72f1a docker.io/hashicorp/tfc-agent:latest ... 3 minutes ago Up 3 minutes inspiring_murdock
Copy the CONTAINER ID (e.g., a4e9b1d72f1a).
- Get a Shell Inside the Container Use the podman exec command with the container ID to get an interactive shell.
podman exec -it a4e9b1d72f1a /bin/sh
You will now have a shell prompt (/ # or $) inside the TFE agent, and you are free to debug for the remainder of your sleep duration.