Create a File via GraphQL
In this guide, you will create a Guardrails File programmatically using the createResource GraphQL mutation. This approach is ideal for automation workflows, triggered by CI/CD pipelines, Lambda functions, or scripts that pull data from external systems.
Prerequisites
- A Guardrails access key with
Turbot/Adminpermissions. - A GraphQL client (e.g.,
curl, the Guardrails CLI, or any HTTP library).
Step 1: Write the Mutation
The createResource mutation creates any Guardrails resource, including files. Set the type field to the file resource type URI.
mutation CreateFile($input: CreateResourceInput!) {
createResource(input: $input) {
data
metadata
turbot {
id
akas
}
trunk {
title
}
}
}
Step 2: Define the Variables
The variables specify the file's parent, type, alias, content, and metadata.
{
"input": {
"parent": "tmod:@turbot/turbot#/",
"type": "tmod:@turbot/turbot#/resource/types/file",
"akas": ["environment_map"],
"data": {
"accounts": {
"123456789012": { "environment": "production", "team": "payments" },
"234567890123": { "environment": "staging", "team": "payments" },
"345678901234": { "environment": "development", "team": "fintech" }
}
},
"metadata": {
"title": "Environment Map",
"description": "Maps AWS account IDs to environment and team metadata"
}
}
}
| Field | Description |
|---|---|
| parent | The parent resource ID or URI. Use tmod:@turbot/turbot#/ for the workspace root, or a folder's numeric ID or AKA. |
| type | Must be tmod:@turbot/turbot#/resource/types/file. |
| akas | An array of unique aliases. Policies reference the file by this name. |
| data | Any valid JSON object. This is the file's content. |
| metadata.title | The file's display name. |
| metadata.description | Optional description. |
Step 3: Execute with curl
curl -s -X POST https://YOUR_WORKSPACE.cloud.turbot.com/api/latest/graphql \
-H "Authorization: Basic $(echo -n 'ACCESS_KEY_ID:SECRET_ACCESS_KEY' | base64)" \
-H "Content-Type: application/json" \
-d '{
"query": "mutation CreateFile($input: CreateResourceInput!) { createResource(input: $input) { data turbot { id akas } } }",
"variables": {
"input": {
"parent": "tmod:@turbot/turbot#/",
"type": "tmod:@turbot/turbot#/resource/types/file",
"akas": ["environment_map"],
"data": {
"accounts": {
"123456789012": { "environment": "production", "team": "payments" }
}
},
"metadata": {
"title": "Environment Map",
"description": "Maps AWS account IDs to environment and team metadata"
}
}
}
}'
Replace YOUR_WORKSPACE, ACCESS_KEY_ID, and SECRET_ACCESS_KEY with your workspace URL and credentials.
Update an Existing File
To update a file's data, use the updateResource mutation with the file's ID or AKA:
mutation UpdateFile($input: UpdateResourceInput!) {
updateResource(input: $input) {
data
turbot {
id
}
}
}
{
"input": {
"id": "environment_map",
"data": {
"accounts": {
"123456789012": { "environment": "production", "team": "payments" },
"234567890123": { "environment": "staging", "team": "payments" },
"345678901234": { "environment": "development", "team": "fintech" },
"456789012345": { "environment": "production", "team": "risk-analytics" }
}
}
}
}
When you update a file, Guardrails automatically re-evaluates all calculated policies that reference it.
Troubleshooting
| Issue | Solution |
|---|---|
| 401 Unauthorized | Verify your access key and secret are correct. Check the key has Turbot/Admin permissions. |
| "AKA already exists" | Use updateResource instead of createResource to modify an existing file. |
| "Parent not found" | Verify the parent resource ID exists. Use tmod:@turbot/turbot#/ for the workspace root. |
| Further assistance | Open a Support Ticket |
Next Steps
- Use a File in a Calculated Policy: Reference this file in policy logic.
- Create a File via Terraform: Manage files declaratively with Terraform.