8

Here I have a variables.tf to define the Input variables.

# Input variable definitions

variable "project" {
  description = "project name, e.g. paylocity, paychex, ultipro"
  type = string
}

variable "environment" {
  description = "the environment of project, e.g. production, sandbox, staging"
  type = string
  default = "sandbox"
}

Is there a way to mark a variable as required or optional?

The pseudo-code in my mind looks like this.

variable "project" {
  description = "project name, e.g. paylocity, paychex, ultipro"
  type = string
  presence = required (or optional)
}
2
  • 3
    If it has default value, then its optional to provide. No sure what else would you like?
    – Marcin
    Commented Jun 30, 2021 at 1:09
  • 2
    I see. so all defined variables are actually "required" to the whole terraform script. but if a variable has default value, the variable becomes optional. Make sense.
    – Ryan Lyu
    Commented Jun 30, 2021 at 1:19

3 Answers 3

8

The purpose of variables.tf

variables.tf - here, you define the variables that must have values in order for your Terraform code to validate and run. You can also define default values for your variables in this file. Note that you don't need to define all of your variables in a file named

from What is the difference between variables.tf and terraform.tfvars?

Input Variables: Default Value

The variable declaration can also include a default argument. If present, the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform. The default argument requires a literal value and cannot reference other objects in the configuration.

From Terraform Documentation

Summary

  1. All defined variables must have values in order to run Terraform code.

  2. Once you set a default value for a variable, it becomes optional.

5

I'm using terraform 1.3.1. I think that this problem can solve with argument nullable.

The nullable argument in a variable block controls whether the module caller may assign the value null to the variable.

This feature is available in Terraform v1.1.0 and later.

Example:

    variable "example" {
      type     = string
      nullable = false
    }

Reference: https://developer.hashicorp.com/terraform/language/values/variables?optInFrom=terraform-io#disallowing-null-input-values

0

Since 0.14 there is an experimental (in time of writing) function that can allow you this. Here are some documents on the matter.

By default, for required attributes, Terraform will return an error if the source value has no matching attribute. Marking an attribute as optional changes the behavior in that situation: Terraform will instead just silently insert null as the value of the attribute, allowing the receiving module to describe an appropriate fallback behavior.

https://www.terraform.io/language/expressions/type-constraints#experimental-optional-object-type-attributes

https://danielrandell93.medium.com/terraform-optional-values-73407f1d5ce5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.