For the complete documentation index, see llms.txt. This page is also available as Markdown.

Custom Build Policy Examples

To create code-based policies for your infrastructure, use these examples as guidance. They showcase how to use different attributes for working with IaC templates.

Bicep Example

The following example shows how to create a Bicep policy that ensures auditing is enabled for Microsoft SQL servers and their databases. It filters resources to include only Microsoft.Sql/servers and Microsoft.Sql/servers/databases, then verifies that auditing settings are configured and enabled for both resource types.

metadata:
  name: "Ensure that auditing is set to on for SQL servers"
  guidelines: "..."
  category: "logging"
definition:
  and:
    - cond_type: filter
      attribute: resource_type
      operator: within
      value:
        - Microsoft.Sql/servers
        - Microsoft.Sql/servers/databases
    - or:
        - and:
            - cond_type: connection
              resource_types:
                - Microsoft.Sql/servers
              connected_resource_types:
                - Microsoft.Sql/servers/auditingSettings
              operator: exists
            - cond_type: attribute
              resource_types:
                - Microsoft.Sql/servers/auditingSettings
              attribute: properties.state
              operator: equals
              value: Enabled
        - and:
            - cond_type: connection
              resource_types:
                - Microsoft.Sql/servers/databases
              connected_resource_types:
                - Microsoft.Sql/servers/databases/auditingSettings
              operator: exists
            - cond_type: attribute
              resource_types:
                - Microsoft.Sql/servers/databases/auditingSettings
              attribute: properties.state
              operator: equals
              value: Enabled

ARM Example

Terraform Examples

If you are creating policies for Terraform, here are some examples you can use to create a custom build policy.

Basic Policy Definition

In the following example, the attribute is tags.env, where tags is the attribute, and env is the key name you are searching for in a key:value pair of tags. To ensure that every resource has a Department:<something> tag, modify and set the attribute to tags.Department. Note that this is case-sensitive. Use the equals_ignore_case operator for a case-insensitive check.

Policy Definition using AND Attribute

Use the sample guideline to create a two policy definitions using attribute AND.

Policy Definition using AND/OR Logic Attribute

Use the sample guideline to create a two policy definitions using attribute AND/OR Logic.

Policy Definition using OR Attribute

Use the sample guideline to create multiple policy definition using OR.

Connection State

A Connection State Block indicates a type of resource that has or does not have a connection to another type of resource. Use the sample guideline to create a connection state with attributes and filter.

If your connection state is complex using filter and attribute you can use the following sample guidelines.

Array

In addition to creating policies using multiple attributes, you can create a policy to check multiple entries, of the same type, within an array.

For this sample, you want to scan all the Ingress CIDR blocks for this resource to determine if any = 0.0.0.0/0.

CloudFormation Examples

If you are creating policies for CloudFormation, here are some examples you can use to create a custom build policy code.

Basic Policy Definition

Use the sample guideline to create a basic policy definition using one attribute.

Policy Definition using OR Attribute

Use the sample guideline to create multiple policy definition using OR.

Connection State

A Connection State Block indicates a type of resource that has or does not have a connection to another type of resource. Use the sample guideline to create a connection state with attributes and filter.

Kubernetes Examples

If you are creating policies for Kubernetes, here are some examples you can use to create a custom build policy code.

Basic Policy Definition

Use the sample guideline to create a basic policy definition using one attribute.

Connection State

A Connection State Block indicates a type of resource that has or does not have a connection to another type of resource. Use the sample guideline to create a connection state with attributes and filter.

Secrets Security Examples

The following examples demonstrate how to create a custom build policy code.

Example #1 Basic Policy Definition

Use the sample guideline to create a basic policy definition using a single attribute.

Example #2 Detect Passwords

This policy detects instances where literal strings starting with "password" or "pwd" are used directly in the code or configuration files. This is a security risk because it exposes the actual password value.

Resource Control - Blocklist Example

Define granular resource controls to allow or block specific resource types. The following example demonstrates how to block the creation of aws_sagemaker_model, to ensure that certain resources are reviewed before deployment.

Last updated

Was this helpful?