Create a Calculated Policy
In this guide, you will create a calculated policy setting that dynamically computes its value based on resource attributes. Unlike a standard policy setting that uses a fixed value, a calculated policy uses a GraphQL query to read resource data and a Nunjucks template to produce the final value. This is useful when different resources within the same scope need different treatment based on their tags, configuration, or other properties.
Prerequisites
- Access to the Guardrails console with
Turbot/Adminpermissions or higher. - The relevant mod installed for the policy type you want to configure.
- Familiarity with GraphQL and Nunjucks templates.
Step 1: Start a New Policy Setting
Select Policies in the left sidebar, then click New Policy Setting. On the Create Policy Setting form, pick the Policy Type and Resource exactly as you would for a standard setting.
For this example, choose GCP > Storage > Bucket > Versioning and target the goliathfederalbank GCP project, so the calculated value applies to every bucket in the project.
Step 2: Enable Calculated Mode
Beside the Setting label, click Enable calculated mode.

The fixed-value options are replaced by Input Query and Template fields and a Launch calculated policy builder link. (The link beside Setting now reads Disable calculated mode so you can switch back to a fixed value.)

Step 3: Launch the Calculated Policy Builder
Click Launch calculated policy builder. The builder opens in the same tab at /developers/calculated-policy-builder and returns you to this form when you're done.
[!NOTE] The builder is not a Developers sidebar tab. It always launches from the policy setting editor, carrying your policy type and target resource with it.
Step 4: Choose a Test Resource
The builder opens with the policy type pre-filled. In Step 1, pick a Test Resource: a single resource of the target type to preview your query and template against in real time.
For this example, choose a GCP storage bucket such as goliath-bucket-3-goliathfederalbank.
Step 5: Write the Input Query
In Step 2, write a GraphQL query (optionally with Nunjucks) that fetches the data your template needs. The Select snippet dropdown offers starting points such as Get bucket. The most common pattern is to read the resource's tags:
{
bucket {
turbot {
tags
}
}
}
The Output [JSON] panel shows the data returned for your test resource, which the template reads as $.bucket.turbot.tags. Chain additional queries with Add input query when one result feeds the next.

Step 6: Write the Template
In Step 3, write a Jinja2/Nunjucks template that reads the query output and returns one of the policy type's valid values (for example Skip or Check: Enabled). The If / Else If / Else snippet is a good starting point.
This template enables versioning checks on buckets provisioned by Terraform (tagged managed_by: terraform) and skips everything else:
{% if $.bucket.turbot.tags.managed_by == "terraform" -%}
'Check: Enabled'
{%- else -%}
Skip
{%- endif %}
The Output [YAML] panel shows the rendered value, and Step 4 validates it against the policy type schema in the Result [JSON] panel. Swap the tag and values to match your own governance model, such as branching on an environment or data-classification tag.

Template Output Rules
- Whitespace around the output is trimmed, so use the
{%-and-%}syntax to strip extra newlines. - Values that contain a colon (such as
Check: Enabled) must be quoted, or YAML parses them as an object. - If the template produces an empty string or an invalid value, the control enters an error state.
Step 7: Save and Create
Click Update to return to the policy setting form with your query and template populated. Set the Precedence (Required or Recommended), add an optional Note, then click Create.
Step 8: Review the Calculated Setting
The setting's detail page marks it as calculated: the Setting tab shows the GraphQL Input Query and Nunjucks Template instead of a fixed value, with Edit and Delete buttons in the header.

Step 9: Review the Computed Values
Click the Values tab to see the value computed for each resource in scope. Each bucket is evaluated independently against your template, so you can confirm the policy resolves the way you intended. In this example, every Terraform-managed bucket resolves to Check: Enabled.

Via Terraform
Calculated policies are well-suited for Terraform, especially when managing them across many accounts. Use the template and template_input arguments instead of value:
resource "turbot_policy_setting" "gcs_bucket_versioning_calculated" {
resource = "gcp://cloudresourcemanager.googleapis.com/projects/goliathfederalbank"
type = "tmod:@turbot/gcp-storage#/policy/types/bucketVersioning"
template_input = <<-EOT
{
bucket {
turbot {
tags
}
}
}
EOT
template = <<-EOT
{% if $.bucket.turbot.tags.managed_by == "terraform" -%}
'Check: Enabled'
{%- else -%}
Skip
{%- endif %}
EOT
precedence = "required"
note = "Enable versioning checks on Terraform-managed buckets"
}
[!NOTE] When using
templateandtemplate_input, do not set thevalueargument. They are mutually exclusive: a policy setting is either standard (withvalue) or calculated (withtemplateandtemplate_input).
Troubleshooting
| Issue | Solution |
|---|---|
| Template produces an error state | The template output doesn't match a valid policy value. Check for typos: values are case-sensitive (e.g., Check: Enabled, not check: enabled) and colon values must be quoted. |
| GraphQL query returns empty data | Verify the query against a test resource in the builder. The resource may not have the property you're querying. |
| Unexpected policy values on some resources | Check the Values tab on the policy setting to see per-resource output. A resource may have unexpected tag values or missing properties causing the template to take the wrong branch. |
| Template not re-evaluating after resource changes | Calculated policies re-evaluate when the queried resource data changes. If the data source hasn't updated, trigger a re-evaluation from the control's Actions menu. |
| Further assistance | Open a Support Ticket |
Next Steps
- Calculated Policy Tester: Test and iterate on your queries and templates.
- Nunjucks Reference: Full reference for Nunjucks template syntax and filters available in Guardrails.
- Create an Exception: Override a policy for specific accounts that need different treatment.