Course contentsShow
← Cloud and Infrastructure
Lesson 2895 of 2,98963. Infrastructure TestingFree lesson

Sentinel Policy Language

Writing Sentinel policies using its rule-based language to enforce Terraform configuration standards.

Sentinel Policy Language

You'll learn how to write rule-based Sentinel policies using its declarative language to enforce Terraform configuration standards before infrastructure deployment.

What is Sentinel Policy Language?

Sentinel Policy Language is HashiCorp's purpose-built, rule-based language for writing policies that evaluate against Terraform configurations, plans, and state. Unlike OPA's Rego (which you've already learned), Sentinel uses a more imperative syntax designed specifically for HashiCorp tooling.

Think of Sentinel as a security checkpoint guard who checks everyone's credentials against a rulebook. The guard doesn't make exceptions—the rules either pass or fail based on what they inspect.

Core Language Elements

Sentinel policies consist of rules that must evaluate to true or false. Each rule contains expressions that query Terraform data using built-in imports:

import "tfplan/v2" as tfplan

# Rule: All S3 buckets must have encryption enabled
main = rule {
  all tfplan.resource_changes as _, rc {
    rc.type is "aws_s3_bucket" implies
      rc.change.after.server_side_encryption_configuration is not null
  }
}

The main rule is mandatory—it determines overall policy success. You use quantifiers like all, any, and filtering with as to iterate resources. The implies operator creates conditional logic: "if this resource type exists, then this requirement must be met."

Policy Enforcement Levels

Sentinel supports three enforcement levels:

  • Advisory: Warning only, cannot block
  • Soft-mandatory: Can be overridden with permissions
  • Hard-mandatory: Cannot be overridden, absolute enforcement

These levels let you progressively adopt policies without breaking existing workflows.

Key Takeaway: Sentinel Policy Language uses rule-based expressions with quantifiers and imports to declaratively enforce Terraform standards, with flexible enforcement levels that balance governance with operational needs.