Calculated Policy Builder

The Calculated Policy Builder is an interactive tool for creating and testing calculated policies directly in the Guardrails console. Calculated policies derive policy values dynamically from live resource data using GraphQL queries and Nunjucks templates, rather than setting static values.

You open the builder from the policy setting editor, not from the sidebar. On the Policies page, click New Policy Setting (or edit an existing setting), turn on Enable calculated mode, then click Launch calculated policy builder. The builder opens in the same tab, pre-loaded with the policy type and resource from your setting, and returns you to the editor when you are done.

Calculated Policy Builder showing the four-step builder with policy type selection, resource picker, query editor, and template editor

The Four-Step Workflow

The builder guides you through creating a calculated policy in four steps:

Step 1: Select a Policy Type

Choose which policy type you want to compute dynamically. For example, select AWS > S3 > Bucket > Versioning to build a calculated policy that determines the versioning setting based on live resource data.

Step 2: Pick a Test Resource

Select a specific resource to test your queries and template against. The builder will use this resource's data to show live results as you develop the policy. Choose a representative resource so your test output reflects real conditions.

Step 3: Write GraphQL Queries

Write a GraphQL query to fetch the data your template needs. The query runs against the test resource you selected, and results appear immediately so you can verify you are fetching the right fields.

For example, to fetch an S3 bucket's tags:

{
  resource {
    tags
  }
}

Step 4: Create a Template

Write a Nunjucks template that transforms the query results into a policy value. The builder evaluates the template in real time against the query output, showing you exactly what the calculated policy will produce.

For example, to set versioning based on whether a bucket has a specific tag:

{%- if $.resource.tags.Environment == "Production" -%}
Enabled
{%- else -%}
Disabled
{%- endif -%}

Tips