In a Terraform test file, when do you reach for `override_resource`, `override_data` or `override_module`, and which override wins when several target one address?
answer
- an address, not a type
- real providers count too
- values versus outputs
- the narrowest scope beats the wider one
- a module override skips the whole child
basics
~20 sTerraform's override blocks replace one address: override_resource and override_data set computed values, override_module sets a child module's outputs and creates nothing inside it. A run block's override beats the file's, and both beat a mock_provider's.
solid answer
~50 sOverride blocks target one address, where `mock_resource` defaults target a whole type, and they work under real providers as well as mocks. `override_resource` skips the provider's plan, read and apply calls for that resource and fills its computed attributes from `values`. `override_data` does the same for a data source, which is the usual way to hand a module a canned lookup. `override_module` replaces a whole child module call: it takes `outputs` instead of `values`, creates no resources in the module, and cannot sit in a `mock_provider` block or a mock data file. All three can sit at file level or inside a `run` block. When several match an address, the `run` block wins, then the file, then the `mock_provider`. An `override_resource` only fills computed values when the resource is first created, so a later `run` block cannot change them.
code
hcl · 29 linesmock_provider "aws" {}
override_data {
target = module.credentials.data.aws_s3_object.data_bucket
values = {
body = "{\"username\":\"username\",\"password\":\"password\"}"
}
}
run "test_file_override" {
assert {
condition = jsondecode(local_file.credentials_json.content).username == "username"
error_message = "incorrect username"
}
}
run "test_run_override" {
override_data {
target = module.credentials.data.aws_s3_object.data_bucket
values = {
body = "{\"username\":\"a_different_username\",\"password\":\"password\"}"
}
}
assert {
condition = jsondecode(local_file.credentials_json.content).username == "a_different_username"
error_message = "incorrect username"
}
}go deeper
Name the three blocks and what each replaces, and remember that override_module takes outputs while the other two take values.
Explain the precedence of run block, file and mock provider, where each block may be placed, and why override_module cannot live in a mock provider.
Be ready to diagnose an override with no effect: a later run block on a resource that already exists, a renamed address that only warns, or an output that came back null.
Weigh the coupling. Every override hard-codes an address, so heavy use ties tests to a module's internals. Set a limit on it, and treat warnings as failures in the shared pipeline.
## Mocks replace a provider, overrides replace an address In Terraform's test framework a `mock_provider` swaps out a whole provider, and its `mock_resource` and `mock_data` defaults apply to every object of a type. An **override** is narrower: it names one resource, data source or module call in a `target` attribute and supplies values for it alone. Overrides work whichever provider sits underneath, so one test can run a real provider and still fake the one expensive or unreachable lookup. An **address** is the path Terraform uses for an object, such as `module.credentials.data.aws_s3_object.data_bucket`. A target can name a whole resource or module, or one instance such as `aws_s3_bucket.logs[0]`, and the instance address is tried first. ## The three blocks | Block | Replaces | Payload | Allowed in | What Terraform skips | |---|---|---|---|---| | `override_resource` | a managed resource | `values` | file, `run`, `mock_provider`, mock data file | the provider's plan, read and apply calls for it | | `override_data` | a data source | `values` | file, `run`, `mock_provider`, mock data file | the provider's read call for it | | `override_module` | a child module call | `outputs` | file, `run` | every resource in the module, plus its input validation and output preconditions | Reach for each like this: - **`override_data`** when the code reads something from outside the configuration, such as an object in a bucket, and the test needs a specific answer. - **`override_resource`** when one resource needs a particular computed value, or when it should not be created while its neighbours are. - **`override_module`** when a child module's insides are not what this test is about and its outputs are all the parent needs. ```hcl mock_provider "aws" {} override_module { target = module.credentials outputs = { data = { username = "username", password = "password" } } } ``` ## Which override wins Terraform assembles the overrides for each `run` block in a fixed order: 1. Overrides in the `run` block itself always apply. 2. File-level overrides apply unless the `run` block already covers that address. 3. Overrides in a `mock_provider` or its mock data files come last, and they apply only to objects that mock provider is handling. Different scopes overlapping is normal, and a `run` block commonly specialises a file-level default. The same address twice in **one** scope is an error, such as `Duplicate override_data block`. ## What an override does not do The surprises come from what these blocks leave in place: - **Configured arguments stay.** `values` fills computed attributes only. A key naming an argument the configuration sets does not replace it. - **Validation stays.** Terraform still sends the resource's configuration to the provider for validation before it reaches the override branch, so an invalid argument still fails. - **Mock defaults are bypassed.** A computed attribute missing from `values` gets a generated value, even if a `mock_resource` block has a default for it. - **First creation only.** The source is explicit that overrides matter only when a resource is created. Once an earlier `run` block has created it, a later `run` block's different `values` do not change it. Data sources are read afresh, so a per-run `override_data` works. - **Missing outputs are null.** The docs page says unspecified outputs are generated, but the implementation returns `null` for any output missing from `override_module`, because it has no type to generate from. ## Credentials and typos Two operational traps follow: - An override does not make a real provider credential-free. A `provider` block still gets configured, which is why the documentation's credential-free examples keep a `mock_provider` beside their overrides. - A mistyped `target` is only a warning, `Invalid override target`, and an override loaded from a mock data file raises not even that. Under a real provider, the typo sends the object you meant to fake to that provider, and an apply run creates it for real. Read the warnings in test output as failures. The address is the entire link between the override and the code it replaces, and it breaks quietly when someone renames a resource, so the test can keep passing while the override does nothing.
- Why does an `override_resource` in a second `run` block sometimes leave the old value in place?Because overrides fill computed values only when a resource is created. If an earlier `run` block already created it in the same state, the later plan is an update that keeps the prior computed values. Give the case its own state or its own test file, or override the data source upstream, which is read afresh.
- When would you choose `override_module` over mocking the child module's provider?When the child's internals are not the subject and it has its own tests. The parent test then needs only outputs, however many resources the child holds. The cost is real: the child's input validation and output preconditions stop running, and you hand-write an output shape that can drift from the real child.
- Does `override_resource` let a test under a real provider run with no credentials?Not on its own. It stops the provider being called for that resource, but a real `provider` block still gets configured, and every resource you did not override still calls the cloud. For a credential-free job, declare a `mock_provider` and use overrides for the specific values you need.
- Overrides need a mock_provider; a resource under a real provider cannot be overridden.
- override_module can sit inside a mock_provider block next to override_data.
- override_resource values can replace an argument the configuration sets.
- A file-level override beats the one inside a run block.
- A mistyped override target fails the test straight away.
- An overridden child module still runs its variable validation rules.