Terraform Cloud As A Remote Backend
Published: 2022-07-05 14:52:41

**_On my previous [article,](https://dev.to/moses_itoya/using-aws-s3-for-terraform-backend-4h9m) I gave a step by step process on using AWS S3 for terraform backend, I also talked about terraform state file and terraform lock file quite extensively._**

In summary, the state file contains information about configuration and managed infrastructure. This state is used by terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructure. This state is stored by default in a local file named `terraform.tfstate`, but it can be stored **remotely**, which works better in a team environment. Read more about terraform state [here](https://www.terraform.io/language/state)

**Backends** are where Terraform's state snapshots are stored. There are different [types of backends](https://www.terraform.io/language/settings/backends) on Terraform, they include; 
- **Local**(_which is the default_)
- **remote**(_which we would look into in this article_)
- **S3**(_which we've previously talked about_)
- **Kubernetes**(_stores the state in a Kubernetes secret_)
- **etcd**(_does not support state locking_) and others. 

> **Securing of your terraform state file is your responsibility.**
 
We would be migrating a state of a project which has been local, remotely to terraform cloud. This is for better security, better team activity and a great UI.
 

**Steps to migrate our state**
1. **Set Up a** [**Terraform cloud Account**](https://app.terraform.io/) - Sign up for a new account if you don't already have one ![Image description](https://community.ops.io/remoteimages/uploads/articles/3aota52pug16uyx21gwc.png) - Log in and create an Organization. Give it any name you like ![Image description](https://community.ops.io/remoteimages/uploads/articles/dbxvlp1zkrivetwfbnlx.png) - Create a `cloud` code block on `main.tf`. When we refactor the code, a `backend.tf` file would be create where the `cloud` code block would be moved to. ```
terraform {
  cloud {
    organization = "static-website"

    workspaces {
      name = "static-web-dry-run"
            }
        }
    }
``` **NOTE: The organization must already exist on terraform cloud, the workspaces however doesn't have to as it would be created if it doesn't already**




2. **Authenticate with Terraform Cloud** - Having defined your Terraform Cloud configuration, authentication with Terraform Cloud is necessary for initialization. Run `terraform login` ![Image description](https://community.ops.io/remoteimages/uploads/articles/qri2elj73ny60dzm7qfy.png) - Follow the prompt and set up the API keys which would be used for authentication ![Image description](https://community.ops.io/remoteimages/uploads/articles/o49as9h881holpwdiy3f.png) ![Image description](https://community.ops.io/remoteimages/uploads/articles/zqr3rp77qyrj10921gt7.png) ![Image description](https://community.ops.io/remoteimages/uploads/articles/kmladd7u4jiqly5qf05i.png) - You should get a successful output like this ![Image description](https://community.ops.io/remoteimages/uploads/articles/4wg5vbcmwrsbz64toqy7.png)


3. **Migrate the state file** - Run `terraform init` so our new `cloud` code block configuration can be recognized ![Image description](https://community.ops.io/remoteimages/uploads/articles/1phntsz0aa93ry7fh6cz.png)

4. **Configure the Terraform Cloud Workspace** - Log into the your [Terraform cloud Account](https://app.terraform.io/)

- Confirm the workspace defined can be seen in the organization ![Image description](https://community.ops.io/remoteimages/uploads/articles/umf6fzn5yetejhi3dci3.png)

- Run an apply and confirm the state is there on the terraform cloud workspace ![Image description](https://community.ops.io/remoteimages/uploads/articles/ws5ec85nqs7jaupte598.png) ![Image description](https://community.ops.io/remoteimages/uploads/articles/kefibke36vdgk7y3dsny.png)