Use hcdiag with Terraform
HashiCorp Diagnostics — hcdiag — is a troubleshooting data-gathering tool that you can use to collect and archive important data from Consul, Nomad, Vault, and TFE server environments. The information gathered by hcdiag
is well-suited for sharing with teams during incident response and troubleshooting.
For Terraform Enterprise, the hcdiag
tool executes a series of commands to collect system and production information by querying replicatedctl
and the Terraform Enterprise API. We recommend installing hcdiag
when you are experiencing an incident. By default, hcdiag
will gather up to 72 hours of server logs. This ensures that you have the latest version of hcdiag
.
In this tutorial, you will:
- Connect to your Terraform Enterprise application
- Install and configure
hcdiag
- Use
hcdiag
to gather troubleshooting information about your Terraform Enterprise application
In the process, you will learn the most important hcdiag
commands and features, and get comfortable with the contents of the data bundle created by the tool.
Prerequisites
To perform the steps in this tutorial, you need:
- A Terraform Enterprise application (Standalone or Active/Active deployment)
- sudo permissions on the host (this will make it possible for you to install
hcdiag
, and forhcdiag
to gather all relevant system data)
Create the TFE API token
The hcdiag
tool requires a user API token to authenticate and query information from the Terraform Enterprise API. Go to the "Tokens" page of your Terraform Enterprise application and create an API token named hcdiag
.
Save this token in a safe place, you will use it later in the tutorial to authenticate hcdiag
to the Terraform Enterprise API.
Connect to your TFE application
Open a shell session to the server that your Terraform Enterprise application is running on to use hcdiag
. Use SSH or your cloud provider's session manager to connect to the Terraform Enterprise application's instances.
Once you have connected to an instance of your Terraform Enterprise application, switch to the root
user. Several commands in this tutorial are only available to the root
user.
Configure the environment
Before you download and run hcdiag
, you must first configure your environment so that the tool knows how to communicate with and authenticate to Terraform Enterprise.
First, set the TFE_HTTP_ADDR
environment variable to your Terraform Enterprise URL. This tells hcdiag
where to query the API.
Then, set the TFE_TOKEN
environment variable to the API token you created earlier. This allows hcdiag
to authenticate to the API and query it for information.
Install dependencies and hcdiag
Before you can download hcdiag
, you need to install a few tools.
Note This tutorial shows package installation commands from the perspective of a TFE host running Ubuntu Linux. If you are running Red Hat Linux or another Linux distribution or operating system, replace what you see in this section with the standard package installation commands for your OS.
Update apt-get
and install the necessary dependencies.
Add the HashiCorp repository.
Update the package cache and install the latest stable version of hcdiag.
Note If you cannot use your operating system's package repositories to download hcdiag
, you can also get binaries from the HashiCorp releases repository. For instructions on this and other installation methods, see the hcdiag installation documentation
Gather troubleshooting data
Now that it's installed, use hcdiag
to gather all troubleshooting data for your Terraform Enterprise application. This may take several minutes.
If you invoke hcdiag
with no flags, it will autodetect which products are installed on the host, and gather all available environment and product information.
For a full list of options supported by hcdiag
, run hcdiag -h
.
Explore the troubleshooting data
Notice that hcdiag
generates an archive with troubleshooting data about the Terraform Enterprise application. First, list the .tar.gz
archive files to discover the file that hcdiag
created.
The filename contains an hcdiag
prefix, followed by a timestamp. Unpack the archive to further examine its contents.
The archive extracts three files into a temporary directory.
- The
Manifest.json
file contains information describing thehcdiag
run, including configuration options used, run duration, and a count of any errors encountered. - The
Results.json
file contains information about the environment and the output from thereplicatedctl support-bundle
command. - The
/var/lib/replicated/support-bundles/replicated-*.tar.gz
file contains the output from thereplicatedctl support-bundle
command
You can further explore the replicated-*.tar.gz
archive by unpacking it and examining its contents.
Warning
We encourage you to inspect the bundle to ensure it contains only information that is appropriate to share. The hcdiag
tool has default redactions which obscure common secrets or sensitive information, but we encourage you to verify the data before sending it to our support team.
When creating a support request for Terraform Enterprise, attach the hcdiag
-generated .tar.gz
bundle -- because it contains data that support engineers need, attaching it reduces the time it will take to troubleshoot your problem.
The hcdiag
tool only works locally and does not export or share the diagnostic bundle with anyone. You must use other tools to transfer it to a secure location so you can share it with specific support staff who need to view it. For information on common workflows to process and send the hcdiag
bundle, see our documentation
Configure hcdiag behavior
You can configure hcdiag's behavior with a HashiCorp Configuration Language (HCL) formatted file. Using this file, you can configure behavior by adding your own custom runners, redacting sensitive content using regular expressions, excluding commands, and more.
To run hcdiag with a custom configuration file, just create the file and point hcdiag
at it with the -config
flag:
Tip
This minimal environment doesn't ship with most common command-line text editors, so you'll want to install one with apt-get install nano
or apt-get install vim
, depending on which one you prefer.
Here is a small configuration file, which does two things:
It adds an agent-level (global) redaction which instructs hcdiag to redact all content that matches the pattern of a Vault Token.
It instructs
hcdiag
to skip thereplicatedctl support-bundle
command.
If you created this file as diag.hcl
and executed hcdiag as follows, then you could expect output like this:
Unarchiving and decompressing the resulting .tar.gz
file shows that this hcdiag
bundle no longer includes the replicated bundle:
Custom configuration is quite powerful, and can help you gather data from complex or nonstandard configurations. Please reference hcdiag's custom configuration documentation for more detailed configuration examples.
Production usage tips
By default, the hcdiag tool includes files from up to 72 hours before the current time. You can specify the desired time range using the -include-since
flag.
Deploying hcdiag in production involves a workflow similar to the following:
Place the hcdiag binary on the host targeted by hcdiag.
When running with a configuration file and the
-config
flag, ensure that the specified configuration file is readable by the user that executes hcdiag.Ensure that the current directory or that specified by the
-dest
flag is writable by the user that executes hcdiag.Ensure connectivity to the HashiCorp products that hcdiag needs to connect to during the run. Export any required environment variables for establishing connection or passing authentication tokens as necessary.
Decide on a duration for information gathering, noting that the default is to gather for up to 72 hours back in server log output. Adjust your needs as necessary with the
-include-since
flag. For example, to include only 24 hours of log output, invoke as:Use redactions to prevent sensitive information like keys or passwords from reaching hcdiag's output or the generated bundle files.
Use the
-dryrun
flag to observe what hcdiag will do without anything actually being done for testing configuration and options.
Next steps
In this tutorial, you installed, configured, and used hcdiag
to gather troubleshooting information about your Terraform Enterprise application.
- Visit the
hcdiag
repository to learn more abouthcdiag
. This repository contains information on additional configuration flags, along with detailed documentation on allhcdiag
features. - If you are using Terraform Enterprise and are experiencing an incident, you can submit a request with your
hcdiag
bundle on this page. Visit this Support Article to learn how to add bundles and files to support tickets through the support portal.