Encrypting sensitive information in Terraform state files with SOPS is a great way to add a layer of security, especially when storing the state in a local Git repository. Below are steps to integrate SOPS into your Terraform workflow.
SOPS is a tool for encrypting and decrypting files. It supports encryption with:
- GPG
- AWS KMS
- Azure Key Vault
- Google Cloud KMS
SOPS allows partial encryption of files, making it a powerful tool for protecting sensitive information like Terraform state files.
Install SOPS on your system using the package manager of your choice:
- Linux:
sudo apt-get install sops
- macOS:
brew install sops
- Windows: Use the precompiled binaries available on the SOPS GitHub releases page.
Choose a key management system to handle encryption/decryption keys:
- Generate a GPG key:
gpg --full-generate-key
- List your GPG keys:
gpg --list-keys
- Note the GPG key ID (e.g.,
ABC12345
).
- Use Azure Key Vault, AWS KMS, or Google Cloud KMS to store encryption keys.
- Set up appropriate IAM permissions for SOPS to access these keys.
Navigate to the directory containing your Terraform state file and encrypt it using SOPS.
sops --encrypt --gpg <YOUR-GPG-KEY-ID> terraform.tfstate > terraform.tfstate.enc
terraform.tfstate
: The unencrypted state file.terraform.tfstate.enc
: The encrypted version of the state file.
The original file (terraform.tfstate
) can now be removed:
rm terraform.tfstate
When running Terraform commands, decrypt the state file to a temporary location.
sops --decrypt terraform.tfstate.enc > terraform.tfstate
Run Terraform commands (e.g., terraform apply
), and then securely delete the unencrypted file:
rm terraform.tfstate
To simplify your workflow, use a script or Makefile to handle encryption and decryption. Here's an example script:
#!/bin/bash
STATE_FILE="terraform.tfstate"
ENCRYPTED_FILE="terraform.tfstate.enc"
if [ "$1" == "apply" ] || [ "$1" == "plan" ]; then
echo "Decrypting state file..."
sops --decrypt $ENCRYPTED_FILE > $STATE_FILE
fi
terraform "$@"
if [ "$1" == "apply" ]; then
echo "Encrypting state file..."
sops --encrypt --in-place $STATE_FILE
mv $STATE_FILE $ENCRYPTED_FILE
fi
Make it executable:
chmod +x terraform-wrapper.sh
Run Terraform commands using this wrapper:
./terraform-wrapper.sh apply
Ensure unencrypted state files are ignored by Git:
# Ignore unencrypted state files
*.tfstate
*.tfstate.backup
-
Use Secure Key Management:
- For teams, prefer cloud KMS solutions (e.g., Azure Key Vault, AWS KMS) over GPG for better scalability and key rotation.
-
Automate Encryption/Decryption:
- Use pre-commit hooks or CI/CD pipelines to enforce encryption before committing state files.
-
Restrict Access to Keys:
- Ensure only authorized users can access the encryption keys.
-
Version Control Encrypted Files:
- Only the encrypted state file (
terraform.tfstate.enc
) should be added to Git.
- Only the encrypted state file (
- Encrypt the state file:
sops --encrypt --gpg <YOUR-GPG-KEY-ID> terraform.tfstate > terraform.tfstate.enc
- Add the encrypted file to Git:
git add terraform.tfstate.enc git commit -m "Add encrypted state file"
- Decrypt when needed:
sops --decrypt terraform.tfstate.enc > terraform.tfstate
Using SOPS ensures your sensitive data is secure, even if the repository is accidentally exposed.