Mock providers and fast unit tests
mock_provider, mock_resource defaults and the override blocks let terraform test run logic checks with no credentials, and show what mocks cannot prove.
Mock providers and fast unit tests
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- mock_provider (Terraform 1.7+) keeps the real schema but invents values for computed attributes, so tests need no credentials and create nothing.
- Unset computed strings become random 8-character strings, numbers 0, booleans false and collections empty, so give ARNs and IDs realistic defaults with mock_resource.
- override_resource, override_data and override_module skip the provider entirely for one target; the most local override wins.
- override_during = plan generates values at plan time, so command = plan tests can assert on normally unknown values. Mocks prove logic, not that the cloud accepts it.
The test that needs an AWS account
You write a module that makes an S3 bucket, a bucket policy and an IAM role. You want to test the logic: does the policy reference the bucket’s ARN, does count drop the replica when the flag is off. A command = apply test would prove all of it against real AWS, and also needs credentials in CI, a sandbox account, a minute of waiting per run and a cleanup that sometimes fails. For logic that never leaves your configuration, that is a lot of machinery.
A mock provider replaces the real one inside terraform test. Terraform still reads the provider’s real schema, so a wrong attribute name still fails. But every API call is replaced by values Terraform makes up. Mocking arrived in Terraform 1.7, which the docs state as the minimum version.
What a mock does
mock_provider "aws" {}
run "policy_points_at_bucket" {
command = apply
assert {
condition = aws_s3_bucket_policy.logs.bucket == aws_s3_bucket.logs.id
error_message = "policy attached to the wrong bucket"
}
}
With mock_provider "aws" {} no credentials are needed, and nothing exists afterwards. The apply is against the mock, so it is the fast kind. I wrote this from the mocking documentation and did not run it, since Terraform is not installed here.
The mock has to invent values for computed attributes, the ones the cloud normally fills in, such as an ARN or an ID. The docs list the rules for optional computed attributes you did not set: numbers become 0, booleans false, strings a random eight-character alphanumeric string, collections empty, and objects are filled in recursively. Required attributes must still come from your configuration.
I implemented those documented rules on a toy bucket schema to see the effect. This is my stand-in, not Terraform:
$ python3 -I mockgen.py
no defaults : {'id': 'ujOeHwdF', 'arn': 'cAefAZhn', 'bucket_domain_name': 'M6Jy8c1r', 'versioning_enabled': False, 'object_lock_days': 0, 'tags_all': {}, 'cors_rules': []}
with default: {'id': 'ihtYlKNx', 'arn': 'arn:aws:s3:::name', 'bucket_domain_name': 'HddmQAtK', 'versioning_enabled': False, 'object_lock_days': 0, 'tags_all': {}, 'cors_rules': []}
arn looks like an ARN? False
The arn is eight random letters. If your configuration builds a policy from it, or a test checks that it starts with arn:aws:s3:::, the default value will not do. The docs say it plainly: mocked values rarely match real provider formats. So you set defaults.
Defaults, overrides and where to put them
Three tools, from narrowest to widest:
mock_resource and mock_data with defaults set values for one resource type across the provider:
mock_provider "aws" {
mock_resource "aws_s3_bucket" {
defaults = {
arn = "arn:aws:s3:::name"
}
}
}
Shared mock definitions can go into a .tfmock.hcl file and be loaded with source = "./testing/aws". A nested block beats the sourced file.
override_resource, override_data and override_module bypass the provider entirely for one specific target. Terraform does not call the provider for it at all. They take a target and values (or outputs for a module), and can sit at the top of a test file, inside a run block, or inside mock_provider. The most local one wins: run block, then file, then the mock provider. A data source that reads a secret from a bucket is the classic case: override it with a fixed body and the module under test never knows.
override_during decides when generated values appear. By default a mock generates values during apply, and plan shows (known after apply). Setting override_during = plan generates them at plan time, which lets a command = plan test assert on attributes that are normally unknown until apply. That is what turns many logic tests into plan tests, and plan tests are the fastest.
There is a limit worth knowing: for repeated nested blocks you cannot target one instance. One set of values applies to all of them. OpenTofu’s tofu test page notes that from OpenTofu 1.12 you can specify instances of a resource or data source that uses for_each or count, for its own override blocks.
What mocks cannot tell you
A mock proves your configuration’s logic. It does not prove AWS will accept it. An invalid CIDR the provider rejects at apply, a name already taken in the real account, an IAM action that does not exist, a quota: none of that is in a mock, because there is no API. This is why the split is two layers:
- Many mock-backed tests, run on every pull request, covering conditionals, naming,
for_eachkeys, outputs and validation errors viaexpect_failures. - A few real-apply tests, in a sandbox account on a schedule or before release, plus Terratest if you need to call the deployed thing over HTTP.
Mocks are also recent enough that coverage is still growing. The Terraform 1.17 changelog, published 21 October 2026, lists “mock_provider support for ephemeral resources” as an enhancement, so a module using ephemeral resources could not be mocked cleanly before it.
About the video
The embed is Learn with GVR’s “Terraform Test Mocking - Terraform v1.7.0 - Mocking Framework for IaC, Explained with Demo”, 47 minutes 16 seconds, from June 2024. The lesson clips it from 4:58, where its chapter list reaches the purpose of test mocking, through the mock types, mock_provider syntax, overrides and a demo. I picked the start from the published chapters and set no end, so it runs to the end of the video, and I have not watched it. It is a small channel. It is long, and the earlier chapters repeat what the previous lesson covered. English was judged from the title, description, channel and its English auto-caption track. Merge Ready’s 3-minute video in deeper is a short summary of the same feature.
Where this fits
Terraform’s own test runner now covers plan-time and mock-backed tests. When you need to prove that a deployed system answers over the network, Terratest is the next tool.
Check yourself
Why does a mocked aws_s3_bucket arn usually need a default?
Computed strings are random 8-character values, which do not look like an ARN, so anything that builds on the arn or checks its format breaks.
What does override_during = plan change?
Generated values appear in the plan instead of showing (known after apply), so plan-only tests can assert on them.
What can a mock never tell you?
Whether the real cloud will accept the configuration: invalid values, name collisions, missing permissions and quotas.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.