iac.htora.dev · security templates

Home/Templates/Encryption keys you control and can switch off

Encryption keys you control and can switch off

Your own keys, rotated on a schedule, with a usage log and an off switch.

Terraform

Why bother

Encryption the cloud manages for you ticks a box. It gives you no way to see who used the key and no way to cut off access in a hurry. Your own key gives you both.

How you know it worked

Encrypt something, switch the key off, and try to decrypt it. The decrypt must fail.

How it works

1Data is writtento storage, a database, a disk2Encrypted with your keyevery use recorded3The key rotates on scheduleold data stays readable4Disable the keyand the data cannot be read
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. A KMS key with yearly rotation, managers and users kept apart, and an alert on the off switch

What you need first

  • An account where you can create KMS keys.
  • For the Standard alert, a CloudTrail trail recording management events. The audit log template creates one.
  • For Standard, the ARNs of the roles that manage the key and the roles that use it.

What it creates

  • A KMS key that rotates every year, and an alias
  • A key policy that keeps the account able to manage the key through IAM
  • Separate statements for managers and users, and a deny on switching the key off or deleting it for anyone else (Standard)
  • An EventBridge rule and SNS topic that fire on any key disable, scheduled deletion, or policy change in the region (Standard)

See the one alert this creates

The code

Quick startReadyone file, 82 lines
One file. Key, alias, yearly rotation, a seven-day deletion wait, and the verify commands.
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: 13 checks passed, and three rules flagged, each with a written reason:

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

outcomes/key-baseline/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfive files, 277 lines
Adds managers and users kept apart, a deny on the off switch for everyone else, and the off switch as a variable. It also adds a 30-day deletion wait, and an alert when anyone pulls either lever.
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.
Download all five files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 24 checks passed, and four rules flagged, each with a written reason:

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

outcomes/key-baseline/aws/t1
alerts.tf
Open alerts.tf 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
HardenedReadysix files, 248 lines
Standard as a module. Plan rules refuse the key or its alerts deleted, key rotation off, and the account recovery statement dropped. They also refuse disable or delete rights outside the named admins, an alert that stops matching, and a disabled rule. A daily plan reports drift, and the scan fails on any unexplained finding.
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.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 21 checks passed, and none flagged.

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

outcomes/key-baseline/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 terraform output verify. It prints four commands: encrypt a word, switch the key off, try to decrypt and watch it fail with DisabledException, and switch it back on.

What catches people

A key policy that names only a role you later delete leaves a key nobody can manage, and only AWS Support can recover it. Both templates keep the account root statement for that reason, and the Standard deny leaves PutKeyPolicy out so a mistake stays fixable.

The alert topic is not encrypted with the key it watches. If it were, switching the key off would also stop the alert about switching it off.

How to undo it

terraform destroy schedules deletion: seven days for the quick start, thirty for Standard. aws kms cancel-key-deletion stops it inside that window. After it, anything encrypted under the key is unreadable.

What it costs

About a dollar a month per key, plus a small charge per request. The alert costs next to nothing.

Good to know

  • Rotation changes the key material used for new encryption. Every cloud keeps the older versions so existing data stays readable, which also means data stored before a rotation stays under the older version until it is rewritten.
  • Services that use envelope encryption keep a data key in memory for a while, so switching a key off may take effect only when they next ask the key service.
  • A completed key deletion is permanent. Anything encrypted only under that key can never be read again, by anyone. The waiting period before deletion is the only chance to notice. That is why the AWS Standard version alerts the moment one is scheduled.

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.