references/verified-syntax.md · 96000
ingress." } enforce { condition = local.has_required_tags error_message = "Security group must have required tags." } }
❌ Wrong — same resource type split across multiple resource_policy blocks
resource_policy "aws_security_group" "ingress_check" { # Check ingress rules only } resource_policy "aws_security_group" "egress_check" { # Check egress rules only }
**Benefits:**
- All checks on the same resource type are co-located and consistent
- Each `enforce` block reports independently — user sees all failures at once
- Easier to maintain and test
---
---
### ✅ All Errors Shown Pattern
**Rule:** Multiple enforce blocks show all failures, not just first
```hcl
resource_policy "aws_security_group" "comprehensive_check" {
enforce {
condition = !local.has_public_ssh
error_message = "SSH violation: ..."
}
enforce {
condition = !local.has_public_rdp
error_message = "RDP violation: ..."
}
enforce {
condition = local.has_description
error_message = "Description required: ..."
}
}
Behavior: User sees all failing messages in encounter order
Benefit: Comprehensive feedback - users can fix all issues at once
Verified Capabilities by Policy Type
resource_policy
- ✅ Full
attrs.*access, nested attributes via dot notation - ✅
meta.provider_type,meta.tfe_workspace.tags - ✅
meta.tfe_stack.deployment_name,meta.tfe_stack.stack_name,meta.tfe_stack.deployment_group; fields are empty strings outside Stack evaluations on 0.3.x/0.4.x andnullon 0.5.0+ - ❌
meta.addressis UNDEFINED in real plan evaluation — do not use infilter,locals,condition, orerror_message; it causesError: Unsupported attributeat runtime. Note:tfpolicy testwill NOT catch this error — onlyterraform plan --policies=will. - ✅
filter,locals, multipleenforceblocks
module_policy
- ✅
meta.source,meta.version(nullable on 0.5.0+),meta.address - ❌
prior_attrsis a parse error (0.5.0+); remove the reference - ✅
filter,locals, multipleenforceblocks - ❌
attrs.*(inputs) - work in progress - ✅
meta.tfe_stack.*,meta.tfe_workspace.tags; Stack fields are empty strings outside Stack evaluations on 0.3.x/0.4.x andnullon 0.5.0+
provider_policy
- ✅ Full
attrs.*(config),meta.aliasandmeta.version(bothnullwhen absent on 0.5.0+; guard function and string-template uses with a conditional),meta.source - ❌
prior_attrsis a parse error (0.5.0+); useattrs - ✅
filter,locals, multipleenforceblocks - ✅
meta.tfe_stack.*,meta.tfe_workspace.tags; Stack fields are empty strings outside Stack evaluations on 0.3.x/0.4.x andnullon 0.5.0+
⚠️ meta.version is the resolved version (e.g. "6.50.0"), not the constraint string (e.g. ">= 4.0"). Use core::semverconstraint(meta.version, "~> 5.0") to enforce an approved range. Test mocks should use realistic resolved version numbers, not constraint strings. Verified on tfpolicy 0.0.2-beta20260513.
Targeting Pattern:
| Policy Type | First Label | Example |
|-------------|-------------|---------|
| resource_policy | Resource TYPE | "aws_instance" |
| module_policy | Full SOURCE path | "app.terraform.io/myorg/vpc/aws" |
| provider_policy | Provider TYPE | "aws" (not "hashicorp/aws") |
Testing
See the tfpolicy-test skill for comprehensive testing guidance.
Module mock syntax (two labels required)
module_policy test mocks take two labels — source and a mock name — matching the policy block's own two-label signature:
# ✅ CORRECT — two labels: source pattern, then mock name
module "registry.terraform.io/hashicorp/consul/aws" "approved" {
meta = {
source = "registry.terraform.io/hashicorp/consul/aws"
version = "0.1.0"
address = "module.consul"
}
}
# ❌ WRONG — one label causes parse error
module "registry.terraform.io/hashicorp/consul/aws" {
...
}
The same applies to the policy block itself:
# ✅ CORRECT
module_policy "*" "require_private_registry" { ... }
# ❌ WRONG — "Only 1 labels (source) are expected" error (misleading message; two ARE required)
module_policy "*" { ... }
Verified on tfpolicy 0.0.2-beta20260513.
Quick Decision Tree
Need to compare versions?
→ SemVer strings (e.g. "6.2.0"): Use core::semverconstraint(). Non-SemVer strings (e.g. AWS "6.x", "5.0.6"): Use core::try(core::regex("^[1-5]\\.", version), null) != null — see Mistake 36.
Checking if a value is one of several allowed values?
→ Use core::contains(allowed_list, value) — NOT chained ||. Store the list in a named local — see Mistake 35.
Enforcing a rule on an S3 companion resource (e.g. public_access_block, acl, versioning, logging)?
→ ALWAYS anchor on resource_policy "aws_s3_bucket" with core::getresources("companion", { bucket = attrs.id }) inside — NEVER anchor on the companion type directly — see Mistake 37.
Enforcing IAM content rules (no admin privileges, no wildcard actions, etc.)?
→ Write 4 separate resource_policy blocks: aws_iam_policy, aws_iam_role_policy, aws_iam_user_policy, aws_iam_group_policy. All use core::jsondecode(core::try(attrs.policy, "{}")). Do NOT use aws_iam_policy_document (data_policy does not exist) — see Mistake 38.
Need built-in Terraform function?
→ Add core:: prefix
Want to use locals in provider_policy? → Go ahead! Language server errors are false
Need string pattern matching?
→ ✅ core::startswith(str, prefix) and core::endswith(str, suffix) are available directly. For regex/substring use core::try(core::regex("pattern", string), null) != null.
Need to handle null values?
→ Use core::try(value, default)
Testing policies?
→ Create .policytest.hcl files and run tfpolicy test
Version Requirements
- Terraform: >= 1.13.0-policyYYYYMMDD (private beta builds)
- tfpolicy CLI: >= 0.0.1-alphaYYYYMMDD
- HCP Terraform: Organization with policy feature enabled
Contact
- Questions: [email protected]
- Documentation: See terraform-policy-agent-skill/ directory
- Examples: See the reusable patterns in
tfpolicy-author.mdand any companion example directories that may exist in your broader beta workspace
Status: ✅ All behaviors verified with user Ready for: Agent skill usage, documentation generation, policy creation Last Review: 2026-02-20
See Also: Authoring Reference