When you start using Terraform more, you need to understand Terraform State and its importance to your Terraform code. To quote HashiCorp:
Terraform must store state about your managed infrastructure and configuration. This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures.
https://www.terraform.io/docs/state/index.html
Terraform State is actually a file that Terraform refers to every time you run terraform plan
, terraform apply
or terraform destroy
. Terraform State is by default stored locally in a file named “terraform.tfstate”. However, as your usage of Terraform gets more advanced, it is usually better to store your Terraform State remotely to take advantage of remote storage features such as versioning and deletion protection.
Backend to store Terraform Remote State
With remote state, Terraform writes the state data to a remote data store, which can then be shared between all members of a team. Terraform supports storing state in Terraform Cloud, HashiCorp Consul, Amazon S3, Azure Blob Storage, Google Cloud Storage, Alibaba Cloud OSS, and more.
https://www.terraform.io/docs/state/remote.html
Other than remote data storage features, remote state enables collaboration by sharing this state file across team members. There are a couple of options to store the remote state mentioned above. However, we will explore using Amazon S3 in this blog post.
Use Terraform to provision Backend
I have written some simple Terraform code published on Github to help you provision a backend on AWS extremely easily. Instructions on prerequisites, installation and usage are all found in the readme.
If you are having issues, please let me know via email: tzhenkai@outlook.com.