Format Terraform Backend Configuration
Format terraform backend blocks for S3, Azure Blob, GCS, and other remote state storage configurations with proper alignment and structure.
Provider & Backend
Detailed Explanation
Formatting Backend Blocks
The terraform block with its backend configuration is critical infrastructure — it determines where your state file is stored. Misformatted backend blocks can lead to state file conflicts, locking issues, or accidental state loss.
S3 Backend Example
terraform {
required_version = ">= 1.5.0"
backend "s3" {
bucket = "my-company-terraform-state"
key = "production/network/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-lock"
encrypt = true
acl = "bucket-owner-full-control"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Key Formatting Details
- Aligned equals signs: All attributes within the
backendblock have their=signs at the same column, making it easy to scan values - Grouped sections:
required_version,backend, andrequired_providersare separated by blank lines - Consistent quoting: All string values use double quotes
- Nested provider source: The
required_providersblock has its own internal alignment
Azure Blob Backend
terraform {
backend "azurerm" {
resource_group_name = "terraform-state-rg"
storage_account_name = "tfstate12345"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
Notice how the longer attribute names (resource_group_name, storage_account_name) push the equals alignment further right, but all values still line up consistently.
Use Case
Standardizing backend configurations across multiple Terraform workspaces and environments, ensuring state files are correctly separated and locked.