How do you configure OpenTofu state encryption?
AdvancedProductionHCP Terraform, OpenTofu, Scale and Cost
Answer
Define a key provider, a method and the encryption target in the terraform block.
terraform {
encryption {
key_provider "pbkdf2" "main" { passphrase = var.state_passphrase }
method "aes_gcm" "default" { keys = key_provider.pbkdf2.main }
state { method = method.aes_gcm.default }
plan { method = method.aes_gcm.default }
}
}
Production setups use a KMS key provider (AWS KMS, GCP KMS) instead of a passphrase. Encryption is client-side, so even a bucket reader sees ciphertext. Back up keys: losing them means losing state.
More on HCP Terraform, OpenTofu, Scale and Cost
How do you design workspaces and projects in HCP Terraform?IntermediateWhat execution modes does HCP Terraform offer?BeginnerWhat are the three run workflows in HCP Terraform?BeginnerHow are variable sets and sensitive variables used?BeginnerHow is HCP Terraform priced, and how does it affect design?IntermediateHow do you choose between HCP Terraform, Atlantis, Spacelift and plain GitHub Actions?Advanced