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

State Encryption at Rest

Enable server-side encryption for remote state using provider-native encryption features and KMS keys.

State Encryption at Rest

What you'll learn: How to protect your Terraform state files from unauthorized access by enabling encryption when they're stored in remote backends.

When your state file sits in a remote backend like S3, Azure Storage, or GCS, it contains sensitive data—resource IDs, connection strings, sometimes even secrets. Without encryption, anyone with storage access could read this plaintext data. Encryption at rest means the storage system automatically encrypts the file before writing it to disk and decrypts it only when authorized users request it.

How Provider-Native Encryption Works

Most cloud storage services offer built-in encryption that requires minimal configuration. Think of it like a safe deposit box: the bank (cloud provider) encrypts your box contents automatically, and only authorized key-holders can open it.

For S3 backends, you enable server-side encryption in your backend configuration:

terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    kms_key_id     = "arn:aws:kms:us-east-1:123456789:key/abc-123"
    dynamodb_table = "terraform-locks"
  }
}

The encrypt = true setting activates default AES-256 encryption. For stronger control, specify a kms_key_id to use AWS KMS (Key Management Service), which provides audit trails and fine-grained access control over who can decrypt the state.

Why This Matters

Without encryption, a compromised storage account exposes your entire infrastructure's configuration. Encryption at rest ensures that even if someone gains storage access, they can't read the state without the encryption keys—which are managed separately with strict IAM policies.

Key Takeaway: Always enable encryption at rest for remote state backends using provider-native features like encrypt = true and KMS keys to protect sensitive infrastructure data from unauthorized access.