Home/Templates/Block the changes you never want
Block the changes you never want
Policies that make dangerous actions fail outright, so there is nothing to alert on afterwards.
Terraform
Why bother
Alerts tell you that someone turned off logging or opened a region you do not use. By then it has happened. A guardrail makes the action fail, so there is nothing to respond to.
Try a blocked action from a member account. It should be refused.
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. Service control policies from the management account
What you need first
- AWS Organizations with all features on, and Terraform running in the management account.
- An OU or account to attach to. Start with a test OU.
- For Standard, the ARNs of the break-glass and security automation roles to exempt.
What it creates
- A policy that denies leaving the organisation, audit and detection tampering, and use of the root user
- Protection for the account-wide S3 public access block and for roles under the security path (Standard)
- A second policy that refuses regional services outside the regions you name (Standard)
- An attachment to each target you list
The code
Quick startReady
target_id = "ou-xxxx-xxxxxxxx"main.tf, line 19
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 destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 12 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
target_ids. OU or account IDs to attach both policies to. Start with a test OU.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars target_ids = ""
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: 23 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.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
target_ids. OU or account IDs to attach both policies to. Start with a test OU.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars target_ids = ""
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: 23 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
From a member account under the target, run aws cloudtrail stop-logging against any trail. It fails with an explicit deny in a service control policy. Standard prints two more checks with terraform output verify.
Service control policies never apply to the management account. Anything run there is unaffected, so keep workloads out of it.
A policy attached to the organisation root applies to every member account at once. Attach it to a test OU first, and try the actions your teams use every day.
The region rule leaves global services out, because they answer from us-east-1. A service missing from that list stops working outside the allowed regions, so review the list against what you run.
Each policy is limited to 5,120 characters, and a target takes at most five policies. Add rules to these two before adding a third.
How to undo it
terraform destroy detaches and deletes the policies, and the denied actions work again at once.
What it costs
Nothing. Service control policies are free.
Terraform. Azure Policy rules that deny, grouped in an initiative
What you need first
- A subscription, and Resource Policy Contributor or Owner on it.
- For Standard, a management group, and the same role on it.
What it creates
- Three custom policies that deny public blob access, public IP addresses on network interfaces, and key vaults without purge protection
- An initiative containing them, assigned to the subscription
- Six rules defined and assigned at a management group, adding TLS 1.2, role-based vault access, and allowed regions (Standard)
- Audit mode first, one switch to enforce, and subscription exemptions with a reason and an end date (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: zero checks passed, and none flagged.
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 provider signs in to. The policy itself is assigned to the management group.management_group_id. Management group the initiative is defined in and assigned to, such as /providers/Microsoft.Management/managementGroups/contoso.
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 = "" management_group_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: zero checks passed, and none flagged.
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 provider signs in to. The policy itself is assigned to the management group.management_group_id. Management group the initiative is defined in and assigned to, such as /providers/Microsoft.Management/managementGroups/contoso.
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 = "" management_group_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: zero 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
About 30 minutes after apply, create a storage account with --allow-blob-public-access true. It fails with RequestDisallowedByPolicy, naming the initiative. Standard prints the checks with terraform output verify.
A new assignment takes up to 30 minutes to take effect. Test after that, or a test that succeeds proves nothing.
Policy judges new and updated resources. Existing storage accounts with public access stay as they are until changed; the compliance template lists them.
Standard starts in audit mode, which refuses nothing. Read the compliance results, then set enforce to true.
The region rule allows global as well, for services that have no region. Without it, those services cannot be created.
How to undo it
terraform destroy removes the assignment and the definitions, and the denied changes work again once Azure notices, within about 30 minutes.
What it costs
Nothing. Azure Policy is free for Azure resources.
Terraform. Organization policy constraints on an organisation, folder, or project
What you need first
- A project inside an organisation.
- The Organization Policy Administrator role, which can only be granted at organisation level.
- For the Standard member domain limit, your directory's customer ID. gcloud organizations list shows it.
What it creates
- Four constraints on one project: no service account keys, no public buckets, OS Login for SSH, and no external IPs on VMs
- Nine constraints on an organisation, folder, or project, in dry run until you switch it off (Standard)
- Optional limits on who can be granted access and where resources may be created (Standard)
The code
Quick startReady
project_id = "your-project-id"main.tf, line 20
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 destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: zero 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
StandardReady
parent. Where the constraints apply: organizations/<id>, folders/<id>, or projects/<id>. Everything underneath inherits them.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars parent = ""
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: zero 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
parent. Where the constraints apply: organizations/<id>, folders/<id>, or projects/<id>. Everything underneath inherits them.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars parent = ""
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: zero 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 gcloud iam service-accounts keys create against any service account. It fails with FAILED_PRECONDITION. In dry run, search Logs Explorer for the constraint's name to see what would have been refused.
Constraints apply to new actions only. Existing keys, public buckets, and external IPs stay until someone removes them.
compute.vmExternalIpAccess blocks new external IPs on every VM, including bastions and anything that reaches the internet directly. Give those Cloud NAT, or use the Standard dry run to find them first.
iam.allowedPolicyMemberDomains takes a customer ID such as C0abc123. gcloud organizations list shows yours.
How to undo it
terraform destroy removes the constraints, and the actions they blocked work again at once.
What it costs
Nothing. Organization policies are free.
Terraform. Security zones that refuse changes breaking their recipe
What you need first
- Cloud Guard enabled, such as by the compliance evaluation template.
- A compartment to protect, and permission to manage security zones and recipes.
- For Standard, an address for the alerts.
What it creates
- A security zone on one compartment using Oracle's Maximum Security Recipe
- A recipe of your own from nine exposure policies, such as no public buckets, subnets, databases, or load balancers (Standard)
- A zone on each compartment you name, covering its subcompartments (Standard)
- An email when anyone changes or removes a zone or the recipe (Standard)
See the one alert this creates
The code
Quick startReady
tenancy_ocid = "ocid1.tenancy.oc1..change-me"main.tf, line 24compartment_ocid = "ocid1.compartment.oc1..change-me"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-09 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. Security policies are looked up and the alert reads audit events here.region. Region identifier, such as us-ashburn-1. Use the Cloud Guard reporting region.compartments. Compartments to put in a security zone, by a short name of your choosing. Each covers its subcompartments too, and a compartment can be in one zone only.alert_email. Address told when a zone or its recipe is changed or removed. The address 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 = "" compartments = "" 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-09 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. Security policies are looked up and the alert reads audit events here.region. Region identifier, such as us-ashburn-1. Use the Cloud Guard reporting region.compartments. Compartments to put in a security zone, by a short name of your choosing. Each covers its subcompartments too, and a compartment can be in one zone only.alert_email. Address told when a zone or its recipe is changed or removed. The address 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 = "" compartments = "" 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-09 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
In the protected compartment, create a bucket with --public-access-type ObjectRead. It is refused, naming the security zone policy. Standard prints more checks with terraform output verify.
The Maximum Security Recipe also refuses internet gateways, NAT gateways, and volumes without a customer-managed key. Use it on a compartment built for sensitive workloads, never on a busy one first.
A compartment can be in one security zone only, and a zone covers every compartment below it.
Resources that broke a policy before the zone existed are not removed. Cloud Guard reports them as problems.
Standard finds Oracle's policies by name. If a name changes, plan stops and lists the names it could not find.
The alert email comes from an alarm on a metric, because a connector that emails directly may read only one compartment. It names the caller and the operation; Logging Search shows which zone.
Deleting the notification topic or the connector's policy stops the alarm, and so does moving the connector out of the tenancy root. Nothing reports it. The daily drift plan in Hardened is what notices.
How to undo it
terraform destroy removes the zones and the recipe, and the refused changes work again.
What it costs
Nothing. Security zones and Cloud Guard are free; Notifications email is free up to a monthly limit.
Good to know
- A guardrail is a deny that no permission inside the account can override. Keep the list to actions nobody should ever take, and handle everything else with ordinary permissions.
- Every refused call is still recorded in the audit log, with the policy that refused it. A burst of refusals is worth a detection of its own.