Skip to content

How to use SOPS + age

As we saw earlier, SOPS supports multiple formats and encryption backends. In this guide, we will focus on using SOPS with age for encrypting sensitive files like *.tfvars, terraform.tfstate, and other secrets.

Terminal window
mkdir $env:APPDATA\sops\age
age-keygen -o $env:APPDATA\sops\age\keys.txt

Grab your public key:

Terminal window
grep public /path/to/keys.txt
# public key: age1xxxx...

There are multiple ways to encrypt a file using age with SOPS:

Terminal window
sops -e --age age1xxxx...,age1yyyy... opentofu.auto.tfvars > opentofu.auto.tfvars.enc

This encrypts the file using the provided public age keys.

Terminal window
export SOPS_AGE_RECIPIENTS="age1xxxx...,age1yyyy..."
sops -e opentofu.auto.tfvars > opentofu.auto.tfvars.enc

This is useful when scripting or storing your recipient key in an environment configuration.

You can also specify the recipient in a .sops.yaml file in your repository:

.sops.yaml
creation_rules:
- path_regex: \.tfvars(\.json)?$
encrypted_regex: "^(.*)$"
age: "age1xxxx...,age1yyyy..."

Using .sops.yaml is best for projects as it avoids setting env variables or using the age flag, enabling consistent, automatic encryption config.

The encryption method may vary, based on the file format

When encrypting files, you need to specify the input type. This is crucial for SOPS to understand how to handle the file correctly.

This is important to ensure that the file is encrypted correctly and can be decrypted later without issues.

For binary files, you can use the default command:

Terminal window
sops encrypt opentofu.auto.tfvars > opentofu.auto.tfvars.enc
Terminal window
sops -d opentofu.auto.tfvars.enc
Terminal window
sops -d opentofu.auto.tfvars.enc > opentofu.auto.tfvars

The decryption method may vary, based on the file format

When decrypting files, you need to specify the output type. This is crucial for SOPS to understand how to handle the file correctly.

This is important to ensure that the file is decrypted correctly and can be used later without issues.

For binary files, you can use the default command:

Terminal window
sops decrypt opentofu.auto.tfvars.enc > opentofu.auto.tfvars

You can also use SSH keys for encryption and decryption. This is particularly useful if you already manage SSH keys for other purposes.

Terminal window
sops -e --age "$(cat ~/.ssh/id_ed25519.pub)" opentofu.auto.tfvars > opentofu.auto.tfvars.enc

By default, SOPS will try to decrypt using the following SSH private keys (in order):

  • ~/.ssh/id_ed25519
  • ~/.ssh/id_rsa

If your private key is stored in a custom location or if you manage multiple keys, you can explicitly specify which one to use:

Terminal window
export SOPS_AGE_SSH_PRIVATE_KEY_FILE=~/.ssh/id_rsa
sops -d opentofu.auto.tfvars.enc > opentofu.auto.tfvars
  • Never commit decrypted files (*.tfvars, *.tfstate, secrets.json, secrets.yaml, .env etc.)
  • Use .gitignore to exclude them
  • Always encrypt before pushing
  • Share only the public key
  • Store private keys securely (e.g., in a password manager or vault)

References