- Terraform Policy
- v0.5.x (latest)
- v0.4.x (beta)
- v0.3.x (beta)
- v0.2.x (beta)
- v0.1.x (beta)
policy
The policy block configures how Terraform policy evaluates your policies, including which version of Terraform to use to perform the evaluation. You must include a policy block with a required_providers nested block in any policy files that contain resource or provider policies.
Configuration model
The policy block supports the following configuration:
policyblockpluginsblock- <plugin_label> map
sourcestring | required
- <plugin_label> map
required_providersblock- <provider_label> map
terraform_configblockrequired_versionstring
Complete configuration example
The following example demonstrates a policy block with all configuration options specified:
policies/example.policy.hcl
policy {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 6.0.0, < 7.0.0"
}
}
terraform_config {
required_version = ">= 1.16.0"
}
plugins {
example = {
source = "../plugins/bin/example"
}
custom_validator = {
source = "../plugins/bin/custom-validator"
}
}
}
Specification
A policy block supports the following configuration.
plugins
Specifies plugins that provide custom functions to use in your policies. Plugins must be compiled binaries. Refer to Policy plugins for more information.
Specify each plugin as a map within the plugins block.
- Data type: Block
- Default: None
- Example: Configure multiple plugins
A plugins block supports the following configuration.
Plugin map label
The plugin block label gives a name to the plugin. Must be unique within the policy file.
Plugin map key source
The source attribute is the path to the plugin binary. The binary must be a valid Terraform policy Go plugin server. Refer to Policy plugins for more information about creating and using policy plugins.
- Data type: String
- Required
In your policies, reference your functions with the syntax plugin::<plugin_label>::<function_name>(<function_arguments>...).
required_providers
Specifies provider requirements for schema validation during policy evaluation. Required for policy files that contain resource or provider policy blocks. When you run tfpolicy validate or tfpolicy test, Terraform policy validates your policy configuration against the specified provider schemas.
Specify each provider as a named block within the required_providers block.
- Data type: Block
- Default: None
- Example: Validate against provider schemas
A required_providers block supports the following configuration.
Provider label
The provider block label gives a name to the provider.
Provider source
The source attribute specifies the provider source address in the format <namespace>/<provider>.
- Data type: String
- Required
Provider version
A version constraint specifying which provider versions to validate against. When you specify a version range, Terraform policy performs validation using the schemas from versions of the provider matching the top and bottom of the range. When you omit the version, Terraform policy selects the oldest released version of the provider. You can't use wildcards (*) in version constraints.
- Data type: String
- Optional
terraform_config
The terraform_config block defines settings specific to the Terraform CLI provisioning the infrastructure. The terraform_config block takes the required_version as an argument.
- Data type: Block
- Default: None
required_version
A version constraint specifying which versions of Terraform the policy is compatible with.
- Data type: String
Examples
The following examples demonstrate common policy block configuration patterns for specific use cases.
Require minimum Terraform version
In the following example, the policy block sets a minimum Terraform version requirement of 1.16.0 or higher.
policy {
terraform_config {
required_version = ">= 1.16.0"
}
}
Configure multiple plugins
In the following example, the policy block configures two custom plugins that provide additional functions for use in policy evaluation.
policy {
plugins {
example = {
source = "../plugins/bin/example"
}
validator = {
source = "../plugins/bin/validator"
}
}
}
Validate against provider schemas
In the following example, the policy block specifies provider requirements to enable schema validation during tfpolicy validate.
policy {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 6.0.0, < 7.0.0"
}
}
}
When you specify a version range, Terraform policy performs best-effort validation using the provider schemas from both the minimum and maximum versions in the range. This helps ensure your policies are compatible across the specified provider versions.