Thuta Learning
AdvancedDevOpsintermediate

Security & Secrets Management

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Security & Secrets Management without any of the intimidation
  • Get hands-on running terraform commands and HCL code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

Hardcoding a password in a Terraform config leaks it the moment it's committed to a git repository — marking a variable `sensitive = true` makes Terraform hide the value in plan/apply output (it's still in the state file though, so state encryption/access control needs to be considered separately). Credentials (like an AWS access key) shouldn't be written directly into the config — set things up so the provider auto-detects them from an environment variable (`AWS_ACCESS_KEY_ID`) or a credentials file (`~/.aws/credentials`). In production, secrets are sometimes pulled dynamically from Vault (HashiCorp Vault) or a cloud secret manager (AWS Secrets Manager).

Let's connect this to a real scenario

Define a database password as `variable "db_password" { type = string; sensitive = true }`, and pass its value through a `.tfvars` file (git-ignored) or an environment variable (`TF_VAR_db_password`) — `terraform plan` output will then show `db_password = (sensitive value)` instead of the real value. AWS credentials shouldn't be written directly into the config either — the provider auto-detects them from an `aws configure` command (or a CI/CD environment variable).

Let's look at it together

hcl
variable "db_password" {
  description = "Database master password"
  type        = string
  sensitive   = true
}

resource "aws_db_instance" "main" {
  # ... other config ...
  password = var.db_password
}

# Set the value via environment variable, never in the .tf file:
# TF_VAR_db_password="..." terraform apply
You should see
$ terraform plan
  ~ resource "aws_db_instance" "main" {
      ~ password = (sensitive value)
    }

5-minute try-it

Define a `sensitive = true` variable, pass its value via the `TF_VAR_<name>` environment variable, and confirm the value is hidden in the plan output.

A quick word of caution

Every bit of sensitive data in the state file (local or remote) is stored in plain text — in production, be sure to set up restrictions on state file access (encryption at rest, IAM permissions).

Easy traps

  • Thinking that marking something `sensitive = true` hides it from the state file too — it's still plain text in the state file
  • Hardcoding a credential in a `.tf` file and forgetting to add `.tf` files to `.gitignore` (since the config file has to be committed, you shouldn't put credentials in it at all)

Now try it yourself

Define a `sensitive = true` variable, pass its value via the `TF_VAR_<name>` environment variable, and confirm the value is hidden in the plan output.

You'll know it worked when: $ terraform plan ~ resource "aws_db_instance" "main" { ~ password = (sensitive value) }

Security & Secrets Management | Thuta Learning