Terraform Cloud: Manage Terraform Remote State file
Published: 2023-01-03 11:07:22

---
title: Terraform Cloud: Manage Terraform Remote State file
published: true
date: 2023-01-03 10:50:26 UTC
tags: 
canonical_url: https://palak-bhawsar.hashnode.dev/terraform-cloud-manage-terraform-remote-state-file
---

 **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 this file to any version control system like GitHub. Store terraform.tfstate file in the backend to keep it safe.

**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 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/](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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672135932668/3eb275fc-8ae9-4f42-88e7-e9b2f13025b0.png)

### **Step-2: Create an organization**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672136074608/6ad2304b-a9a4-48c5-acdd-14b5906fe9f2.png)

### **Step-3: Create a workspace**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672136299365/bdfea010-02db-4cf7-be83-27f13e5494c9.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672136562734/60696683-da7d-4010-9f56-532c23f21fe0.png)

### **Step-4: Clone repository**

Clone this repository [https://github.com/palakbhawsar98/Terraform/tree/main/terraform-remote-state-hands-on](https://github.com/palakbhawsar98/Terraform/tree/main/terraform-remote-state-hands-on) and 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 local but in the remote backend i.e Terraform cloud.

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

```

### **Step-5: Add environment variables**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672141509065/bd838896-484b-4088-9ea6-3d9708752bc4.png)

Select the **Environment variable** and Add AWS\_ACCESS\_KEY and SECRET\_ACCESS\_KEY and don't forget to checkbox **Sensitive** and click **Save variable**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672141605421/a23c3b1b-9096-4201-8943-1f01679cd65e.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672142456772/bca48902-0197-4631-9e9a-0dda2d28f6c9.png)

### **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.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672140160368/b60b8647-88dd-45bc-accb-f1ca62588d95.png)

Copy the API token and paste it into the terminal.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672140240551/c5917542-28b2-4548-a9e2-42408e5a5820.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672143729040/821de627-4832-4d6e-8df5-14d45b117a1e.png)

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672143778887/35371d1f-0386-4d4f-b5b4-a89100218c04.png)