OpsCanary
Back to daily brief
terraformtestingPractitioner

Mastering Mocks in Terraform Testing

5 min read HashiCorp DocsApr 27, 2026
Share
PractitionerHands-on experience recommended

In the world of Terraform, testing your configurations without the need to provision actual resources is a game changer. Mocks allow you to simulate providers, resources, and data sources, enabling you to validate your Terraform code without incurring costs or requiring access to live credentials. This capability is particularly useful for ensuring your modules behave as expected before deployment.

Terraform's mocking functionality is straightforward. You can define a mock_provider block in your test configurations, which mimics the original provider's schema. This allows you to pass the mocked provider to your tests seamlessly. The override_during attribute specifies when Terraform should generate values for individual resources, while override_resource, override_data, and override_module let you customize outputs without calling the actual provider. For instance, you can create a mock resource like this:

HCL
mock_resource "aws_s3_bucket" { defaults = { arn = "arn:aws:s3:::name" } }

This approach allows you to test your code's logic without the overhead of actual resource management. However, keep in mind that mocked providers may not accurately reflect the expected formats of computed attributes, leading to potential discrepancies during real deployments. Ensure you are using Terraform v1.7.0 or later to access these mocking features, as they were introduced in this version.

Be aware of the limitations of mocks. They do not provide real data formats for computed attributes, which can lead to unexpected results. Always validate your configurations against real providers before going live. The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Key takeaways

  • Use `mock_provider` to simulate provider behavior in tests.
  • Implement `override_during` to control when values are generated.
  • Leverage `override_resource` and `override_data` to customize outputs without actual provider calls.
  • Ensure you are on Terraform v1.7.0 or later to utilize mocking features.
  • Be cautious of discrepancies in computed attributes when using mocks.

Why it matters

Effective testing with mocks can significantly reduce costs and risks associated with infrastructure provisioning. It allows teams to validate configurations quickly and confidently before deployment.

Code examples

HCL
1# bucket_name.tftest.hcl
2mock_provider "aws" {}
3run "sets_correct_name" {
4  variables {
5    bucket_name = "my-bucket-name"
6  }
7  assert {
8    condition = aws_s3_bucket.my_bucket.bucket == "my-bucket-name"
9    error_message = "incorrect bucket name"
10  }
11}
HCL
# mocked_providers.tftest.hcl
provider "aws" {}
mock_provider "aws" { alias = "fake" }
run "use_real_provider" { providers = { aws = aws } }
run "use_mocked_provider" { providers = { aws = aws.fake } }
HCL
mock_provider "aws" { override_during = plan }

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.