Skip to content

[terraform test] Support expect_failures of resouces inside local module in run blocks #34700

@alexis-renard

Description

@alexis-renard

Terraform Version

Terraform v1.7.3

Use Cases

Hello,

We would like to expect_failure from a local module call done in a run block with terraform test. You can find some context about our issue down below.

The issue

It seems that resources, when placed inside a local module, are not handled by the expect_failures of the run block. Either we missed something and that is possible, either that would be a nice thing to have to be able to test our module with the integration of its own local modules.

The terraform test output

  run "check_precondition_ssm_parameter_encryption"... fail
╷
│ Error: Invalid `expect_failures` reference
│ 
│   on tests/main.tftest.hcl line 22, in run "check_precondition_ssm_parameter_encryption":
│   22:     module.workshop_participant_users.aws_ssm_parameter.example,
│ 
│ You cannot expect failures from module.workshop_participant_users.aws_ssm_parameter. You can only expect failures from checkable objects such as input variables, output values,
│ check blocks, managed resources and data sources.

Files content

# file : tests/main.tftest.hcl

run "check_precondition_ssm_parameter_encryption" {
  command = plan

  # [...]

  expect_failures = [
    module.workshop_participant_users.aws_ssm_parameter.example,
  ]
}
# file : main.tf

module "workshop_users" {
  source = "./modules/users"

  kms_key_id                    = var.create_kms_key ? try(resource.aws_kms_key.backend[0].arn, "") : ""
}

# file : modules/users/main.tf

resource "aws_ssm_parameter" "example" {

  # [...]

  key_id      = var.kms_key_id

  lifecycle {
    precondition {
      condition     = var.kms_key_id != ""
      error_message = "A key must be created to encrypt the ssm parameter."
    }
  }
}

Any ideas on that ?

Attempted Solutions

We have tried with the expecting failure on the whole module scope as well as for other resources inside the local module.

Proposal

No response

References

We are not sure whether or not it is also the case for the registry modules as well as the local ones tested here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions