Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not nullable module input variable validates null before default value #30307

Closed
MusicDin opened this issue Jan 6, 2022 · 3 comments
Closed
Assignees
Labels
bug config confirmed a Terraform Core team member has reproduced this issue v1.1 Issues (primarily bugs) reported against v1.1 releases

Comments

@MusicDin
Copy link
Contributor

MusicDin commented Jan 6, 2022

Terraform Version

1.1.2

Terraform Configuration Files

Module:

# main.tf
module "demo" {
  source = "./modules/demo"
  action = null
}
# module/demo/variables.tf
variable "action" {
  type        = string
  description = "Action (create, upgrade)"
  default     = "create"
  nullable    = false

  validation {
    condition     = contains(["create", "upgrade"], var.action)
    error_message = "Variable 'action' is invalid. Possible values are: ['create', 'upgrade']."
  }
}

Debug Output

image

Expected Behavior

Before validating variable value, the default value should be set as it's value, because the variable is tagged as nullable = false.

In the docs it's written that:
Setting nullable to false ensures that the variable value will never be null within the module. If nullable is false and the variable has a default value, then Terraform uses the default when a module input argument is null.

Actual Behavior

In cases where input variable has nullable argument set to false and the value passed to the variable is null, the validated value is null instead of the default value.

Steps to Reproduce

  1. terraform init
  2. terraform plan/apply

Additional context

I know there are workarounds for this problem, but shouldn't the default value be inserted into validation condition, because the default value will be used further in the module and not the null?

@MusicDin MusicDin added bug new new issue not yet triaged labels Jan 6, 2022
@jbardin jbardin added config confirmed a Terraform Core team member has reproduced this issue v1.1 Issues (primarily bugs) reported against v1.1 releases and removed new new issue not yet triaged labels Jan 6, 2022
@jbardin
Copy link
Member

jbardin commented Jan 6, 2022

Hi @MusicDin,

Thanks for filing the issue. This does appear to be unintended behavior related to the new nullable option in combination with the validation rules.

What appears to be happening is that the validation rules (or really the language in general) never had the possibility of ensuring a non-nullable value before, so any condition must have been able to handle a null value. Now that a variable has a way to ensure the value is never null, we also need to pass that constraint into the validation logic.

Thanks!

@jbardin
Copy link
Member

jbardin commented Jan 18, 2022

Closed via #30330 and #29959

@jbardin jbardin closed this as completed Jan 18, 2022
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config confirmed a Terraform Core team member has reproduced this issue v1.1 Issues (primarily bugs) reported against v1.1 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants