iac.htora.dev · security templates

Home/Templates/Somewhere to keep secrets that rotates them

Somewhere to keep secrets that rotates them

A managed store with scheduled rotation, a record of every read, and a way to take access away.

Terraform

Why bother

Secrets pasted into pipeline settings and environment variables never get rotated, nobody can tell who read them, and there is no way to pull them back. A managed store fixes all three.

How you know it worked

Rotate the secret, or add a new version. The application uses the new value on its next read, with no redeploy.

How it works

1An application needs a secretread when it connects, never baked in2It asks the secrets storeusing its own identity3The read is recordedwho, which secret, when4A new value arrives on schedulegenerated on AWS, a reminder on Google Cloud
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Terraform. Secrets Manager with a rotation function

What you need first

  • An account where you can create secrets, Lambda functions, and IAM roles.
  • For Standard, the ARNs of the roles that read the secret, which must exist before apply, and a CloudTrail log group and SNS topic for the alerts.

What it creates

  • A secret, and a rotation function that generates every value, including the first
  • Rotation every 30 days, run once at apply to create the first value
  • The secret under its own key, usable only through Secrets Manager (Standard)
  • A secret policy that refuses reads to every role outside the named list, administrators included (Standard)
  • Alarms on refused reads, changes to the secret or its policy, and failed rotations (Standard)

See the three alerts this creates

The code

Quick startReadyone file, 174 lines
One file. A secret, and a rotation function that generates every value, so none passes through Terraform.
Change these first
  • name = "app/api-token" main.tf, line 16
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform output verify
    Prints the commands that prove it worked.
  6. terraform destroy
    When you have finished. If it refuses, How to undo it below explains why.
Download main.tf
Checks

Formatting passedValid passedLint passed

Security scan: 35 checks passed, and six rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/archive 2.8.1, hashicorp/aws 6.66.0.

outcomes/secrets-backend/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfive files, 348 lines
Adds its own key, a read policy naming the only roles allowed, the rotation code in its own file with its log kept, and alarms on refused reads, changes, and failed rotations.
Change these first
  • name. The secret's name, such as app/api-token.
  • reader_role_arns. Roles allowed to read the secret. Everyone else is refused, administrators included. Remove a role here to take its access away.
  • trail_log_group. CloudWatch log group a CloudTrail trail delivers to, for the alerts.
  • alert_topic_arn. SNS topic for the alerts.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    name = ""
    reader_role_arns = ""
    trail_log_group = ""
    alert_topic_arn = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all five files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 60 checks passed, and nine rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/archive 2.8.1, hashicorp/aws 6.66.0.

outcomes/secrets-backend/aws/t1
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
rotate.py
Open rotate.py as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
HardenedReadysix files, 228 lines
Standard as a module. Plan rules refuse the secret, key, or rotation deleted, a recovery window under seven days, a public secret policy, the reader deny dropped, rotation without a schedule, silent alarms, key rotation off. A daily plan reports drift, and the scan fails on any unexplained finding.
Change these first
  • name. The secret's name, such as app/api-token.
  • reader_role_arns. Roles allowed to read the secret. Everyone else is refused, administrators included. Remove a role here to take its access away.
  • trail_log_group. CloudWatch log group a CloudTrail trail delivers to, for the alerts.
  • alert_topic_arn. SNS topic for the alerts.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    name = ""
    reader_role_arns = ""
    trail_log_group = ""
    alert_topic_arn = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 54 checks passed, and none flagged.

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/archive 2.8.1, hashicorp/aws 6.66.0.

outcomes/secrets-backend/aws/t2
.checkov.yml
Open .checkov.yml as plain text
guard.yml
Open guard.yml as plain text
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text

How to check it worked

Run the three commands from terraform output verify. After rotate-secret, a new version ID has AWSCURRENT and the old one AWSPREVIOUS.

What catches people

The value never passes through Terraform, so it never lands in state. Defining a secret version in Terraform would put the value in state in plain text.

The function's setSecret and testSecret steps are empty, because the value is a self-contained token. For a database or vendor key, those two steps must update and test the real system, or rotation breaks every client.

Applications that copy the secret into an environment variable at start keep the old value until they restart. Read it at use time, or cache it for minutes.

Standard's key policy names the reader roles, and a key policy naming a role that does not exist fails to apply. Create the roles first.

How to undo it

terraform destroy schedules the secret for deletion: seven days for the quick start, 30 for Standard. aws secretsmanager restore-secret stops it inside that window.

What it costs

About 40 cents a month per secret, plus a small charge per 10,000 reads. The rotation function costs next to nothing.

Good to know

  • A secret that never changes is a secret that stays leaked. Rotation limits how long a copy taken from a laptop, a log, or a pipeline keeps working.
  • Taking access away is part of the design. Standard lists who may read each secret, so removing one line removes one reader.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.