Skip to content

Remote Backends

Terraform’s remote state backends provide a centralized way to store and manage infrastructure state files, enabling collaboration, version control, and disaster recovery. This section covers configuring remote backends using AWS S3, Azure Blob Storage, and Consul, which are commonly used for distributed state management.


AWS S3 Backend

The AWS S3 backend stores Terraform state files in an S3 bucket. It requires proper IAM permissions and a bucket configured with versioning and encryption.

Configuration Example:

terraform {
  backend "s3" {
    bucket = "my-terraform-state-bucket"
    key    = "production/app-state.tfstate"
    region = "us-west-2"
    encrypt = true
  }
}

Key Requirements: - An S3 bucket with versioning enabled. - IAM policy allowing s3:PutObject, s3:GetObject, and s3:ListBucket. - AWS credentials configured via environment variables (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) or shared credentials file.

Locking and Encryption: - S3 supports state locking via AWS DynamoDB (configured via dynamodb_table argument). - Encryption at rest is enforced with encrypt = true.


Azure Blob Storage Backend

Azure Blob Storage provides a similar workflow for cloud environments. It requires a storage account and container setup.

Configuration Example:

terraform {
  backend "azurerm" {
    storage_account_name = "myterraformstorage"
    container_name       = "tfstate"
    key                  = "production/app-state.tfstate"
    access_key           = "your-azure-storage-access-key"
  }
}

Key Requirements: - An Azure storage account with a container configured for blob storage. - Storage account access key (or managed identity in Azure AD-integrated environments). - Blob container must be publicly accessible (or use signed URLs for restricted access).

Locking: - Azure Blob Storage does not natively support state locking. Use external tools like Azure Blob Lease or integrate with Consul for distributed locks.


Consul Backend

Consul, a distributed coordination tool, allows Terraform to store state in a key-value store, enabling dynamic discovery and multi-team collaboration.

Configuration Example:

terraform {
  backend "consul" {
    address = "consul.example.com:8500"
    path    = "terraform/state/production/app-state"
  }
}

Key Requirements: - A running Consul server (agent or cluster) with ACLs enabled (if using authentication). - ACL policies allowing read/write access to the specified path. - Consul’s KV store must be configured for persistence.

Advantages: - Real-time state sharing across teams. - Built-in support for locks and multi-team workflows via ACLs.


Collaboration and Best Practices

  1. Use Workspaces:
    Isolate environments (e.g., dev, prod) using terraform workspace to avoid state collisions.
    Example:

    terraform workspace new dev
    terraform workspace select dev
    

  2. Version Control:
    Store state files in Git (or another VCS) with .terraform.lock.hcl for deterministic deployments.

  3. State Locking:
    Enable locking in S3 or Consul to prevent concurrent modifications.
    Example (S3):

    backend "s3" {
      ...
      dynamodb_table = "terraform-locks"
    }
    

  4. Encryption:
    Always enable encryption at rest for state files in S3 and Azure Blob Storage.


Key takeaways

  • Remote backends (S3, Azure, Consul) enable team collaboration and state management across environments.
  • AWS S3 requires IAM permissions and encryption, while Azure Blob Storage needs access keys and proper container setup.
  • Consul offers dynamic state sharing with built-in locking and ACLs for secure multi-team workflows.
  • Always use workspaces and version control to manage state isolation and reproducibility.