0
0
Terraformcloud~5 mins

Mock providers in tests in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When writing Terraform tests, you sometimes need to simulate cloud providers to test your code without creating real resources. Mock providers let you do this by pretending to be the real provider, so you can check your code's logic safely.
When you want to test your Terraform modules without incurring cloud costs.
When you need to verify your Terraform code logic without deploying real infrastructure.
When you want to run automated tests in a CI/CD pipeline without cloud access.
When you want to simulate provider responses to test error handling.
When you want to speed up tests by avoiding slow real cloud API calls.
Config File - main.tf
main.tf
terraform {
  required_providers {
    mock = {
      source  = "hashicorp/mock"
      version = "1.0.0"
    }
  }
}

provider "mock" {}

resource "mock_resource" "example" {
  name = "test-resource"
}

output "resource_name" {
  value = mock_resource.example.name
}

This Terraform configuration uses the mock provider to simulate a cloud provider. The mock_resource resource is a fake resource used to test your Terraform code without real infrastructure. The output shows the resource's name to verify the mock works.

Commands
This command initializes Terraform, downloads the mock provider plugin, and prepares the working directory for running Terraform commands.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/mock versions matching "1.0.0"... - Installing hashicorp/mock v1.0.0... - Installed hashicorp/mock v1.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command shows what Terraform will do. It uses the mock provider to simulate creating the resource without real cloud calls.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # mock_resource.example will be created + resource "mock_resource" "example" { + id = (known after apply) + name = "test-resource" } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the plan, creating the mock resource. Since it's a mock, no real infrastructure is created, but Terraform state updates as if it did.
Terminal
terraform apply -auto-approve
Expected OutputExpected
mock_resource.example: Creating... mock_resource.example: Creation complete after 1s [id=mock-resource-id] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: resource_name = "test-resource"
-auto-approve - Automatically approve the apply without prompting
This command shows the output value from the mock resource to verify the test result.
Terminal
terraform output resource_name
Expected OutputExpected
test-resource
Key Concept

If you remember nothing else from this pattern, remember: mock providers let you test Terraform code safely without creating real cloud resources.

Common Mistakes
Trying to use a mock provider without initializing Terraform first.
Terraform won't download the mock provider plugin, so commands fail.
Always run 'terraform init' before planning or applying.
Expecting mock providers to create real infrastructure.
Mock providers simulate resources only in Terraform state; no real cloud changes happen.
Use mock providers only for testing logic, not for real deployments.
Not specifying the mock provider source and version in the configuration.
Terraform cannot find or use the mock provider without this info.
Declare the mock provider in the 'required_providers' block with source and version.
Summary
Initialize Terraform to download the mock provider plugin.
Use 'terraform plan' to see simulated resource changes.
Apply the plan to update Terraform state without real infrastructure.
Use 'terraform output' to verify mock resource outputs.