Use a File in a Calculated Policy
In this guide, you will reference a Guardrails File in a calculated policy. This lets you centralize shared data in a file and write policies that dynamically adapt based on that data, without duplicating configuration across policies.
Prerequisites
- Access to the Guardrails console with
Turbot/AdminorTurbot/Ownerpermissions. - An existing Guardrails File. If you haven't created one, see Create a File via GraphQL or Create a File via Terraform.
How Calculated Policies Reference Files
A calculated policy has two parts:
- Template Input: A GraphQL query that fetches data. This is where you query the file by its AKA.
- Template: A Nunjucks template that produces the policy value using the queried data.
The key technique: in the template_input query, use resource(id: "file_aka") to fetch the file's data alongside the target resource's data.
Step 1: Understand the File Data
Assume you have a file with the AKA cidrs created via Terraform:
resource "turbot_file" "cidr_ranges" {
parent = "tmod:@turbot/turbot#/"
title = "CIDR Ranges"
description = "Org defined CIDR range list"
akas = ["cidrs"]
content = jsonencode({
cidrs = {
cidr_1 = "0.0.0.0/0"
cidr_2 = "10.0.0.0/8"
cidr_3 = "10.153.13.0/28"
cidr_4 = "192.168.1.1/32"
}
})
}
You want to reference this data in a security group ingress rules policy that approves traffic from specific CIDR ranges based on a resource tag.
Step 2: Write the Template Input
The template_input is a GraphQL query that runs against each target resource. Query both the target resource and the file in a single query:
{
securityGroup {
turbot {
tags
}
}
cidr_list: resource(id: "cidrs") {
data
}
}
| Query | Description |
|---|---|
securityGroup | The target resource being evaluated by this policy. |
resource(id: "cidrs") | Fetches the file by its AKA. The alias cidr_list: makes the data accessible in the template as $.cidr_list. |
Step 3: Write the Nunjucks Template
The template uses the queried data to produce the policy value. In this example, the policy checks the security group's cidr_range tag and approves the corresponding CIDR block from the file:
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_1' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_1 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_2' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_2 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_3' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_3 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_4' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_4 }}
{%- endif -%}
REJECT *
Key points:
- The policy checks the
cidr_rangetag on the security group to determine which CIDR block to approve. - The file is referenced by its AKA (
cidrs). - Policies can reference any file in the workspace.
- A single
template_inputquery can reference the target resource and multiple files.
Step 4: Test in the Calculated Policy Builder
Launch the Calculated Policy Builder from a policy setting editor: on the Create Policy Setting (or Update Policy Setting) form, click Enable calculated mode beside the Setting label, then click Launch calculated policy builder. (The builder is not a tab on the Developers page.)
Enter your template_input query and template to verify the output before deploying. The Output panel shows the queried file data, so you can confirm the resource(id: "cidrs") reference resolves before writing your template logic.

Step 5: Deploy with Terraform
Once tested, deploy the calculated policy as a policy setting. Here's a complete Terraform example:
resource "turbot_policy_setting" "security_group_ingress_rules_approved_rules" {
resource = turbot_policy_pack.security.id
type = "tmod:@turbot/aws-vpc-security#/policy/types/securityGroupIngressRulesApprovedRules"
template_input = <<-QUERY
{
securityGroup {
turbot {
tags
}
}
cidr_list: resource(id: "cidrs") {
data
}
}
QUERY
template = <<-EOT
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_1' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_1 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_2' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_2 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_3' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_3 }}
{%- endif -%}
{%- if $.securityGroup.turbot.tags['cidr_range'] == 'cidr_4' %}
APPROVE $.turbot.cidr:{{ $.cidr_list.data.cidrs.cidr_4 }}
{%- endif -%}
REJECT *
EOT
}
When you update the CIDR ranges file, Guardrails automatically re-evaluates all policies that reference it, no policy changes needed.
Troubleshooting
| Issue | Solution |
|---|---|
| File data is null in the template | Verify the file AKA matches exactly. AKAs are case-sensitive. |
| "Resource not found" in template_input | The file doesn't exist or the AKA is wrong. Create the file first. |
| Template output is empty | Check your Nunjucks syntax. Use the Calculated Policy Builder to debug. |
| Policy not re-evaluating after file update | Guardrails re-evaluates automatically, but it may take a few minutes. Check the control's activity log. |
| Further assistance | Open a Support Ticket |
Next Steps
- Learn more about Policies and how calculated policies work.
- Explore Guardrail Patterns for more examples of policy configurations.