DEV Community

Palak Bhawsar
Palak Bhawsar

Posted on • Originally published at palak-bhawsar.hashnode.dev on

Easily manage your Terraform Remote State file with Terraform Cloud

In this article, we will see how to store terraform remote state file in the terraform cloud. Also how to create Terraform cloud account and workspaces to manage remote state file. We are storing terraform state file in Terraform cloud to keep it safe as it contains sensitive information about the infrastructure.

What is Terraform?

  • Terraform is an open-source infrastructure as a code tool created by HashiCorp, that lets you provision, build, change, and version cloud and on-prem resources. It lets you define both Cloud and on-prem resources in human-readable configuration files that you can version reuse, and share.

What is Infrastructure as Code?

  • Infrastructure as code (IaC) is the process that allows you to manage infrastructure with configuration files rather than through a graphical user interface. IAC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share.

What is Terraform state file?

When we create infrastructure after executing terraform apply command. Terraform creates a state file called terraform.tfstate. This state file contains all the information about the resources created using Terraform and keeps track of resources created by your configuration and maps them to real-world resources. Terraform state file is a sensitive file as it contains information about the infrastructure that we have created.

You should never push terraform state file to any version control system like GitHub. Store terraform.tfstate file in the backend to keep it safe.

What is Terraform cloud?

Terraform Cloud is HashiCorps managed service offering. It eliminates the need for unnecessary tooling and documentation for practitioners, teams, and organizations to use Terraform in production. Terraform Cloud enables infrastructure automation for provisioning, compliance, and management of any cloud, data center, and service.

The backend supported by Terraform:

  • Amazon S3

  • Azure Storage

  • Google Cloud Storage

  • HashiCorps Terraform Cloud and Terraform Enterprise.

In this article, we are using Terraform Cloud as backend to store Terraform state file.

Prerequisite

  • AWS account and AWS Access key and Secret key created

  • Terraform installed on your IDE

  • AWS CLI installed and configured on your IDE

  • Basic understanding of AWS services and Terraform

Step-1: Create Terraform cloud account

Go to https://app.terraform.io/ and sign-up for a free account. After login creates an organization and a workspace. Choose Start from the scratch workflow.

Step-2: Create an organization

Create a new organization, enter your organization name and email address as shown below, and click Create organization.

Step-3: Create a workspace

Choose the workflow of your choice, here I am selecting a CLI-driven workflow.

Give a name and description to your workflow and click Create workspace.

Step-4: Clone repository

Clone this code, add the backend block in the configuration file and enter your organization-name and workspace name that you have created in previous steps. This block ensures that terraform.tfstate file doesn't get created in your workspace locally and it gets created in the remote backend i.e. Terraform cloud.

terraform {
  backend "remote" {
    organization = "organization-name"
    workspaces {
      name = "terraform-backend-handson"
    }
  }

Enter fullscreen mode Exit fullscreen mode

Step-5: Add environment variables

Go to the variables section in the left bar and click Add variable. Here we can add variables that will be used by configuration files.

Select the Environment variable and Add AWS_ACCESS_KEY and SECRET_ACCESS_KEY and don't forget to checkbox Sensitive and click Save variable.

After adding environment variables the console will look like this. You can also add terraform variables in this step.

Step-6: Terraform Login

Go to your project directory in the console and execute terraform login command and enter the value yes. Terraform will open the browser. Click Create API token.

Copy the API token and paste it into the terminal where we have executed terraform login command.

Now run the terraform init, terraform plan, and terraform apply commands to provision the infrastructure, You will see the console where the build starts.

After the triggered build is successful you will able to see the resources created in the AWS console, as well as the state file in Terraform Cloud.

Congratulations, triggered CLI workflow run successfully and the state file got created in terraform cloud.

If you face any issues contact me on my socials πŸ‘‰ Contact m e

Top comments (0)