DEV Community

Hamdi KHELIL
Hamdi KHELIL

Posted on

1

๐Ÿ” Secure Secret Management with SOPS in Terraform & Terragrunt

When managing infrastructure as code (IaC), keeping secrets safe while still making them accessible to Terraform/Terragrunt is a challenge. Storing secrets in plaintext is a security risk ๐Ÿšจโ€”and thatโ€™s where SOPS (Secrets OPerationS) comes in!

In this guide, weโ€™ll cover:

  • โœ… How to use SOPS with age and GPG
  • โœ… How to configure SOPS with sops.yaml for better management
  • โœ… How to use Terragruntโ€™s built-in SOPS decryption (without run_cmd)
  • โœ… A GitHub Actions workflow to securely use secrets in CI/CD

๐Ÿ“Œ Why Use SOPS?

SOPS is an open-source tool from Mozilla that lets you encrypt and decrypt secrets easily. It supports multiple encryption methods, including GPG, AWS KMS, Azure Key Vault, Google Cloud KMS, and age.

Hereโ€™s why itโ€™s awesome:

  • โœ… Keeps secrets encrypted in Git repositories
  • โœ… Works with YAML, JSON, ENV files
  • โœ… Has built-in support in Terragrunt (no extra scripting needed!)
  • โœ… Integrates with GitHub Actions, Kubernetes, and CI/CD pipelines

Now, letโ€™s see how to use SOPS with age and GPG, then configure it properly for Terragrunt and GitHub Actions.

๐Ÿ”‘ Using SOPS with age

Age is a modern, simple, and secure encryption tool. If youโ€™re new to encryption, age is a great alternative to GPG.

โœจ Step 1: Install age and sops

First, install age and sops:

sudo apt install age    # Ubuntu/Debian
Enter fullscreen mode Exit fullscreen mode

โœจ Step 2: Generate an age Key

Run:

age-keygen -o ~/.config/sops/age/keys.txt
Enter fullscreen mode Exit fullscreen mode

This will generate a key similar to:

# public key: age1xxxxxxx
AGE-SECRET-KEY-1XXXXXXYYYYYYYYZZZZZZ
Enter fullscreen mode Exit fullscreen mode

Copy the public key (age1xxxxxxx)โ€”this will be used for encryption.

โœจ Step 3: Encrypt a YAML File with SOPS

Create a file called secrets.yaml:

db_user: "admin"
db_password: "supersecret"
Enter fullscreen mode Exit fullscreen mode

Now, encrypt it using SOPS:

sops --encrypt --age age1xxxxxxx -i secrets.yaml
Enter fullscreen mode Exit fullscreen mode

If you open secrets.yaml, you'll see it's fully encrypted! ๐Ÿ›ก๏ธ

To decrypt:

sops --decrypt secrets.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง Configuring sops.yaml for Better Management

Instead of specifying the encryption method manually every time, SOPS supports a configuration file (.sops.yaml). This makes it easier to manage secrets across teams.

Create .sops.yaml in your repository:

creation_rules:
  - path_regex: secrets/.*\.yaml$
    age:
      - age1xxxxxxx  # Replace with your public key
  - path_regex: secrets/.*\.json$
    pgp:
      - ABC12345  # Replace with your GPG key ID
Enter fullscreen mode Exit fullscreen mode

Now, when encrypting secrets inside the secrets/ folder, SOPS will automatically use the right encryption method! ๐ŸŽ‰

Encrypt a new secret:

sops --encrypt -i secrets/app.yaml
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Using SOPS with Terragruntโ€™s Built-in Decryption

Terragrunt has native support for SOPS, meaning you donโ€™t need to use run_cmd(). Instead, you can directly reference encrypted files in your terragrunt.hcl.

โœจ Step 1: Encrypt the Secrets

Create secrets.yaml:

aws_access_key: "AKIAxxxxxxxxxxxx"
aws_secret_key: "abcdefghijklmno1234567890"
Enter fullscreen mode Exit fullscreen mode

Encrypt it:

sops --encrypt -i secrets.yaml
Enter fullscreen mode Exit fullscreen mode

โœจ Step 2: Use Terragrunt's Built-in SOPS Decryption

Modify terragrunt.hcl:

locals {
  secrets = yamldecode(sops_decrypt_file("secrets.yaml"))
}

inputs = {
  aws_access_key = local.secrets.aws_access_key
  aws_secret_key = local.secrets.aws_secret_key
}
Enter fullscreen mode Exit fullscreen mode

Now, when you run:

terragrunt apply
Enter fullscreen mode Exit fullscreen mode

Terragrunt automatically decrypts secrets.yaml without requiring external scripts! ๐Ÿš€

๐Ÿค– Using SOPS in GitHub Actions

When using GitHub Actions, we need to decrypt secrets safely without exposing them.

โœจ Step 1: Store the age Private Key in GitHub Secrets

Go to GitHub โ†’ Your Repo โ†’ Settings โ†’ Secrets and variables โ†’ Actions, and add:

  • SOPS_AGE_KEY: The private key from ~/.config/sops/age/keys.txt

โœจ Step 2: Use SOPS in a GitHub Workflow

Create .github/workflows/deploy.yml:

name: Deploy with Terraform & SOPS

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y sops age

      - name: Set up SOPS
        run: |
          mkdir -p ~/.config/sops/age/
          echo "${{ secrets.SOPS_AGE_KEY }}" > ~/.config/sops/age/keys.txt
          chmod 600 ~/.config/sops/age/keys.txt

      - name: Decrypt Secrets
        run: sops --decrypt secrets.yaml > secrets.decrypted.yaml

      - name: Deploy with Terraform
        run: |
          terragrunt run-all apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฅ What Happens in This Workflow?

  1. Checks out the code โœ…
  2. Installs SOPS & age โœ…
  3. Loads the age private key from GitHub Secrets โœ…
  4. Decrypts secrets into a temporary file โœ…
  5. Runs Terraform/Terragrunt with the decrypted secrets โœ…

Security Tip:
Make sure secrets.decrypted.yaml is ignored in .gitignore and is never committed to Git!

๐ŸŽฏ Wrapping Up

SOPS is a powerful and secure way to manage secrets for Terraform, Terragrunt, and GitHub Actions. With age encryption, .sops.yaml for better configuration, and Terragrunt's built-in decryption, managing secrets has never been easier! ๐Ÿ’ช

By integrating SOPS into your workflow, you get:

  • โœ… Encrypted secrets in Git repositories
  • โœ… Easy decryption in Terraform/Terragrunt
  • โœ… Safe usage of secrets in CI/CD

Want to take it a step further? Try using AWS KMS, GCP KMS, or Azure Key Vault instead of age/GPG for even tighter security! ๐Ÿ”๐Ÿš€

Have questions or suggestions? Drop them in the comments! ๐Ÿ’ฌ

Happy clustering and stay safe ! ๐Ÿ”

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup ๐Ÿš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment Josรฉ and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere โ€œthank youโ€ often brightens someoneโ€™s dayโ€”share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay