This article provides instructions for configuring the tfc-agent binary to run as a persistent service using systemd on a Linux system.
Prerequisites
- A Linux system with
systemd. - The
rsyslogservice enabled. - A local copy of the tfc-agent binaries.
Expected Outcome
The tfc-agent binary is managed as a service via systemd, allowing it to start on boot and restart automatically on failure.
Procedure
-
Create a directory for the agent binaries and an environment file. Copy the binaries into the new directory.
# mkdir /opt/tfc_agent # cp /path/to/binaries/tfc-agent* /opt/tfc_agent/
-
Create a file with the HCP Terraform agent environment variables. For a full list of environment variables, refer to the agent configuration documentation. The
TFC_ADDRESSvariable is only required for Terraform Enterprise.Execute the following command as the root user.
# cat > /opt/tfc_agent/tfc-agent.env << EOF TFC_ADDRESS="https://tfe.example.net" TFC_AGENT_TOKEN=$TFC_AGENT_TOKEN TFC_AGENT_NAME=$AGENT_NAME TFC_AGENT_LOG_LEVEL=INFO EOF
After completing these steps, your directory structure should appear as follows.
/opt/tfc_agent ├── tfc-agent ├── tfc-agent-core └── tfc-agent.env
-
Create a new
systemdservice file.Execute the following command as the root user.
# cat > /etc/systemd/system/tfc-agent.service << EOF [Unit] Description=Service to automatically start TFC/E Agent After=network.target [Install] WantedBy=multi-user.target [Service] EnvironmentFile=/opt/tfc_agent/tfc-agent.env Type=simple ExecStart=/opt/tfc_agent/tfc-agent KillSignal=SIGINT WorkingDirectory=/opt/tfc_agent Restart=always RestartSec=5 StandardOutput=syslog StandardError=syslog SyslogIdentifier=%n EOF
-
Reload the
systemddaemon, then enable and start thetfc-agentservice.# systemctl daemon-reload # systemctl enable tfc-agent # systemctl start tfc-agent
Verification
-
Verify that the
tfc-agentservice has started successfully. The log level in this example was set toTRACEfor verbose output.$ systemctl status tfc-agent
The output shows the service is active and running.
● tfc-agent.service - Service to automatically start TFC/E Agent Loaded: loaded (/etc/systemd/system/tfc-agent.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2023-02-27 11:33:45 CST; 1h 45min ago Main PID: 7493 (tfc-agent) Tasks: 28 (limit: 4915) CGroup: /system.slice/tfc-agent.service ├─7493 /opt/tfc_agent/tfc-agent └─7514 /opt/tfc_agent/tfc-agent-core Feb 27 13:18:49 td230 tfc-agent.service[7493]: 2023-02-27T13:18:49.781-0600 [DEBUG] core: Updating status: status.agent=idle Feb 27 13:18:49 td230 tfc-agent.service[7493]: 2023-02-27T13:18:49.781-0600 [DEBUG] core: Executing HTTP request: attempt=0 method=PUT url=https://tfe.example.net/api/agent/status Feb 27 13:18:49 td230 tfc-agent.service[7493]: 2023-02-27T13:18:49.916-0600 [DEBUG] core: HTTP request succeeded: method=PUT url=https://tfe.example.net/api/agent/status status=204 Feb 27 13:19:11 td230 tfc-agent.service[7493]: 2023-02-27T13:19:11.816-0600 [DEBUG] core: Executing HTTP request: attempt=0 method=GET url=https://tfe.example.net/api/agent/jobs Feb 27 13:19:11 td230 tfc-agent.service[7493]: 2023-02-27T13:19:11.974-0600 [DEBUG] core: HTTP request succeeded: method=GET url=https://tfe.example.net/api/agent/jobs status=204 Feb 27 13:19:11 td230 tfc-agent.service[7493]: 2023-02-27T13:19:11.974-0600 [DEBUG] core: Server specified delay for next job fetch: delay=30s Feb 27 13:19:11 td230 tfc-agent.service[7493]: 2023-02-27T13:19:11.974-0600 [DEBUG] core: No jobs ready -
Review the
tfc-agentservice logs for detailed information.$ journalctl -xu tfc-agent -f
The output displays agent activity, including registration and job handling.
Feb 27 11:19:49 td230 systemd[1]: Started Service to automatically start TFC/E Agent. Feb 27 11:19:49 td230 tfc-agent.service[28407]: 2023-02-27T11:19:49.127-0600 [INFO] agent: Starting: name=ubuntu version=1.6.0 Feb 27 11:19:49 td230 tfc-agent.service[28407]: 2023-02-27T11:19:49.144-0600 [INFO] core: Starting: version=1.6.1 Feb 27 11:19:49 td230 tfc-agent.service[28407]: 2023-02-27T11:19:49.490-0600 [INFO] core: Agent registered successfully with HCP Terraform: agent.id=agent-RSo7KWFqoxbU7HJG agent.pool.id=apool-NPva8oWcq14i1hv5 Feb 27 11:19:49 td230 tfc-agent.service[28407]: 2023-02-27T11:19:49.646-0600 [INFO] agent: Core version is up to date: version=1.6.1 Feb 27 11:19:49 td230 tfc-agent.service[28407]: 2023-02-27T11:19:49.646-0600 [INFO] core: Waiting for next job Feb 27 11:22:23 td230 tfc-agent.service[28407]: 2023-02-27T11:22:23.419-0600 [INFO] core: Job received: job.type=plan job.id=run-ufee6ynJNRQUWxYH Feb 27 11:22:23 td230 tfc-agent.service[28407]: 2023-02-27T11:22:23.419-0600 [INFO] terraform: Handling run: run.id=run-ufee6ynJNRQUWxYH run.operation=plan organization.name=havananet workspace.name=memtest Feb 27 11:22:24 td230 tfc-agent.service[28407]: 2023-02-27T11:22:24.126-0600 [INFO] terraform: Extracting Terraform from release archive Feb 27 11:22:24 td230 tfc-agent.service[28407]: 2023-02-27T11:22:24.776-0600 [INFO] terraform: Terraform CLI details: version=1.3.2 Feb 27 11:22:24 td230 tfc-agent.service[28407]: 2023-02-27T11:22:24.776-0600 [INFO] terraform: Downloading Terraform configuration Feb 27 11:22:24 td230 tfc-agent.service[28407]: 2023-02-27T11:22:24.890-0600 [INFO] terraform: Running terraform init Feb 27 11:22:26 td230 tfc-agent.service[28407]: 2023-02-27T11:22:26.745-0600 [INFO] terraform: Running terraform plan Feb 27 11:22:28 td230 tfc-agent.service[28407]: 2023-02-27T11:22:28.501-0600 [INFO] terraform: Generating and uploading plan JSON Feb 27 11:22:29 td230 tfc-agent.service[28407]: 2023-02-27T11:22:28.836-0600 [INFO] terraform: Generating and uploading provider schemas JSON Feb 27 11:22:30 td230 tfc-agent.service[28407]: 2023-02-27T11:22:30.345-0600 [INFO] terraform: Persisting filesystem to remote storage Feb 27 11:22:31 td230 tfc-agent.service[28407]: 2023-02-27T11:22:31.645-0600 [INFO] terraform: Finished handling run