Policy as code examples in five engines show one rule shape: select a resource, test one attribute, return a verdict
Facts read 16 Nov 2026. Page reviewed 16 Nov 2026.
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.
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.moduledeny contains msg if {some rdesc := resources[r].values.descriptioncontains(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-accountresource: aws.s3region: us-east-1filters:- type: cross-accountactions:- type: remove-statementsstatement_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/v1kind: ClusterPolicymetadata:name: require-ns-purpose-labelspec:rules:- name: require-ns-purpose-labelmatch:any:- resources:kinds:- Namespacevalidate:failureAction: Enforcepattern: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.
| Engine | Reads | On a match | Project license |
|---|---|---|---|
| Open Policy Agent | Terraform plan JSON | Returns a deny message for the caller to act on | Apache 2.0 |
| Sentinel | Terraform plan through imports | Pass or fail, at the enforcement level set in the product | None found |
| Cloud Custodian | Live S3 buckets | Runs the action, here removing statements | Apache 2.0 |
| Kyverno | A Kubernetes Namespace | Blocks the request while failureAction is Enforce | Apache 2.0 |
| CloudFormation Guard | A CloudFormation template | Reports a failing rule | Apache 2.0 |
Related pages
Sources
- Open Policy Agent: Terraform (Apache 2.0 project), read 16 Nov 2026
- GitHub: OPA LICENSE, read 16 Nov 2026
- HashiCorp: define Sentinel policies in HCP Terraform, read 16 Nov 2026
- GitHub: Cloud Custodian README, read 16 Nov 2026
- GitHub: Cloud Custodian LICENSE, read 16 Nov 2026
- Kyverno: validate rules, read 16 Nov 2026
- GitHub: Kyverno LICENSE, read 16 Nov 2026
- GitHub: CloudFormation Guard README, read 16 Nov 2026
- GitHub: CloudFormation Guard LICENSE, read 16 Nov 2026
Last reviewed 16 Nov 2026.