#4540·opentofu

Mocked module executing code in test scenario

Author: pietervinckenCreated Aug 31, 2026Updated Sep 16, 2026
Labelsbugaccepted

Community note

[!TIP] Hi there, OpenTofu community! The OpenTofu team prioritizes issues based on upvotes. Please make sure to upvote this issue and describe how it affects you in detail in the comments to show your support.

OpenTofu Version

bash
OpenTofu v1.12.6
on darwin_arm64
+ provider registry.opentofu.org/hashicorp/aws v6.62.0
+ provider registry.opentofu.org/hashicorp/external v2.4.1
+ provider registry.opentofu.org/hashicorp/local v2.9.0
+ provider registry.opentofu.org/hashicorp/null v3.3.1

OpenTofu Configuration Files

main.tf

hcl
module "lambda_function" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "8.8.0"

  function_name = "my-lambda1"
  description   = "My awesome lambda function"
  handler       = "index.lambda_handler"
  runtime       = "python3.12"

  source_path = "./src/lambda-function1"

  tags = {
    Name = "my-lambda1"
  }
}

tests/basic.tftest.hcl

hcl
mock_provider "aws" {
}

run "bare_minimum_test" {
  command = plan

  override_module {
    target = module.lambda_function
    outputs = {
      lambda_function_arn = "arn:aws:lambda:eu-west-1:123456789012:function:lambda-eu-west-1-test123"
    }
  }

  assert {
    condition     = output.lambda_function_arn != ""
    error_message = "The Lambda function ARN should not be empty."
  }
}

Debug Output

Unable to create gist with output. Uploaded file instead.

log.txt

Error:

tests/basic.tftest.hcl... fail
  run "bare_minimum_test"... fail
╷
│ Error: Invalid index
│ 
│   on .terraform/modules/lambda/main.tf line 15, in locals:
│   15:   source_code_hash = var.ignore_source_code_hash ? null : (terraform_data.package_filename_for_hash[0].output.filename != null ? try(filebase64sha256(terraform_data.package_filename_for_hash[0].output.filename), null) : null)
│     ├────────────────
│     │ terraform_data.package_filename_for_hash is empty tuple
│ 
│ The given key does not identify an element in this collection value: the collection has no elements.
╵

Failure! 0 passed, 1 failed.

Expected Behavior

The test passes and all module code is bypassed without being executed. Output of module is overridden with provided override.

Actual Behavior

The test fails due to a piece of module code that is executed and failing the test scenario.

Steps to Reproduce

  1. tofu init
  2. tofu test

Additional Context

No response

References

No response