Course contentsShow
← Cloud and Infrastructure
Lesson 791 of 2,98915. Terraform in PracticeFree lesson

Partial Backend Configuration

Leverage partial configuration and backend config files to avoid hardcoding sensitive backend credentials.

Partial Backend Configuration

What You'll Learn

You'll learn how to keep sensitive backend credentials out of your version-controlled Terraform code by using partial backend configuration and separate config files.

The Problem with Hardcoded Backend Settings

When you configure a backend like S3 or Azure Storage, you might be tempted to put everything directly in your Terraform code:

terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    access_key     = "AKIAIOSFODNN7EXAMPLE"  # ⚠️ Don't do this!
    secret_key     = "wJalrXUtnFEMI/K7MDENG..."  # ⚠️ Security risk!
  }
}

This creates a security nightmare — your credentials end up in Git, visible to everyone.

The Solution: Partial Configuration

Think of it like a restaurant menu that lists dishes but not prices — you fill in the prices separately when you order. Similarly, you can define non-sensitive backend settings in your code and provide sensitive values later:

terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    # credentials intentionally omitted
  }
}

Then supply credentials via a separate backend config file (backend.hcl) that stays out of version control:

access_key = "AKIAIOSFODNN7EXAMPLE"
secret_key = "wJalrXUtnFEMI/K7MDENG..."

Initialize with: terraform init -backend-config=backend.hcl

Key Takeaway: Partial backend configuration separates public infrastructure settings from private credentials, keeping sensitive data out of version control while maintaining reproducible backend configuration.