Terraform basics: Persisting Terraform state in S3 backend

Yogesh Upadhyay
2 min readJul 4, 2021

Terraform maintains state of infrastructure in .tfstate file. You can keep it local or store it in S3 bucket. Keeping in S3 bucket makes it easy for a team to track and update infra as needed. This is commonly referred as remote state. Whereas keeping it local also referred aslocal-state is something where only a single dev maintains the infrastructure, or its just one time infra deployment, its useful in that case. But here we are interested to explore option for remote-state where we are using S3 to maintain tf state.

Let’s quickly create an S3 bucket for this purpose using below command.

$ aws s3api create-bucket --bucket terraformbucket123 --region us-east-1
{
"Location": "/terraformbucket123"
}

Assuming you already have terraform configured. From your terraform work directory, to configure S3 backend for your terraform state, create a file such as backend.tf

Once above file created. Run

$ terraform init

As you can see in output, terraform has configured S3 as backend and will continue to use this back end unless we change the configuration.

Don’t forget to make formatting tool to keep .tf file organised and syntactically well formatted.

$ terraform fmt

Thanks for reading. Hope this quick tutorial was helpful to you. See you in next article.

--

--