Resources and Meta-Arguments Quiz
Quiz
Question 1 of 26
(0 answered)
Question 1
What are the five core meta-arguments that work with any Terraform resource type?
โ
Correct!
The five meta-arguments available for all resource types are:
count, for_each, depends_on, provider, and lifecycle. These are Terraform-specific arguments that control resource behavior.โ
Incorrect
The five meta-arguments available for all resource types are:
count, for_each, depends_on, provider, and lifecycle. These are Terraform-specific arguments that control resource behavior.Think about the arguments shown in the meta-arguments matrix.
Question 2
Complete the code to create 3 identical EC2 instances:
Fill in the missing meta-argument
resource "aws_instance" "server" {
_____ = 3
ami = "ami-123"
instance_type = "t2.micro"
}โ
Correct!
The
count meta-argument creates multiple identical instances of a resource. Setting count = 3 creates three instances addressed as [0], [1], [2].โ
Incorrect
The
count meta-argument creates multiple identical instances of a resource. Setting count = 3 creates three instances addressed as [0], [1], [2].Question 3
When using
count, if you remove an item from the middle of the list, Terraform will only destroy that specific item without affecting others.โ
Correct!
This is FALSE and a major pitfall of
count. When using count, items are indexed by position. Removing an item from the middle causes all subsequent items to shift down, resulting in Terraform destroying and recreating those shifted items. This is why for_each is often preferred for dynamic collections.โ
Incorrect
This is FALSE and a major pitfall of
count. When using count, items are indexed by position. Removing an item from the middle causes all subsequent items to shift down, resulting in Terraform destroying and recreating those shifted items. This is why for_each is often preferred for dynamic collections.Think about how count uses array indices.
Question 4
What is the primary advantage of using
for_each over count?โ
Correct!
for_each provides stable addressing using keys instead of numeric indices. This means adding or removing items doesn’t affect other items. For example, server["web"] remains stable even if server["app"] is removed.โ
Incorrect
for_each provides stable addressing using keys instead of numeric indices. This means adding or removing items doesn’t affect other items. For example, server["web"] remains stable even if server["app"] is removed.Consider what happens when you remove an item from the middle of a collection.
Question 5
Within a resource using
for_each, what variable gives you access to the current map value?โ
Correct!
Inside a resource with
for_each, you access the current key with each.key and the current value with each.value. For example, in for_each = var.instances, each.key might be “web” and each.value would be the corresponding value.โ
Incorrect
Inside a resource with
for_each, you access the current key with each.key and the current value with each.value. For example, in for_each = var.instances, each.key might be “web” and each.value would be the corresponding value.It starts with ’each.’ and refers to the data, not the identifier.
Question 6
Given this Terraform code, what will be the reference address for the ‘app’ instance?
variable "servers" {
default = ["web", "app", "db"]
}
resource "aws_instance" "server" {
for_each = toset(var.servers)
ami = "ami-123"
instance_type = "t2.micro"
}What will this code output?
โ
Correct!
When using
for_each with a set, resources are addressed using the key in brackets: aws_instance.server["app"]. This is different from count, which uses numeric indices like [0], [1], [2].โ
Incorrect
When using
for_each with a set, resources are addressed using the key in brackets: aws_instance.server["app"]. This is different from count, which uses numeric indices like [0], [1], [2].for_each uses key-based addressing, not numeric indices.
Question 7
Which of the following are valid use cases for the
depends_on meta-argument?โ
Correct!
depends_on should only be used for hidden dependencies that Terraform cannot automatically infer. Option 1 (IAM policy) and Option 3 (database without direct reference) are valid uses. Option 2 (subnet referencing VPC) creates an implicit dependency automatically. Option 4 is not recommended - only use depends_on when necessary, not for all resources.โ
Incorrect
depends_on should only be used for hidden dependencies that Terraform cannot automatically infer. Option 1 (IAM policy) and Option 3 (database without direct reference) are valid uses. Option 2 (subnet referencing VPC) creates an implicit dependency automatically. Option 4 is not recommended - only use depends_on when necessary, not for all resources.depends_on is for HIDDEN dependencies that Terraform can’t automatically detect through references.
Question 8
What does the
provider meta-argument do?What does the
provider meta-argument do?Selects a non-default provider configuration
The provider meta-argument allows you to use an aliased provider instead of the default one. This is essential for multi-region or multi-account deployments.
Example: provider = aws.west uses the AWS provider with alias “west” instead of the default provider.
Did you get it right?
โ
Correct!
โ
Incorrect
Question 9
Arrange these lifecycle argument options in order from most to least restrictive:
Drag to arrange from most restrictive (prevents changes) to least restrictive
โฎโฎ
prevent_destroy
โฎโฎ
create_before_destroy
โฎโฎ
ignore_changes
โฎโฎ
replace_triggered_by
โ
Correct!
From most to least restrictive: 1)
prevent_destroy - blocks destruction entirely, 2) ignore_changes - prevents Terraform from making changes to specified attributes, 3) replace_triggered_by - forces replacement based on other resource changes, 4) create_before_destroy - just changes replacement order but doesn’t prevent changes.โ
Incorrect
From most to least restrictive: 1)
prevent_destroy - blocks destruction entirely, 2) ignore_changes - prevents Terraform from making changes to specified attributes, 3) replace_triggered_by - forces replacement based on other resource changes, 4) create_before_destroy - just changes replacement order but doesn’t prevent changes.Question 10
What is the default behavior when a
remote-exec provisioner fails during resource creation?โ
Correct!
By default (
on_failure = fail), when a provisioner fails, the resource is marked as tainted and the apply fails. On the next apply, Terraform will recreate the tainted resource. You can change this with on_failure = continue to ignore errors.โ
Incorrect
By default (
on_failure = fail), when a provisioner fails, the resource is marked as tainted and the apply fails. On the next apply, Terraform will recreate the tainted resource. You can change this with on_failure = continue to ignore errors.Think about what happens to resources that fail during creation.
Question 11
Provisioners are recommended as the first choice for configuring resources in Terraform.
โ
Correct!
FALSE. Provisioners are a LAST RESORT. The documentation explicitly warns about this. Prefer alternatives like cloud-init/user_data, Packer for AMI building, or configuration management tools (Ansible, Chef). Use provisioners only when no other option exists.
โ
Incorrect
FALSE. Provisioners are a LAST RESORT. The documentation explicitly warns about this. Prefer alternatives like cloud-init/user_data, Packer for AMI building, or configuration management tools (Ansible, Chef). Use provisioners only when no other option exists.
Review the warning box in the Provisioners section.
Question 12
What provisioner type runs commands on the machine executing Terraform?
โ
Correct!
The
local-exec provisioner runs commands on the local machine where Terraform is being executed, not on the resource being created. In contrast, remote-exec runs commands on the remote resource.โ
Incorrect
The
local-exec provisioner runs commands on the local machine where Terraform is being executed, not on the resource being created. In contrast, remote-exec runs commands on the remote resource.Think about where the command executes - locally or remotely.
Question 13
What is the fundamental difference between a resource and a data source in Terraform?
โ
Correct!
Resources (
resource blocks) create and manage infrastructure - Terraform controls their lifecycle. Data sources (data blocks) only read/query existing infrastructure - Terraform just fetches information about them but doesn’t manage them.โ
Incorrect
Resources (
resource blocks) create and manage infrastructure - Terraform controls their lifecycle. Data sources (data blocks) only read/query existing infrastructure - Terraform just fetches information about them but doesn’t manage them.Think about what Terraform controls vs. what it just reads.
Question 14
Complete the code to reference a data source for an existing VPC:
Fill in the missing keyword
_____ "aws_vpc" "existing" {
id = "vpc-123456"
}
resource "aws_subnet" "main" {
vpc_id = data.aws_vpc.existing.id
}โ
Correct!
Data sources are declared with the
data keyword, not resource. The syntax is data "provider_type" "name". They allow you to fetch information about existing infrastructure.โ
Incorrect
Data sources are declared with the
data keyword, not resource. The syntax is data "provider_type" "name". They allow you to fetch information about existing infrastructure.Question 15
According to the decision tree, when should you use
for_each instead of count?โ
Correct!
Use
for_each when: 1) Items have stable identifiers that won’t change, and 2) Items will be added/removed dynamically (for_each handles this safely without recreating other resources). Use count for conditional creation (0 or 1) or when you just need a fixed number without dynamic changes.โ
Incorrect
Use
for_each when: 1) Items have stable identifiers that won’t change, and 2) Items will be added/removed dynamically (for_each handles this safely without recreating other resources). Use count for conditional creation (0 or 1) or when you just need a fixed number without dynamic changes.Think about stability and dynamic changes.
Question 16
What happens when you apply this configuration with
var.create_instance = false?variable "create_instance" {
type = bool
default = true
}
resource "aws_instance" "web" {
count = var.create_instance ? 1 : 0
ami = "ami-123"
instance_type = "t2.micro"
}What will this code output?
โ
Correct!
When
count = 0, the resource is not created (or is destroyed if it existed). The ternary var.create_instance ? 1 : 0 evaluates to 0 when false, resulting in zero instances. This is a common pattern for conditional resource creation.โ
Incorrect
When
count = 0, the resource is not created (or is destroyed if it existed). The ternary var.create_instance ? 1 : 0 evaluates to 0 when false, resulting in zero instances. This is a common pattern for conditional resource creation.What does count = 0 mean for resource creation?
Question 17
What is the purpose of
create_before_destroy in the lifecycle block?What is the purpose of
create_before_destroy in the lifecycle block?Changes resource replacement order to eliminate downtime
Default behavior: Destroy old โ Create new (causes downtime)
With create_before_destroy = true: Create new โ Destroy old (zero downtime)
This is critical for resources that must always be available, like load balancers or DNS records.
Did you get it right?
โ
Correct!
โ
Incorrect
Question 18
If you need to convert a list to work with
for_each, which function should you use?โ
Correct!
for_each requires either a map or a set, not a list. Use toset() to convert a list to a set. Example: for_each = toset(["web", "app", "db"]). You can also use tomap() if you need key-value pairs.โ
Incorrect
for_each requires either a map or a set, not a list. Use toset() to convert a list to a set. Example: for_each = toset(["web", "app", "db"]). You can also use tomap() if you need key-value pairs.for_each accepts maps or sets, not lists.
Question 19
The
ignore_changes lifecycle argument prevents Terraform from detecting any changes to the specified attributes, even in plan output.โ
Correct!
TRUE. When you use
ignore_changes = [attribute], Terraform completely ignores drift in those attributes. This is useful when attributes are managed externally (like auto-scaling capacity or tags managed by other tools). Terraform won’t show changes or attempt to update those attributes.โ
Incorrect
TRUE. When you use
ignore_changes = [attribute], Terraform completely ignores drift in those attributes. This is useful when attributes are managed externally (like auto-scaling capacity or tags managed by other tools). Terraform won’t show changes or attempt to update those attributes.Think about what ‘ignore’ means in this context.
Question 20
Which of the following statements about provisioners are correct?
โ
Correct!
Correct statements: 1)
file provisioner copies files localโremote, 2) when = destroy runs provisioners during destruction, 3) self references the current resource. INCORRECT: Provisioners are NOT idempotent by default - you must design them to be idempotent yourself.โ
Incorrect
Correct statements: 1)
file provisioner copies files localโremote, 2) when = destroy runs provisioners during destruction, 3) self references the current resource. INCORRECT: Provisioners are NOT idempotent by default - you must design them to be idempotent yourself.Think about provisioner capabilities and limitations.
Question 21
How many subnets will this code create?
variable "availability_zones" {
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
resource "aws_subnet" "public" {
count = length(var.availability_zones)
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index}.0/24"
availability_zone = var.availability_zones[count.index]
}What will this code output?
โ
Correct!
The
length() function returns 3 (the number of AZs in the list), so count = 3, creating 3 subnets. The subnets will have CIDR blocks 10.0.0.0/24, 10.0.1.0/24, and 10.0.2.0/24 using count.index.โ
Incorrect
The
length() function returns 3 (the number of AZs in the list), so count = 3, creating 3 subnets. The subnets will have CIDR blocks 10.0.0.0/24, 10.0.1.0/24, and 10.0.2.0/24 using count.index.What does length([“us-east-1a”, “us-east-1b”, “us-east-1c”]) return?
Question 22
When should you use an aliased provider with the
provider meta-argument?โ
Correct!
Use aliased providers (with the
provider meta-argument) when deploying across multiple regions, accounts, or configurations. For example, deploying resources in both us-east-1 and us-west-2 requires two provider configurations, one as default and one with an alias.โ
Incorrect
Use aliased providers (with the
provider meta-argument) when deploying across multiple regions, accounts, or configurations. For example, deploying resources in both us-east-1 and us-west-2 requires two provider configurations, one as default and one with an alias.Think about scenarios requiring different provider configurations simultaneously.
Question 23
What lifecycle argument would you use to force a resource to be replaced whenever another resource changes?
โ
Correct!
replace_triggered_by forces a resource to be replaced when specified resources or attributes change. Example: replace_triggered_by = [aws_security_group.web.id] recreates an instance whenever the security group changes.โ
Incorrect
replace_triggered_by forces a resource to be replaced when specified resources or attributes change. Example: replace_triggered_by = [aws_security_group.web.id] recreates an instance whenever the security group changes.Look for the lifecycle argument that ’triggers’ replacement based on other resources.
Question 24
Arrange these steps in the order Terraform manages a resource lifecycle:
Drag to arrange in the correct order
โฎโฎ
Create (when first applied)
โฎโฎ
Read (refresh state)
โฎโฎ
Update (when arguments change)
โฎโฎ
Delete (when removed or destroyed)
โ
Correct!
Terraform manages the complete resource lifecycle: 1) Create - when first applied, 2) Read - refresh state to detect drift, 3) Update - when configuration changes, 4) Delete - when removed from config or destroyed.
โ
Incorrect
Terraform manages the complete resource lifecycle: 1) Create - when first applied, 2) Read - refresh state to detect drift, 3) Update - when configuration changes, 4) Delete - when removed from config or destroyed.
Question 25
Data sources can have dependencies on resources, meaning Terraform will wait for the resource to be created before querying the data source.
โ
Correct!
TRUE. Data sources can depend on resources. For example, if a data source references
aws_vpc.main.id, Terraform will create the VPC first, then query the data source. This allows you to query resources you just created.โ
Incorrect
TRUE. Data sources can depend on resources. For example, if a data source references
aws_vpc.main.id, Terraform will create the VPC first, then query the data source. This allows you to query resources you just created.Think about whether data sources can reference resource attributes.
Question 26
What is the splat operator used for when working with
count?โ
Correct!
The splat operator
[*] references all instances created by count. Example: aws_instance.server[*].public_ip returns a list of all public IPs from all instances, instead of accessing them individually with [0], [1], etc.โ
Incorrect
The splat operator
[*] references all instances created by count. Example: aws_instance.server[*].public_ip returns a list of all public IPs from all instances, instead of accessing them individually with [0], [1], etc.Think about accessing all instances at once instead of individually.
Quiz Results
Score
0/0
Accuracy
0%
Right
0
Wrong
Skipped
0
Last updated on