Prerequisites
- Systemd ready Linux system
- Rsyslog Service enabled
- Local copy of the tfc-agent binaries
Expected Outcome
Manage tfc-agent
binary as a Service via Systemd
Procedure
- Create a directory for the binaries. Copy the binaries to the new folder. Create a file with the terraform cloud agent environment variables. NOTE: The
TFC_ADDRESS
variable is only required for Terraform Enterprise.sudo mkdir /opt/tfc_agent
sudo cp /path/to/binaries/tfc-agent* /opt/tfc_agent/
# https://developer.hashicorp.com/terraform/cloud-docs/agents/agents#cli-options
# Run the following command as root
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
# Directory structure after completing the steps above
/opt/tfc_agent
├── tfc-agent
├── tfc-agent-core
└── tfc-agent.env
0 directories, 3 files - Create a new Systemd Service file. Reload Systemd, enable and start the
tfc-agent
service.# Run the following command as root
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 Systemd
sudo systemctl daemon-reload
sudo systemctl enable tfc-agent
sudo systemctl start tfc-agent - Verify the
tfc-agent
service has successfully started. (Log level was set toTRACE
for this example)
$ sudo systemctl status tfc-agent
● 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-agent
service logs, example:$ sudo journalctl -xu tfc-agent -f
-- Logs begin at Mon 2018-06-25 08:42:59 CDT, end at Mon 2023-02-27 13:22:02 CST. --
Feb 27 11:19:49 td230 systemd[1]: Started Service to automatically start TFC/E Agent.
-- Subject: Unit tfc-agent.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit tfc-agent.service has finished starting up.
--
-- The start-up result is RESULT.
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 Terraform Cloud: 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