Variables and Outputs Quiz
Quiz
Question 1 of 27
(0 answered)
Question 1
What is the PRIMARY benefit of using variables in Terraform instead of hardcoded values?
โ
Correct!
The primary benefit is reusability - you can use the same Terraform code across dev, staging, and production by simply changing variable values, without modifying the code itself.
โ
Incorrect
The primary benefit is reusability - you can use the same Terraform code across dev, staging, and production by simply changing variable values, without modifying the code itself.
Think about deploying the same infrastructure to different environments.
Question 2
Which of the following are primitive types in Terraform?
โ
Correct!
Primitive types in Terraform are: string, number, and bool. List and map are collection types (complex types).
โ
Incorrect
Primitive types in Terraform are: string, number, and bool. List and map are collection types (complex types).
Primitive types are the simplest, most basic data types.
Question 3
A variable without a default value is optional and can be omitted when running Terraform.
โ
Correct!
Variables without default values are REQUIRED and must be provided either through .tfvars files, command-line flags, or environment variables. Only variables WITH default values are optional.
โ
Incorrect
Variables without default values are REQUIRED and must be provided either through .tfvars files, command-line flags, or environment variables. Only variables WITH default values are optional.
Consider what happens when Terraform doesn’t know what value to use.
Question 4
To reference a variable named ‘region’ in your Terraform configuration, you would use: _____
โ
Correct!
Variables are referenced using the
var. prefix followed by the variable name: var.region.โ
Incorrect
Variables are referenced using the
var. prefix followed by the variable name: var.region.All variable references start with a three-letter prefix.
Question 5
What will be the final value of
instance_type given this precedence chain?# variables.tf: default = "t2.micro"
# TF_VAR_instance_type = "t2.small"
# terraform.tfvars: instance_type = "t2.medium"
# Command: terraform apply -var="instance_type=t2.large"What will this code output?
โ
Correct!
Command-line
-var flags have the highest precedence and override all other sources. The precedence order is: default < environment vars < .tfvars < -var flags.โ
Incorrect
Command-line
-var flags have the highest precedence and override all other sources. The precedence order is: default < environment vars < .tfvars < -var flags.Command-line flags win over all other variable sources.
Question 6
What is the difference between a Map and an Object in Terraform?
What is the difference between a Map and an Object in Terraform?
Map: Flexible key-value pairs where all values must be the same type. Keys can be any string, and you can add/remove keys dynamically.
Object: Fixed structure with predefined keys where each field can have a different type. Schema enforces required fields.
Example:
- Map:
map(string)- any keys, all string values - Object:
object({name=string, age=number})- fixed keys, mixed types
Did you get it right?
โ
Correct!
โ
Incorrect
Question 7
Arrange these variable value sources from LOWEST to HIGHEST precedence:
Drag to arrange in the correct order (lowest precedence first)
โฎโฎ
Default value in variable block
โฎโฎ
Environment variables (TF_VAR_*)
โฎโฎ
terraform.tfvars file
โฎโฎ
-var command-line flag
โ
Correct!
Correct precedence order: 1) Default values (lowest), 2) Environment variables, 3) .tfvars files, 4) -var command-line flags (highest).
โ
Incorrect
Correct precedence order: 1) Default values (lowest), 2) Environment variables, 3) .tfvars files, 4) -var command-line flags (highest).
Question 8
Complete the variable declaration to accept a list of strings:
Fill in the missing type constraint
variable "availability_zones" {
type = _____
default = ["us-east-1a", "us-east-1b"]
}โ
Correct!
For a list containing strings, the type constraint is
list(string). This ensures the variable only accepts a list where all elements are strings.โ
Incorrect
For a list containing strings, the type constraint is
list(string). This ensures the variable only accepts a list where all elements are strings.Question 9
When using
for_each with a set variable to create multiple S3 buckets, what advantage does this provide over using count with a list?โ
Correct!
The key advantage is stability: with
for_each on sets, resources are keyed by value (e.g., aws_s3_bucket.buckets["app-logs"]). Removing one doesn’t shift indexes like with count, preventing unintended resource destruction/recreation.โ
Incorrect
The key advantage is stability: with
for_each on sets, resources are keyed by value (e.g., aws_s3_bucket.buckets["app-logs"]). Removing one doesn’t shift indexes like with count, preventing unintended resource destruction/recreation.Think about what happens when you remove the middle element from a list vs a set.
Question 10
Which validation functions can be used in variable validation blocks?
โ
Correct!
All of these functions (and more) can be used in validation blocks. Common patterns include:
contains() for allowed values, can(regex()) for pattern matching, length() for size constraints, and can() to safely test expressions.โ
Incorrect
All of these functions (and more) can be used in validation blocks. Common patterns include:
contains() for allowed values, can(regex()) for pattern matching, length() for size constraints, and can() to safely test expressions.Validation blocks support a wide range of Terraform functions.
Question 11
Setting
sensitive = true on a variable encrypts the value in the state file.โ
Correct!
FALSE. The
sensitive flag only hides the value in console output and logs. It does NOT encrypt the value - sensitive values are still stored in plain text in the state file. Use external secret management (AWS Secrets Manager, Vault) for true security.โ
Incorrect
FALSE. The
sensitive flag only hides the value in console output and logs. It does NOT encrypt the value - sensitive values are still stored in plain text in the state file. Use external secret management (AWS Secrets Manager, Vault) for true security.Think about where Terraform actually stores resource data.
Question 12
To mark a variable as sensitive and hide its value in Terraform output, set _____ = true
โ
Correct!
The
sensitive attribute when set to true prevents Terraform from showing the variable value in plan and apply output, displaying <sensitive> instead.โ
Incorrect
The
sensitive attribute when set to true prevents Terraform from showing the variable value in plan and apply output, displaying <sensitive> instead.The attribute name describes what kind of data it protects.
Question 13
Given this configuration, how many subnets will be created?
variable "availability_zones" {
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
resource "aws_subnet" "public" {
count = length(var.availability_zones)
availability_zone = var.availability_zones[count.index]
cidr_block = "10.0.${count.index}.0/24"
}What will this code output?
โ
Correct!
3 subnets will be created.
length(var.availability_zones) returns 3 (number of items in the list), so count = 3. Each iteration creates one subnet in a different AZ: us-east-1a, us-east-1b, us-east-1c.โ
Incorrect
3 subnets will be created.
length(var.availability_zones) returns 3 (number of items in the list), so count = 3. Each iteration creates one subnet in a different AZ: us-east-1a, us-east-1b, us-east-1c.Count the number of items in the availability_zones list.
Question 14
What is the correct way to access a module’s output value in the root module?
โ
Correct!
Module outputs are accessed using
module.<module_name>.<output_name>. For example, if you have module "vpc" with output "vpc_id", you reference it as module.vpc.vpc_id.โ
Incorrect
Module outputs are accessed using
module.<module_name>.<output_name>. For example, if you have module "vpc" with output "vpc_id", you reference it as module.vpc.vpc_id.The syntax starts with the keyword ‘module’.
Question 15
What is the purpose of output values in Terraform?
What is the purpose of output values in Terraform?
Output values serve four main purposes:
- Display information after
terraform apply(e.g., IP addresses, URLs) - Share data between root and child modules
- Query infrastructure state using
terraform output - Provide values to external systems (via
-jsonflag)
Example:
output "instance_ip" {
value = aws_instance.web.public_ip
}Query with: terraform output instance_ip
Did you get it right?
โ
Correct!
โ
Incorrect
Question 16
Complete the validation block to ensure the environment is one of: dev, staging, or prod
Fill in the missing condition
variable "environment" {
type = string
validation {
condition = _____
error_message = "Environment must be dev, staging, or prod."
}
}โ
Correct!
The
contains() function checks if a value exists in a list. Syntax: contains(list, value). This validates that var.environment is one of the allowed values.โ
Incorrect
The
contains() function checks if a value exists in a list. Syntax: contains(list, value). This validates that var.environment is one of the allowed values.Question 17
Which statements about complex types are TRUE?
โ
Correct!
All statements are true. Complex types combine basic types: map of objects gives flexibility + structure, list of objects gives order + schema, objects allow mixed types per field, and maps enforce same type for all values.
โ
Incorrect
All statements are true. Complex types combine basic types: map of objects gives flexibility + structure, list of objects gives order + schema, objects allow mixed types per field, and maps enforce same type for all values.
Complex types build on the rules of their component types.
Question 18
When using *.auto.tfvars files, Terraform loads them automatically in alphabetical order without needing the -var-file flag.
โ
Correct!
TRUE. Files matching
*.auto.tfvars or *.auto.tfvars.json are automatically loaded by Terraform in alphabetical order, without requiring explicit -var-file flags.โ
Incorrect
TRUE. Files matching
*.auto.tfvars or *.auto.tfvars.json are automatically loaded by Terraform in alphabetical order, without requiring explicit -var-file flags.The ‘.auto.’ in the filename is a hint about its behavior.
Question 19
Which approach is BEST PRACTICE for storing database passwords in Terraform?
โ
Correct!
Best practice is to fetch secrets from external secret management at runtime using data sources. This keeps secrets out of your code, state files, and git. The
sensitive flag only hides display output but doesn’t encrypt storage.โ
Incorrect
Best practice is to fetch secrets from external secret management at runtime using data sources. This keeps secrets out of your code, state files, and git. The
sensitive flag only hides display output but doesn’t encrypt storage.Think about where secrets should actually be stored and managed.
Question 20
What will happen when you run terraform apply with this configuration?
variable "project_name" {
type = string
# No default value
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = var.project_name
}
}What will this code output?
โ
Correct!
Terraform will prompt you to enter a value for
project_name because it’s a required variable (no default). You can provide it interactively, via -var flag, environment variable, or .tfvars file.โ
Incorrect
Terraform will prompt you to enter a value for
project_name because it’s a required variable (no default). You can provide it interactively, via -var flag, environment variable, or .tfvars file.Required variables must be provided somehow.
Question 21
To set a Terraform variable via environment variable, use the prefix _____
โ
Correct!
Environment variables for Terraform must use the
TF_VAR_ prefix. For example, to set variable region, use: export TF_VAR_region="us-east-1"โ
Incorrect
Environment variables for Terraform must use the
TF_VAR_ prefix. For example, to set variable region, use: export TF_VAR_region="us-east-1"The prefix is three characters followed by an underscore.
Question 22
Explain the difference between
count and for_each when creating multiple resources.Explain the difference between
count and for_each when creating multiple resources.count:
- Creates resources indexed by number:
[0],[1],[2] - Order matters; removing middle item shifts indexes
- Can cause unintended resource destruction/recreation
- Use for: simple, fixed-count resources
for_each:
- Creates resources keyed by value:
["web"],["app"] - No index shifting; remove by name
- Safer for dynamic resource sets
- Works with: sets and maps
- Use for: resources that change dynamically
Example impact:
With count, removing [1] makes [2] become [1] โ destroys & recreates.
With for_each, removing ["app"] only removes that one resource.
Did you get it right?
โ
Correct!
โ
Incorrect
Question 23
In a map of objects variable, what does
each.key represent when used with for_each?โ
Correct!
each.key represents the map key in a for_each loop. For instances = {web = {...}, app = {...}}, each.key would be “web” or “app”. Use each.value to access the object fields.โ
Incorrect
each.key represents the map key in a for_each loop. For instances = {web = {...}, app = {...}}, each.key would be “web” or “app”. Use each.value to access the object fields.Think about the structure: map has keys and values.
Question 24
Complete the object type definition for an instance configuration:
Fill in the missing type structure
variable "instance_config" {
type = _____
default = {
instance_type = "t2.micro"
ami = "ami-12345"
monitoring = true
}
}โ
Correct!
An object type requires defining the schema with field names and their types:
object({instance_type = string, ami = string, monitoring = bool}). Field order doesn’t matter, but all fields must be defined.โ
Incorrect
An object type requires defining the schema with field names and their types:
object({instance_type = string, ami = string, monitoring = bool}). Field order doesn’t matter, but all fields must be defined.Question 25
Which are valid ways to provide variable values to Terraform? (Select all that apply)
โ
Correct!
All of these are valid methods to provide variable values. Terraform supports multiple input methods with different precedence levels, giving flexibility in how you configure your infrastructure.
โ
Incorrect
All of these are valid methods to provide variable values. Terraform supports multiple input methods with different precedence levels, giving flexibility in how you configure your infrastructure.
Terraform provides multiple ways to set variables for flexibility.
Question 26
The ‘any’ type should be preferred over specific types like ‘string’ or ’number’ because it provides more flexibility.
โ
Correct!
FALSE. While ‘any’ is more flexible, specific types are preferred because they provide type safety, better validation, and clearer documentation. Only use ‘any’ when the type truly varies based on input or when building generic modules.
โ
Incorrect
FALSE. While ‘any’ is more flexible, specific types are preferred because they provide type safety, better validation, and clearer documentation. Only use ‘any’ when the type truly varies based on input or when building generic modules.
Consider the trade-off between flexibility and safety/clarity.
Question 27
Arrange these file organization elements in the recommended structure:
Drag to arrange in logical order for a Terraform project
โฎโฎ
variables.tf (declarations)
โฎโฎ
terraform.tfvars (common values)
โฎโฎ
main.tf (resources)
โฎโฎ
outputs.tf (output definitions)
โ
Correct!
Recommended organization: 1) variables.tf for declarations, 2) terraform.tfvars for values, 3) main.tf for resources, 4) outputs.tf for outputs. This separation makes code easier to navigate and maintain.
โ
Incorrect
Recommended organization: 1) variables.tf for declarations, 2) terraform.tfvars for values, 3) main.tf for resources, 4) outputs.tf for outputs. This separation makes code easier to navigate and maintain.
Quiz Results
Score
0/0
Accuracy
0%
Right
0
Wrong
Skipped
0
Last updated on