Given this Terraform variable declaration, what is the default value of instance_type?
variable "instance_type" {
type = string
default = "t2.micro"
}Look at the default attribute inside the variable block.
The default attribute sets the default value for the variable if no value is provided during deployment. Here, it is set to "t2.micro".
Consider this Terraform variable declaration without a default value:
variable "region" {
type = string
}What happens if you run terraform apply without providing a value for region?
Think about how Terraform handles required variables without defaults.
If a variable has no default value and no value is provided, Terraform will prompt the user to enter a value during execution.
You have a Terraform module with a variable environment that has a default value "dev". You want to reuse this module for production without changing the module code.
Which approach correctly overrides the default value when calling the module?
<pre>module "app" { source = "./app_module" # How to override environment? }</pre>
Think about how to pass values to modules to override defaults.
To override a module variable's default, you pass the new value in the module block. Changing the module code or renaming variables is not needed.
A Terraform variable is declared as sensitive with a default value containing a password:
variable "db_password" {
type = string
sensitive = true
default = "defaultPass123"
}What is the main security risk of this setup?
Consider what happens if code with default secrets is stored in public repositories.
Storing default sensitive values like passwords in code risks exposure if the code is shared or leaked, even if marked sensitive.
Given the following Terraform locals and variables:
variable "count" {
type = number
default = 3
}
locals {
total = var.count + 2
}What is the value of local.total if no value is provided for count?
Remember how default values are used when no input is given.
The variable count defaults to 3. The local total adds 2 to this, resulting in 5.