Create a File via Terraform
In this guide, you will create a Guardrails File using the Guardrails Terraform provider. Managing files as infrastructure-as-code gives you version control, change tracking, and repeatable deployments.
Prerequisites
- Terraform installed (v1.0+).
- The Guardrails Terraform provider configured with your workspace credentials.
Step 1: Define the File Resource
The turbot_file resource creates a Guardrails File. Define the file in your Terraform configuration:
resource "turbot_file" "approved_cidrs" {
parent = "tmod:@turbot/turbot#/"
title = "Approved CIDR Ranges"
description = "Organization-approved CIDR ranges for security group validation"
akas = ["approved_cidrs"]
content = jsonencode({
corporate_vpn = "10.0.0.0/8"
office_wifi = "192.168.1.0/24"
datacenter = "172.16.0.0/12"
monitoring = "10.153.13.0/28"
public_internet = "0.0.0.0/0"
})
}
| Argument | Description |
|---|---|
| parent | The parent resource. Use tmod:@turbot/turbot#/ for the workspace root, or a folder ID. |
| title | The file's display name. |
| description | Optional description of the file's purpose. |
| akas | A list of unique aliases. Policies reference the file by this name. |
| content | The file content as a JSON string. Use jsonencode() for readability. |
[!TIP] Use
jsonencode()to define the file content inline. For large files, usefile()to read content from a separate JSON file:content = file("${path.module}/data/cidrs.json").
Step 2: Apply the Configuration
Run Terraform to create the file:
terraform init
terraform plan
terraform apply
Terraform shows the planned changes before applying. Review the output to confirm the file will be created with the correct properties.
Step 3: Verify
After applying, verify the file exists by querying it in the Guardrails console. Navigate to Developers > GraphQL Explorer and run:
{
resource(id: "approved_cidrs") {
data
turbot {
title
id
}
}
}
Examples
File with Data from a JSON File
Store the file content in a separate JSON file for easier maintenance:
data/environment-map.json:
{
"accounts": {
"123456789012": { "environment": "production", "team": "payments" },
"234567890123": { "environment": "staging", "team": "payments" },
"345678901234": { "environment": "development", "team": "fintech" }
}
}
main.tf:
resource "turbot_file" "environment_map" {
parent = "tmod:@turbot/turbot#/"
title = "Environment Map"
description = "Maps AWS account IDs to environment and team metadata"
akas = ["environment_map"]
content = file("${path.module}/data/environment-map.json")
}
File Under a Folder
Create a file as a child of a specific folder instead of the workspace root:
resource "turbot_folder" "config" {
parent = "tmod:@turbot/turbot#/"
title = "Configuration Files"
description = "Shared configuration data for calculated policies"
}
resource "turbot_file" "golden_amis" {
parent = turbot_folder.config.id
title = "Golden AMIs"
description = "Approved AMI list by region"
akas = ["golden_amis"]
content = jsonencode({
"us-east-1" = ["ami-0abcdef1234567890", "ami-0fedcba0987654321"]
"us-west-2" = ["ami-0123456789abcdef0"]
})
}
Troubleshooting
| Issue | Solution |
|---|---|
| "AKA already in use" error | Another resource already uses this AKA. Choose a different alias or import the existing resource with terraform import. |
| Provider authentication error | Verify your TURBOT_ACCESS_KEY_ID and TURBOT_SECRET_ACCESS_KEY environment variables are set correctly. |
| Invalid JSON in content | Ensure the content field is a valid JSON string. Use jsonencode() for inline data or validate your JSON file. |
| Further assistance | Open a Support Ticket |
Next Steps
- Use a File in a Calculated Policy: Reference this file in policy logic.
- Learn more about the Guardrails Terraform provider.