# Remote State

Terraform allows us to keep the state of what's being deployed preserved. By default this is done locally. However, it is possible to keep the state saved in a remote backend as well. Currently there are two supported backends for this purpose,

* AWS S3
* Consuel

When working with multiple infrastructure instances with a team of developers, it is possible to get the states conflicted and also there could be sensitive information present in the state. Considering all that is it good to go with one of remote backends for keeping the state.

Frist step of configuring backend for keeping remote state is to provision an S3 bucket and then locally configure AWS CLI with configure command.

Once above it done, create a file with the name `backend.tf` with below content,

```
terraform {
  backend "s3" {
    bucket = "terraform-isuru-devops"
    key    = "terraform/isuru-1"
    region = "us-east-2"
  }
}
```

In here I configure the backend to be an S3 setup. I have to specify a bucket name and a path along with the region it is located in.

Initialize the backend with below command,

```
$ terraform init
Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

...
```

Now the backend is ready to keep the state remotly stored. To further experiement I used the same content of [03-Output-Attributes](https://github.com/isurusiri/infra-n-plat-notes/tree/c9356020fbefd1428788b7ab3b924ecf14cc613c/03-Output-Attributes.md).

Once we apply the changes, it should be possible to see our terraform state available in the S3 bucket.
