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

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.

Create Policy Setting form with the Enable calculated mode link beside the Setting label

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.)

Create Policy Setting form in calculated mode showing Input Query, Template, and the Launch calculated policy builder link

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.

Calculated Policy Builder Steps 1 and 2 with the Get bucket query returning the test bucket's tags

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.

Calculated Policy Builder Steps 3 and 4 with the template rendering Check: Enabled and a valid result

Template Output Rules

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.

Policy setting detail page showing the calculated GraphQL input query and Nunjucks template

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.

Values tab showing Check: Enabled computed for each GCP storage bucket in the project

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 template and template_input, do not set the value argument. They are mutually exclusive: a policy setting is either standard (with value) or calculated (with template and template_input).

Troubleshooting

IssueSolution
Template produces an error stateThe 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 dataVerify the query against a test resource in the builder. The resource may not have the property you're querying.
Unexpected policy values on some resourcesCheck 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 changesCalculated 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 assistanceOpen a Support Ticket

Next Steps