provider
To test a provider policy, add a mock provider block to your test file. Use the meta argument to set meta-attributes, including the provider source and version. Use the attrs argument to set values for any attributes defined by the provider itself.
Configuration model
provider "<provider_name>" "<test_case_name>" {
meta = {
source = "<provider_source>"
version = "<provider_version>"
alias = "<provider_alias>"
}
attrs = {
<attribute_name> = <attribute_value>
}
expect_failure = <boolean>
skip = <boolean>
input {
<input_name> = <input_value>
}
}
Complete configuration example
provider "aws" "approved_configuration" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "us-east-1"
}
}
provider "aws" "wrong_version" {
expect_failure = true
meta = {
source = "hashicorp/aws"
version = "4.0.0"
}
attrs = {
region = "us-east-1"
}
}
Specification
Labels
Provider blocks support two labels.
| Label | Required | Description |
|---|---|---|
| First label | Yes | The provider name (For example: aws, google, azurerm). |
| Second label | Yes | A unique name for this test case within the test file. |
Arguments
Provider blocks support the followng arguments.
| Argument | Required | Type | Description |
|---|---|---|---|
meta | No | Object | Meta-attributes for the provider, including source, version, and alias. |
attrs | No | Object | Provider configuration attributes as defined by the provider schema. |
expect_failure | No | Boolean | Set to true to expect this mock to fail the matching policies. Defaults to false. |
skip | No | Boolean | Set to true to skip evaluating this mock against policies. Defaults to false. |
input | No | Block | Set input values specific to this test case. |
The meta argument
You can set the following meta values in the meta argument.
| Attribute | Type | Description |
|---|---|---|
source | String | The provider source (For example: "hashicorp/aws"). |
version | String | The provider version (For example: "5.0.0"). |
alias | String | The provider alias, if testing aliased providers. |
The attrs argument
Set attributes for the mock provider with the attrs argument. Each provider defines the value attributes. Refer to the provider documentation for more information.
Examples
Test provider source and version
provider "aws" "approved_provider" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "us-east-1"
}
}
provider "aws" "unapproved_source" {
expect_failure = true
meta = {
source = "custom/aws"
version = "5.0.0"
}
attrs = {
region = "us-east-1"
}
}
Test provider configuration
provider "aws" "correct_region" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "us-east-1"
}
}
provider "aws" "wrong_region" {
expect_failure = true
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "eu-west-1"
}
}
Test aliased providers
provider "aws" "primary_region" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
alias = "primary"
}
attrs = {
region = "us-east-1"
}
}
provider "aws" "secondary_region" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
alias = "secondary"
}
attrs = {
region = "us-west-2"
}
}
Test with input variables
input "allowed_region" {
type = string
default = "us-east-1"
}
provider "aws" "configurable_region" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = input.allowed_region
}
}
provider "aws" "override_region" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "us-west-2"
}
input {
allowed_region = "us-west-2"
}
}
Test multiple provider configurations
provider "aws" "pass_all_checks" {
meta = {
source = "hashicorp/aws"
version = "5.0.0"
}
attrs = {
region = "us-east-1"
default_tags = {
tags = {
Environment = "production"
ManagedBy = "terraform"
}
}
}
}
provider "google" "approved_config" {
meta = {
source = "hashicorp/google"
version = "5.0.0"
}
attrs = {
project = "my-project"
region = "us-central1"
}
}
Related documentation
- Write and run policy tests - Learn how to write and run tests
- The
provider_policy {}block - Provider policy reference