This guide covers implementing Policy as Code in Terraform Cloud using Sentinel to enforce organizational policies for infrastructure compliance.
In this lab, we’ll implement Policy as Code in Terraform Cloud with Sentinel. Sentinel enforces organizational policies between the terraform plan and terraform apply stages, ensuring your infrastructure remains compliant before changes are applied.
Sentinel policies require the Teams & Governance tier in Terraform Cloud. In Settings → Plan & Billing, activate your free trial or subscription for this tier to unlock Policy as Code, cost estimation, and run tasks.
You must have an active Teams & Governance plan in Terraform Cloud before you can enforce Sentinel policies.
In Plan & Billing you’ll see your current plan:
The Teams & Governance tier includes all Team features plus Policy as Code and additional run capabilities:
Start by forking the HashiCorp Sentinel policy repository into your GitHub account. This gives you a local copy to customize and connect to Terraform Cloud.
This policy uses the tfplan-functions import to require that every AWS EC2 instance in the plan has a Name tag. The main rule fails if any instance is missing this tag.
Copy
Ask AI
import "tfplan-functions" as plan# Tags that must be present on every EC2 instancemandatory_tags = ["Name"]# Gather EC2 instances from the planallEC2Instances = plan.find_resources("aws_instance")# Identify instances missing required tagsviolatingEC2Instances = plan.filter_attribute_not_contains_list( allEC2Instances, "tags", mandatory_tags, true)# Fail the policy if any violations existmain = rule { length(violatingEC2Instances["messages"]) is 0}
This policy ensures only specific EC2 instance types (t2.micro, t2.small, t2.medium) are allowed. Any other type triggers a violation.
Copy
Ask AI
import "tfplan-functions" as plan# List of approved EC2 instance typesallowed_types = ["t2.micro", "t2.small", "t2.medium"]# Gather EC2 instances from the planallEC2Instances = plan.find_resources("aws_instance")# Find instances with disallowed typesviolatingEC2Instances = plan.filter_attribute_not_in_list( allEC2Instances, "instance_type", allowed_types, true)# Count how many violations we haveviolations = length(violatingEC2Instances["messages"])# Enforce zero violationsmain = rule { violations is 0}
Trigger a new run in your workspace. After the plan stage, you’ll see Policy check:
Policy results appear in the run output:
Copy
Ask AI
## Policy 1: AWS-Global-Policies/enforce-mandatory-tags (advisory)Result: trueDescription:This policy uses the Sentinel tfplan import to require that all EC2 instances have all mandatory tags.## Policy 2: AWS-Global-Policies/restrict-ec2-instance-type (hard-mandatory)Result: trueDescription:This policy uses the Sentinel tfplan/v2 import to require that all EC2 instances have instance types from an allowed list.
Configure your backend for Terraform Cloud and run terraform login.
Change the instance_type in terraform.auto.tfvars to an unapproved value (e.g., "m5.large").
Execute:
Copy
Ask AI
terraform initterraform plan
This triggers a remote run with policy checks. Violations will block the plan.
Copy
Ask AI
## Policy: AWS-Global-Policies/restrict-ec2-instance-type (hard-mandatory)Result: falseDescription:aws_instance.clumsy_bird has instance_type with value m5.large that is not in the allowed list: [t2.micro, t2.small, t2.medium]Error: Organization Policy Check hard failed.
git add terraform.auto.tfvarsgit commit -m "Set instance_type to t2.medium"git push
Terraform Cloud will automatically start a new run and, if compliant, proceed to apply:
Copy
Ask AI
## Policy 2: AWS-Global-Policies/restrict-ec2-instance-type (hard-mandatory)Result: trueDescription:This policy uses the Sentinel tfplan/v2 import to require that all EC2 instances have instance types from an allowed list.
In this guide, we covered how to store Sentinel policies as code, connect them to Terraform Cloud, and enforce them via both the UI and CLI. With Sentinel policies running automatically between plan and apply, you can enforce organizational standards for every infrastructure change.