DEV Community

Terraform Backends

Backend Configuration
A backend defines where Terraform stores its state data files.

Terraform Backend is a configuration option in Terraform that allows you to store and manage the state of your infrastructure in a remote or local location. The backend is responsible for storing the state file and providing an interface for reading and writing state data.

Available Backends
1.local: The local backend stores state on the local filesystem

terraform {
  backend "local" {
    path = "relative/path/to/terraform.tfstate"
  }
}

Enter fullscreen mode Exit fullscreen mode

create a file - backend.tf and add the below to save the state file locally

terraform {
  backend "local" {
    path = "C:/srinivas/terraform.tfstate"
  }
}

Enter fullscreen mode Exit fullscreen mode

2.S3 :

  • Stores the terraform state as a given key in a given bucket on Amazon S3.
  • This backend also supports state locking and consistency checking via Dynamo DB, which can be enabled by setting the dynamodb_table field to an existing DynamoDB table name.
  • A single DynamoDB table can be used to lock multiple remote state files.
  • create a file backend.tf and keep the below mentioned code
terraform {
  backend "s3" {
    bucket = "bucket_name"
    key    = "myproject/terraform.tfstate"
    region = "eu-west-1"
  }
}
Enter fullscreen mode Exit fullscreen mode

Note : You need to replace bucket_name to desired bucket and
"myproject" to the desired folder and terraform.tfstate to be replaced with the desired state file name

3.azurerm : Stores the state as a Blob with the given Key within the Blob Container within the Blob Storage Account.

When authenticating using the Azure CLI or a Service Principal (either with a Client Certificate or a Client Secret):

terraform {
  backend "azurerm" {
    resource_group_name  = "terraform-training"
    storage_account_name = "training2024"
    container_name       = "tfstate"
    key                  = "prod.terraform.tfstate"
  }
}

Enter fullscreen mode Exit fullscreen mode

Note : Replace resource group name ,storage account name , container name and key based on your requirement.

Some more backend types in terraform

  • remote
  • consul
  • cos
  • gcs
  • http
  • kubernetes
  • oss
  • pg

Conclusion : Discussed about terraform backends and will further add more about other backends. Any needs one github samples let me know in the comments will share the github repo link

๐Ÿ’ฌ If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it ๐Ÿ˜€ and follow me in linkedin

Top comments (0)