Skip to content
Cloud Governance Tools

Policy as code examples in five engines show one rule shape: select a resource, test one attribute, return a verdict

Five short policy as code examples, each copied from its project's own documentation: a Rego rule for Open Policy Agent, a Sentinel allow list, a Cloud Custodian YAML policy, a Kyverno ClusterPolicy and a CloudFormation Guard rule. Each is attributed with the project license, read 16 Nov 2026, with indentation removed.

See the ranking ยท All guides

Rego flags a security group whose description mentions HTTP

From the Open Policy Agent Terraform guide. The project is Apache License 2.0. The helper that collects resources from the plan is on the same page and omitted here. Lines, in order:

  • package terraform.module
  • deny contains msg if {
  • some r
  • desc := resources[r].values.description
  • contains(desc, "HTTP")
  • msg := sprintf("No security groups should be using HTTP. Resource in violation: %v", [r.address])
  • }

Sentinel checks an EC2 instance type against an allow list

From HashiCorp's page on defining Sentinel policies in HCP Terraform. The license for Sentinel: none found on 16 Nov 2026. The function validate_ec2_instance_types is defined elsewhere on that page.

  • allowed_types = [
  • "t2.small",
  • "t2.medium",
  • "t2.large",
  • ]
  • main = rule {
  • validate_ec2_instance_types(allowed_types)
  • }

Cloud Custodian removes cross-account access from S3 buckets

From the Cloud Custodian README. The project is Apache License 2.0. The description lines are omitted.

  • policies:
  • - name: s3-cross-account
  • resource: aws.s3
  • region: us-east-1
  • filters:
  • - type: cross-account
  • actions:
  • - type: remove-statements
  • statement_ids: matched

Kyverno blocks a Namespace that lacks a purpose label

From the Kyverno documentation on validate rules. The project is Apache License 2.0. Comments and the message line are omitted.

  • apiVersion: kyverno.io/v1
  • kind: ClusterPolicy
  • metadata:
  • name: require-ns-purpose-label
  • spec:
  • rules:
  • - name: require-ns-purpose-label
  • match:
  • any:
  • - resources:
  • kinds:
  • - Namespace
  • validate:
  • failureAction: Enforce
  • pattern:
  • metadata:
  • labels:
  • purpose: production

CloudFormation Guard requires an S3 bucket prefix, KMS encryption and a skip when none exist

From the CloudFormation Guard README. The project is Apache License 2.0.

  • let s3_buckets = Resources.*[ Type == /S3::Bucket/ ]
  • rule s3_bucket_name_encryption_check when %s3_buckets !empty {
  • %s3_buckets {
  • Properties {
  • BucketName == /^MyCompanyPrefix/
  • BucketEncryption.ServerSideEncryptionConfiguration[*] {
  • ServerSideEncryptionByDefault.SSEAlgorithm IN ["aws:KMS"]
  • }
  • }
  • }
  • }

Reuse terms are the project license, and documentation terms were not read separately

Each snippet above sits in the project's own documentation or README, and each repository carries the license stated. A separate license for the documentation site itself: none found on 16 Nov 2026 in the pages read. Treat the snippets as illustrations and check the project's terms before copying them into a product. The sample SCPs on the SCP mechanism page are different: they come from the aws-samples repository under MIT-0, which the library records for every entry.

The shape is the same in all five. A selector picks a resource: a security group, an instance, a bucket, a Namespace, a template section. A condition tests one attribute. A result says deny, remove, block or fail. The differences are in what the engine can see and when.

The five examples differ in what they read and what happens on a match

Only the Custodian example changes the cloud. The others report or block. For the same intents in the clouds' own objects, see Deny member accounts leaving the organization and the library, and for what each engine is, see what is policy as code.

EngineReadsOn a matchProject license
Open Policy AgentTerraform plan JSONReturns a deny message for the caller to act onApache 2.0
SentinelTerraform plan through importsPass or fail, at the enforcement level set in the productNone found
Cloud CustodianLive S3 bucketsRuns the action, here removing statementsApache 2.0
KyvernoA Kubernetes NamespaceBlocks the request while failureAction is EnforceApache 2.0
CloudFormation GuardA CloudFormation templateReports a failing ruleApache 2.0

Sources

  1. Open Policy Agent: Terraform (Apache 2.0 project), read 16 Nov 2026
  2. GitHub: OPA LICENSE, read 16 Nov 2026
  3. HashiCorp: define Sentinel policies in HCP Terraform, read 16 Nov 2026
  4. GitHub: Cloud Custodian README, read 16 Nov 2026
  5. GitHub: Cloud Custodian LICENSE, read 16 Nov 2026
  6. Kyverno: validate rules, read 16 Nov 2026
  7. GitHub: Kyverno LICENSE, read 16 Nov 2026
  8. GitHub: CloudFormation Guard README, read 16 Nov 2026
  9. GitHub: CloudFormation Guard LICENSE, read 16 Nov 2026

Last reviewed 16 Nov 2026.