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.
Encrypt something, switch the key off, and try to decrypt it. The decrypt must fail.
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. 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 startReady
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: 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.
Open main.tf as plain text
StandardReady
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.
Formatting passedValid passedLint passed
Security scan: 24 checks passed, and four rules flagged, each with a written reason:
- IAM permissions management without constraints
- IAM write access without constraints
- SNS topic encrypted
- IAM resource of *
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open alerts.tf 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
HardenedReady
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.
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.
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 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.
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.
Terraform. A Key Vault key with a rotation policy, Azure roles for access, and an audit log
What you need first
- az login, ARM_SUBSCRIPTION_ID exported for the quick start, and rights to assign roles on the vault.
- For Standard, your public IP in allowed_ip_ranges, because the vault refuses everything else, and the object IDs of the key's users.
What it creates
- A vault using Azure roles for access, and a key that rotates thirty days before its yearly expiry
- The Key Vault Crypto Officer role for whoever applies it
- A Premium vault with an HSM-backed key, purge protection, a default-deny firewall, users granted on the key alone, and every vault operation sent to Log Analytics (Standard)
The code
Quick startReady
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 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 seven rules flagged, each with a written reason:
- Key Vault private endpoint
- Key Vault firewall
- Key Vault purge protection
- Key backed by an HSM
- Key Vault public network access off
- Key expiration date
- Key Vault recoverable
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open main.tf as plain text
StandardReady
subscription_id. Subscription the vault goes in. The provider has required it since version 4.
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 = ""
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: five checks passed, and three rules flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.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
subscription_id. Subscription the vault goes in. The provider has required it since version 4.
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 = ""
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: five checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.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 terraform output verify. It prints four az commands: encrypt a word, disable the key, try to decrypt and watch it be refused, and enable it again. For Standard, run them from an allowed address.
Owner on a subscription can create a vault and still cannot create a key inside it. Keys are data, and need a data role such as Key Vault Crypto Officer. Both templates grant it to whoever applies them.
Without a diagnostic setting the vault keeps no searchable record of who used a key. The Standard version sends every operation to Log Analytics, in the AzureDiagnostics table.
How to undo it
terraform destroy removes the quick start completely, because purge protection is off there. A deleted Standard vault and its keys stay recoverable for 90 days, nobody can purge them sooner, and the vault name stays taken until then.
What it costs
Vaults charge per 10,000 operations. HSM-backed keys in a Premium vault add a monthly charge per key. The Log Analytics workspace is charged per gigabyte ingested.
Terraform. A Cloud KMS key with 90-day rotation, roles kept apart, and every use logged
What you need first
- gcloud auth application-default login, and permission to switch on the Cloud KMS API.
- For Standard, permission to change the project's audit log settings, and the members who manage and use the key.
What it creates
- The Cloud KMS API, a key ring, and a key that rotates every 90 days
- Hardware protection, a 30-day wait before destroyed versions are gone, and deletion protection (Standard)
- Managers granted on the ring, users on the key, and Data Access logging for Cloud KMS (Standard)
The code
Quick startReady
project_id = "your-project-id"main.tf, line 17
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: three 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
StandardReady
project_id. Project that contains the key ring.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
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: five checks passed, and none flagged.
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 that contains the key ring.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
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: five 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
Run terraform output verify. It prints five gcloud commands: write a word to a file, encrypt it, disable key version 1, try to decrypt and watch it fail, and enable version 1 again.
Google Cloud switches off key versions, one at a time. Data encrypted under an older version stays readable until that version is disabled too.
Encrypt and decrypt calls are Data Access logs, which are off by default, so nobody can say who used the key until they are on. The Standard version switches them on for Cloud KMS. That resource owns the setting for the whole project, so merge it with anything else that manages Cloud KMS audit settings.
Key rings and keys can never be deleted, so their names are used up for good. The templates add a random suffix to the ring so a second apply after a destroy does not collide.
How to undo it
Neither level can be removed completely. terraform destroy schedules the key versions for destruction and forgets the ring and key, which stay in the project. The Standard key refuses destroy until prevent_destroy is removed, and its versions wait 30 days, during which they can be restored.
What it costs
A small monthly charge per active key version, more for hardware protection, plus per-operation charges. Data Access logs are charged by volume beyond the free allowance.
Terraform. A Vault key with hardware protection, an off switch in code, and policies keeping roles apart
What you need first
- ~/.oci/config working, plus a compartment OCID and a region identifier.
- For Standard, rights to create policies in the root compartment, and the names of the groups that manage and use the key.
What it creates
- A vault and an AES-256 key
- A hardware-protected key whose on or off state is a variable (Standard)
- A policy letting the manager group manage keys and vaults, and the user group use this one key (Standard)
The code
Quick startReady
compartment_ocid = "ocid1.compartment.oc1..change-me"main.tf, line 14region = "us-ashburn-1"main.tf, line 15
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 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 none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. The IAM policy is created here.compartment_ocid. Compartment that contains the vault and key.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
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-10-02 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. The IAM policy is created here.compartment_ocid. Compartment that contains the vault and key.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
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-10-02 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
Run terraform output verify. It prints four oci commands: encrypt a word, disable the key, try to decrypt and watch it fail, and enable it again. The output fills in the vault's own endpoints.
Every vault has two addresses. Encrypt and decrypt go to its crypto endpoint, and management calls such as disable go to its management endpoint. A call sent to the wrong one fails.
In Standard, switching the key off is a change to enabled in code, reviewed like any other. Switching it off in the console is drift, and the next apply switches it back on.
These templates rotate keys by hand, with oci kms management key rotate. Automatic rotation depends on the vault type, so check what your tenancy supports before relying on it.
How to undo it
terraform destroy schedules the vault and key for deletion, with a waiting period of at least seven days during which it can be cancelled. After that, anything encrypted under the key is gone.
What it costs
Software-protected keys carry no charge. Hardware-protected keys beyond the tenancy's free allowance are billed per key version, so check the rate card.
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.