Effortless Remote State Management with Terraform Cloud

Configuring Terraform Cloud for Remote State Management

In this blog post, we will explore how to configure Terraform Cloud to remotely host our Terraform state file. We will use the free subscription, and assume that you have some Terraform config already set up. We will also use the results of a previous three-part blog post series, entitled “Terraform and vSphere”.

Creating an Account on Terraform Cloud

First, we need to create an account on the Terraform Cloud portal. Fill in the details for a new account and click “Create Account”. Register your account.

Next, select the option to Create an Organization. Create a new organization and specify a name for it (it must be unique to Terraform Cloud). Specify an email address for future correspondence to go to. Click “Create Organization”.

Specifying Organisation Details

We have now successfully registered our organization on the Terraform Cloud platform.

Generating a Token

While logged in to the Terraform Cloud portal, click the user icon at the top right and select User Settings from the dropdown menu. Navigate to User Settings. Select the Tokens option from the menu on the left. Enter a description for the token (e.g., “terraform_access_token”). Click “Generate Token”. A token will be generated for you and displayed. You must take a copy of this and store it carefully. If you lose it, you will have to delete it and generate a new one.

Storing the Token

On our development system where we have Terraform located, we need to create a file called “terraform.rc” that will contain our generated token. I prefer to keep this file in a folder called “cli_config” and locate it with my other Terraform files. Create a folder called “cli_config” and a blank text file inside called “terraform.rc”.

Configuring Terraform to Read the Token

To make sure that Terraform reads our configuration file, we need to tell Terraform where to locate it. Add a new environment variable as follows:

Environment Variable: TF_CONFIG_FILE_PATH

Value: cli_config/terraform.rc

Remember, you will need to close and reopen any command prompt windows for the new variable to take effect.

Open the “terraform.rc” file in a text editor (e.g., Visual Studio Code) and enter the following into the file:

Content of terraform.rc file

—————————

[your_organization_name]

Your token will be wrapped in double quotes. Save the file and close the editor.

Creating a Backend File

Navigate into the folder where your Terraform definition is (e.g., “deploy_datacenter”). Create a text file called “backend.tf” and open it in your text editor. This file will tell Terraform where to store the state, using the credential token we supplied in the “terraform.rc” file. We will enter the following details into the file:

Content of backend.tf file

————————-

provider “your_organization_name” {

region = “your_region”

}

backend “your_organization_name” {

token = “your_token”

}

Note: Make sure that the token is wrapped in double quotes.

Saving and Closing the Editor

Once you have entered the relevant information into the “backend.tf” file, save the file and close the text editor.

Initializing Terraform

From within your folder, execute “terraform init”. This will allow Terraform to read through the “backend.tf” file and “terraform.rc” file. Terraform will connect to Terraform Cloud and under your organization create a workspace. If all goes well, you should see a message indicating that Terraform has successfully initialized.

Viewing the State Record in Terraform Cloud

After running “terraform init”, if you look in Terraform Cloud in your workspace, you will find that we now have a stored state from the execution. Future changes in state will now be recorded as a new entry here.

Conclusion

In this blog post, we have explored how to configure Terraform Cloud for remote state management. We have covered creating an account on Terraform Cloud, generating a token, storing the token, configuring Terraform to read the token, creating a backend file, and initializing Terraform. By following these steps, you can now use Terraform Cloud to store and manage your infrastructure as code.

Leave a Reply