Storing state files locally only may cause the following issues:
tfstate
file is stored locally in the current working directory by default. If computer damage causes file loss, all the resources corresponding to the tfstate
file will become unmanageable, leading to a resource leak.tfstate
file cannot be shared among group members.To facilitate the storage and sharing of state files, Terraform introduces the remote state storage mechanism called "backend", an abstract remote storage API. Similar to a provider, backend supports a variety of remote storage services as described in Available Backends. A Terraform backend has two modes:
plan
and apply
on a remote server) in addition to the standard features.terraform init
to verify and configure the backend.tfstate
file is stored in the local directory by default.The definition of a backend
block is nested in a top-level Terraform block. This document uses the Tencent Cloud Object Storage (COS) service as an example for configuration. For more information on other storage modes, see Available Backends.
terraform {
backend "cos" {
region = "ap-nanjing"
bucket = "tfstate-cos-1309190246"
prefix = "terraform/state"
}
}
If you have the tfstate-cos-1309190246
bucket in COS, the Terraform state information will be written into the terraform/state/terraform.tfstate
file as shown below:
Was this page helpful?