Policy Packs as Code
In this guide, you will create and manage a policy pack using Terraform. Defining policy packs as code gives you version control, peer review through pull requests, and repeatable deployments across workspaces, all things that are difficult to achieve when configuring packs through the console alone.
The Guardrails Terraform provider includes three resources that map directly to the policy pack workflow:
| Terraform Resource | Purpose |
|---|---|
turbot_policy_pack | Creates the policy pack container with a title and description. |
turbot_policy_setting | Adds a policy setting to the pack (uses the pack's ID as the resource). |
turbot_policy_pack_attachment | Attaches the pack to a resource in the hierarchy. |
Prerequisites
- Terraform v1.0 or later installed locally.
- A Guardrails API access key pair. See Reference > CLI > Installation for setup instructions.
- Familiarity with the Guardrails workspace resource hierarchy (workspace root, folders, accounts).
Step 1: Set Up the Provider
Create a providers.tf file to configure the Guardrails Terraform provider. The provider needs your workspace URL and API credentials.
# providers.tf
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.0"
}
}
}
provider "turbot" {
# Set these via environment variables or terraform.tfvars:
# TURBOT_WORKSPACE = "https://your-workspace.cloud.turbot.com/api/v5"
# TURBOT_ACCESS_KEY = "your-access-key-id"
# TURBOT_SECRET_KEY = "your-secret-access-key"
}
Set your credentials as environment variables to keep them out of version control:
export TURBOT_WORKSPACE="https://your-workspace.cloud.turbot.com/api/v5"
export TURBOT_ACCESS_KEY="your-access-key-id"
export TURBOT_SECRET_KEY="your-secret-access-key"
Step 2: Create the Policy Pack
Create a main.tf file with the policy pack resource. The title and description match what you'd enter in the console's Create Policy Pack dialog.
# main.tf
resource "turbot_policy_pack" "s3_versioning" {
title = "GFB - Enforce S3 Bucket Versioning"
description = "Enforces versioning on all S3 buckets in the Retail Banking nonprod account to meet GFB data retention requirements."
}
Step 3: Add Policy Settings
Create a policies.tf file to define the policy settings for the pack. The key difference from a regular turbot_policy_setting is the resource argument. Instead of pointing to an account or folder, it references the policy pack's ID.
# policies.tf
resource "turbot_policy_setting" "s3_bucket_versioning" {
resource = turbot_policy_pack.s3_versioning.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketVersioning"
value = "Enforce: Enabled"
}
The type field is the Policy Type URI, a unique identifier for every policy type in Guardrails. You can find it in the console by navigating to a policy setting's detail page, or by browsing policy types in the Turbot Hub.
You can add as many settings as needed to a single pack:
resource "turbot_policy_setting" "s3_bucket_versioning" {
resource = turbot_policy_pack.s3_versioning.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketVersioning"
value = "Enforce: Enabled"
}
resource "turbot_policy_setting" "s3_bucket_encryption" {
resource = turbot_policy_pack.s3_versioning.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketEncryptionAtRest"
value = "Enforce: AWS SSE or higher"
}
Step 4: Attach the Pack to a Resource
Add an attachment to apply the pack to a specific resource. The resource is the Guardrails resource ID (AKA) of the target, an account, folder, organization, or the workspace root.
# main.tf (continued)
resource "turbot_policy_pack_attachment" "s3_versioning_nonprod" {
resource = "148221445497" # AWS account ID
policy_pack = turbot_policy_pack.s3_versioning.id
}
You can attach the same pack to multiple resources:
resource "turbot_policy_pack_attachment" "s3_versioning_nonprod" {
resource = "148221445497"
policy_pack = turbot_policy_pack.s3_versioning.id
}
resource "turbot_policy_pack_attachment" "s3_versioning_staging" {
resource = "295837104582"
policy_pack = turbot_policy_pack.s3_versioning.id
}
Step 5: Apply the Configuration
Initialize the Terraform working directory and apply the configuration:
terraform init
terraform plan
terraform apply
Review the plan output carefully before confirming. Terraform will create the policy pack, add the settings, and attach it to the specified resources.
Full Working Example
Here's the complete three-file structure for the GFB S3 Versioning example:
# providers.tf
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.0"
}
}
}
provider "turbot" {}
# main.tf
resource "turbot_policy_pack" "s3_versioning" {
title = "GFB - Enforce S3 Bucket Versioning"
description = "Enforces versioning on all S3 buckets in the Retail Banking nonprod account to meet GFB data retention requirements."
}
resource "turbot_policy_pack_attachment" "s3_versioning_nonprod" {
resource = "148221445497"
policy_pack = turbot_policy_pack.s3_versioning.id
}
# policies.tf
resource "turbot_policy_setting" "s3_bucket_versioning" {
resource = turbot_policy_pack.s3_versioning.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketVersioning"
value = "Enforce: Enabled"
}
Finding Policy Type URIs
Every policy type in Guardrails has a unique URI that you need for the type field in turbot_policy_setting. There are two ways to find it:
- In the console: Navigate to a policy setting's detail view. The Policy Type URI is shown in the setting metadata.
- In the Guardrails Hub: Browse to the mod and policy type in the Guardrails Hub. The URI is listed on the policy type's page.
URIs follow the pattern tmod:@turbot/{mod-name}#/policy/types/{policyType}. For example:
tmod:@turbot/aws-s3#/policy/types/bucketVersioningtmod:@turbot/aws-ec2#/policy/types/instanceApprovedtmod:@turbot/azure-storage#/policy/types/storageAccountHttpsOnly
Community Policy Packs
The Guardrails community maintains a library of ready-to-use policy packs in the guardrails-samples repository. These cover common compliance frameworks and best practices across AWS, Azure, and GCP.
You can use these samples as-is, fork them for your organization, or contribute your own packs back to the community. Each sample follows the same three-file Terraform structure described in this guide.
Troubleshooting
| Issue | Solution |
|---|---|
Error: Invalid resource ID on attachment | Verify the resource ID is the Guardrails AKA or numeric ID, not the cloud provider's ARN or resource path. You can find the Guardrails resource ID in the console on the resource's detail page. |
Error: Policy type not found | Confirm the mod that defines the policy type is installed in your workspace. The Policy Type URI must exactly match the format shown in the Hub. |
Plan shows destroy for existing pack | If you imported an existing pack into Terraform state, ensure the title matches exactly. Terraform tracks resources by state, not by name. |
| Credentials not working | Verify your environment variables are set correctly. The workspace URL must end with /api/v5. |
| Further assistance | Open a Support Ticket |
Next Steps
- guardrails-samples repository, Browse community policy packs for common compliance frameworks.
- Create a Policy Pack, Create packs through the console when you need a quick, one-off configuration.
- Terraform Provider Reference, Full documentation for the Guardrails Terraform provider.