If a module declares a variable with a default value, must that variable also be defined within the module?

Prepare for the HashiCorp Terraform Associate Exam. Utilize flashcards and multiple-choice questions with detailed hints and explanations. Boost your confidence and be ready for success!

Multiple Choice

If a module declares a variable with a default value, must that variable also be defined within the module?

Explanation:
The key idea is that a default value makes a variable optional for the caller. When a module declares a variable with a default, you don’t have to pass a value for that variable when you use the module; if you omit it, Terraform uses the default. You still define the variable inside the module (with its type, description, and default), but you don’t need to redefine or supply a value at the call site unless you want to override it. If you do provide a value at the call site, that value overrides the default. If there’s no default and you don’t supply a value, Terraform will require one and produce an error.

The key idea is that a default value makes a variable optional for the caller. When a module declares a variable with a default, you don’t have to pass a value for that variable when you use the module; if you omit it, Terraform uses the default. You still define the variable inside the module (with its type, description, and default), but you don’t need to redefine or supply a value at the call site unless you want to override it. If you do provide a value at the call site, that value overrides the default. If there’s no default and you don’t supply a value, Terraform will require one and produce an error.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy