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.
Rotate the secret, or add a new version. The application uses the new value on its next read, with no redeploy.
How it works
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 startReady
name = "app/api-token"main.tf, line 16
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 35 checks passed, and six rules flagged, each with a written reason:
- Lambda concurrency limit
- Lambda dead-letter queue
- Lambda inside a VPC
- Secrets Manager secret encrypted with your own key
- Lambda code signing
- Lambda tracing
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/archive 2.8.1, hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
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.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# 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.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 60 checks passed, and nine rules flagged, each with a written reason:
- IAM permissions management without constraints
- IAM write access without constraints
- Lambda concurrency limit
- Lambda dead-letter queue
- Lambda inside a VPC
- Log group encrypted with KMS
- Lambda code signing
- IAM resource of *
- Lambda tracing
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/archive 2.8.1, hashicorp/aws 6.66.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open rotate.py as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
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.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# 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.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
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.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
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.
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.
Terraform. Key Vault with role-based access
What you need first
- A subscription, and Owner or User Access Administrator on it, to grant vault roles. Standard's locks need the same rights.
What it creates
- A Key Vault using Azure roles only, with purge protection and 90-day soft delete
- Key Vault Secrets Officer for you
- Every read and change logged to a Log Analytics workspace, and an email on refused access
- A vault firewall allowing named address ranges, readers and officers by identity, and logs kept two years (Standard)
- Alerts on changes, and an optional HSM-backed key that rotates every 90 days (Standard)
- Three native activity log alerts, which Azure evaluates without the workspace: a diagnostic setting deleted, a diagnostic setting written, and a log search rule disabled by Azure
- Cannot-delete locks on the workspace, the action group, and every alert, and a native alert on a lock deleted (Standard)
See the six alerts this creates
The code
Quick startReady
alert_email = "security-alerts@example.com"main.tf, line 26
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid not runLint passed
Security scan: three checks passed, and three rules flagged, each with a written reason:
Last run 2026-11-09 with OpenTofu v1.12.6.
Open main.tf as plain text
StandardReady
subscription_id. Subscription for the vault and its monitoring.alert_emails. Addresses told about refused access and changes.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid not runLint passed
Security scan: five checks passed, and three rules flagged, each with a written reason:
Last run 2026-11-09 with OpenTofu v1.12.6.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
subscription_id. Subscription for the vault and its monitoring.alert_emails. Addresses told about refused access and changes.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid not runLint passed
Security scan: five checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Wait up to 90 minutes after the first apply, while the vault's diagnostic setting starts sending. Then set a test secret, and read it as an identity with no role on the vault. It is Forbidden, and the email follows once the entry reaches the workspace. Standard prints more checks with terraform output verify.
Purge protection cannot be switched off, and a deleted vault's name stays taken until its retention ends.
Standard's vault firewall refuses Terraform too. Run apply from an allowed address range.
Role assignments take a few minutes to reach Key Vault. A Forbidden right after apply may be that delay.
Every alert, the native ones included, emails through one action group. In the quick start, deleting or disabling that group, or deleting the resource group, silences them all. Standard's locks stop the deletes, but whoever can edit the group can still disable it or change its addresses.
Standard locks the alert path alone. A lock on the vault or its resource group would also block deleting the vault's role assignments, and taking a reader away must stay one line. Any locked resource still stops the resource group being deleted.
Standard's locks block Terraform too. A change that replaces or removes a locked resource fails while its lock is on. Remove the locks with terraform destroy -target=azurerm_management_lock.alert_path, then apply, and they return. Their removal fires the lock-deleted alert.
A cannot-delete lock does not stop a workspace purge. Nothing here alerts on one, so keep the Data Purger role from anyone who does not need it.
The diagnostic-setting-written alert fires on every diagnostic setting created or changed in the subscription, policy remediation included, because a rewrite can drop AuditEvent or send the record elsewhere without a delete.
A diagnostic setting can take up to 90 minutes to start sending after it is made, so run the drills after that.
The alert rules have no identity of their own, so each reads the workspace as whoever last saved it. Azure disables a rule whose query fails for a week, and the rule-disabled alert reports that without the workspace.
How to undo it
In Standard, remove the locks first with terraform destroy -target=azurerm_management_lock.alert_path, or module.standard.azurerm_management_lock.alert_path in Hardened. Then terraform destroy deletes the vault, which stays recoverable for 90 days and cannot be purged. Secrets in it are unreadable in the meantime.
What it costs
Per 10,000 operations, more for HSM keys, plus Log Analytics by GB. The activity log alerts are free.
Terraform. Secret Manager with versions and conditional access
What you need first
- A project where you can create secrets and Pub/Sub topics.
- The service account that reads the secret.
- For Standard, a Cloud KMS key in each region the secret is stored in, such as the key baseline template builds.
What it creates
- A secret, readable by one service account
- A Pub/Sub topic that receives a rotation reminder every 30 days, and a subscription to see them
- Storage only in named regions, each under your own key (Standard)
- Time-limited read grants that end by themselves, and a one-day delay before a destroyed version is gone (Standard)
- Data Access logs on every read, and alerts on reads by anyone not listed and on destruction or access changes (Standard)
See the two alerts this creates
The code
Quick startReady
project_id = "your-project-id"main.tf, line 17reader = "serviceAccount:app@your-project-id.iam.gserviceaccount.com"main.tf, line 18
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: one check passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
StandardReady
project_id. Project containing the secret.secret_id. The secret's name.readers. Members allowed to read the secret, such as serviceAccount:<email>. Remove one here to take its access away.locations. Regions the secret is stored in, each with a Cloud KMS key in that same region, such as keys from the key baseline template.alert_emails. Addresses told when someone outside the readers reads the secret, or when it is destroyed or its access changed.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" secret_id = "" readers = "" locations = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: two checks passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
project_id. Project containing the secret.secret_id. The secret's name.readers. Members allowed to read the secret, such as serviceAccount:<email>. Remove one here to take its access away.locations. Regions the secret is stored in, each with a Cloud KMS key in that same region, such as keys from the key baseline template.alert_emails. Addresses told when someone outside the readers reads the secret, or when it is destroyed or its access changed.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" secret_id = "" readers = "" locations = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: two checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Add a version with gcloud, then read it as the reader. Add another, and the latest version changes. terraform output verify prints the commands.
Terraform creates the secret, never its value. Add versions with gcloud, so no value lands in state.
Secret Manager only sends the rotation reminder. Something you run has to read the topic and add the new version.
Reads are Data Access logs, off by default, so without Standard nobody can say who read the secret.
Roles granted on the whole project, such as Secret Manager Secret Accessor, reach every secret in it. The reader list here covers grants on this secret only, which is why Standard alerts on any read by someone not listed.
How to undo it
terraform destroy deletes the secret and every version at once. With Standard, destroying a single version waits a day, and can be undone within it.
What it costs
A few cents a month per active version, plus a small charge per 10,000 reads. Data Access logs are billed by volume beyond the free allowance.
Terraform. OCI Vault with policies scoped to one vault
What you need first
- A compartment, and permission to manage vaults, keys, and policies.
- A group for readers and a group for officers.
What it creates
- A vault and a hardware-protected key for secrets
- A policy letting readers read secrets in this vault and officers manage them, without deleting the vault or key
- An email when someone is refused a secret, from four connectors on the audit log
- Readers limited to named secrets, workloads reading through dynamic groups, and a fifth connector emailing on every change to a secret, the key, or the vault (Standard)
See the five alerts this creates
The code
Quick startReady
tenancy_ocid = "ocid1.tenancy.oc1..change-me"main.tf, line 21compartment_ocid = "ocid1.compartment.oc1..change-me"main.tf, line 22alert_email = "security-alerts@example.com"main.tf, line 23readers_group = "secrets-readers"main.tf, line 24officers_group = "secrets-officers"main.tf, line 25
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: one check passed, and none flagged.
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. Policies are created here.region. Region identifier, such as us-ashburn-1.compartment_ocid. Compartment for the vault, key, and alerts.officers_group. Group that may create and change secrets in this vault. It cannot delete the vault or the key.alert_email. Address told about refused reads and about changes. It must confirm the subscription before mail arrives.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" region = "" compartment_ocid = "" officers_group = "" alert_email = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: one check passed, and none flagged.
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
tenancy_ocid. The tenancy. Policies are created here.region. Region identifier, such as us-ashburn-1.compartment_ocid. Compartment for the vault, key, and alerts.officers_group. Group that may create and change secrets in this vault. It cannot delete the vault or the key.alert_email. Address told about refused reads and about changes. It must confirm the subscription before mail arrives.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" region = "" compartment_ocid = "" officers_group = "" alert_email = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: one check passed, and none flagged.
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Confirm the subscription email first. Then, as an officer, create a secret. As a user in neither group, read it with oci secrets secret-bundle get-secret-bundle-by-name. The call is refused, and the email follows within a few minutes.
OCI answers a caller without permission with 404, the same as for a name that does not exist. The refused-read alert fires on both.
Oracle documents and and or for a connector's filter, with no parentheses and no order for mixing them. So a refused read is four connectors: either read operation, answered 401 or 404. Connector Hub allows 20 connectors per region by default and five on a trial account. Quick start uses four and Standard five.
Deleting a vault or key is scheduled seven to 30 days out, and cannot be hurried. A destroyed vault's secrets stay unreadable until then.
Secret contents are never in Terraform here. Officers add them with the CLI or console, so they stay out of state.
Deleting the notification topic or the connectors' policy stops every alert, and none of them can report it. So does moving a connector out of the compartment, for its own alert. The daily drift plan in Hardened is what notices.
How to undo it
terraform destroy schedules the vault and key for deletion and removes the policies and alerts.
What it costs
Software keys are free. HSM key versions bill monthly beyond a free allowance. Check the Vault price list for secrets.
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.