0
0
Terraformcloud~5 mins

Module inputs (variables) in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a module input variable in Terraform?
A module input variable is a way to pass values into a Terraform module so it can be customized when used. Think of it like giving a recipe different ingredients to change the final dish.
Click to reveal answer
beginner
How do you define a variable inside a Terraform module?
You define a variable using the variable block with a name and optionally a type and default value. For example:<br>
variable "region" {
  type = string
  default = "us-west-1"
}
Click to reveal answer
beginner
How do you pass a value to a module input variable when calling a module?
You pass values by specifying the variable name and value inside the module block. For example:<br>
module "my_module" {
  source = "./modules/example"
  region = "us-east-1"
}
Click to reveal answer
intermediate
What happens if you do not provide a value for a module input variable that has no default?
Terraform will show an error and stop because it needs a value to use. It's like missing an important ingredient in a recipe.
Click to reveal answer
beginner
Why is it good practice to use variables in Terraform modules?
Variables make modules reusable and flexible. You can use the same module with different settings without changing the module code, just by passing different inputs.
Click to reveal answer
How do you define a string variable named "env" with a default value "dev" in a Terraform module?
Avariable "env" { type = string default = "dev" }
Binput "env" = "dev"
Cvar env = "dev"
Dmodule "env" { default = "dev" }
What is the purpose of module input variables in Terraform?
ATo define resources inside modules
BTo store output values from modules
CTo pass values into modules to customize their behavior
DTo create new modules automatically
If a module variable has no default and you don't provide a value, what happens?
ATerraform throws an error and stops
BTerraform assigns an empty string
CTerraform uses a null value
DTerraform ignores the variable
How do you pass a value to a module variable named "region" when calling the module?
Amodule.region = "us-east-1"
Bregion = "us-east-1" inside the module block
Cset region "us-east-1"
Dvariable "region" = "us-east-1"
Why should you use variables in Terraform modules?
ATo define provider settings
BTo hardcode values inside modules
CTo create output values
DTo make modules reusable with different inputs
Explain how to create and use input variables in a Terraform module.
Think about how you give a module different settings when you use it.
You got /4 concepts.
    Describe what happens if a required module input variable is not provided a value.
    What if you forget an important ingredient in a recipe?
    You got /3 concepts.