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

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"
  })
}
ArgumentDescription
parentThe parent resource. Use tmod:@turbot/turbot#/ for the workspace root, or a folder ID.
titleThe file's display name.
descriptionOptional description of the file's purpose.
akasA list of unique aliases. Policies reference the file by this name.
contentThe file content as a JSON string. Use jsonencode() for readability.

[!TIP] Use jsonencode() to define the file content inline. For large files, use file() 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

IssueSolution
"AKA already in use" errorAnother resource already uses this AKA. Choose a different alias or import the existing resource with terraform import.
Provider authentication errorVerify your TURBOT_ACCESS_KEY_ID and TURBOT_SECRET_ACCESS_KEY environment variables are set correctly.
Invalid JSON in contentEnsure the content field is a valid JSON string. Use jsonencode() for inline data or validate your JSON file.
Further assistanceOpen a Support Ticket

Next Steps