0
0
Terraformcloud~5 mins

Variable declaration syntax in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the basic syntax to declare a variable in Terraform?
Use the variable block with a name and optional attributes like type, default, and description.<br><br>Example:<br>
variable "region" {<br>  type = string<br>  default = "us-west-1"<br>  description = "AWS region to deploy resources"<br>}
Click to reveal answer
beginner
What does the default attribute do in a Terraform variable declaration?
It sets a value that Terraform uses if no other value is provided when running.<br><br>This makes the variable optional because it has a fallback value.
Click to reveal answer
intermediate
How do you specify the type of a variable in Terraform?
Use the type attribute inside the variable block.<br><br>Common types: string, number, bool, list(string), map(string).<br><br>Example:<br>
variable "ports" {<br>  type = list(number)<br>}
Click to reveal answer
beginner
Can you declare a variable without a default value in Terraform? What happens then?
Yes, you can declare a variable without a default.<br><br>Terraform will require you to provide a value when you run your configuration, or it will show an error.
Click to reveal answer
beginner
How do you add a description to a Terraform variable and why is it useful?
Use the description attribute inside the variable block.<br><br>This helps others (and yourself) understand what the variable is for.<br><br>Example:<br>
variable "env" {<br>  type = string<br>  description = "The environment to deploy to, e.g., dev or prod"<br>}
Click to reveal answer
Which block is used to declare a variable in Terraform?
Avariable
Bresource
Coutput
Dprovider
What happens if you declare a variable without a default value and do not provide a value when running Terraform?
ATerraform uses a random value
BTerraform uses an empty string
CTerraform throws an error and stops
DTerraform ignores the variable
How do you specify that a variable should be a list of strings?
Atype = string[]
Btype = list(string)
Ctype = array
Dtype = map(string)
Which attribute is used to provide a fallback value for a variable?
Avalue
Binitial
Cfallback
Ddefault
Why is adding a description to a variable useful?
AIt documents the variable's purpose
BIt changes the variable's value
CIt makes the variable required
DIt encrypts the variable
Explain how to declare a variable in Terraform including type, default, and description.
Think about the structure of a variable block and what each attribute does.
You got /4 concepts.
    What happens if you declare a variable without a default and do not provide a value when running Terraform?
    Consider Terraform's behavior when required inputs are missing.
    You got /2 concepts.