Consider a Terraform resource with a count expression. What is the effect of setting count = var.create_resource ? 1 : 0?
variable "create_resource" { type = bool default = true } resource "aws_s3_bucket" "example" { count = var.create_resource ? 1 : 0 bucket = "my-example-bucket" acl = "private" }
Think about how count controls the number of resource instances.
The count expression controls how many instances of the resource Terraform creates. Using a conditional expression like var.create_resource ? 1 : 0 means the resource is created only when create_resource is true.
Given a list of subnet CIDRs, which Terraform expression correctly creates one subnet resource per CIDR?
variable "subnet_cidrs" { type = list(string) default = ["10.0.1.0/24", "10.0.2.0/24"] } resource "aws_subnet" "example" { count = length(var.subnet_cidrs) vpc_id = aws_vpc.main.id cidr_block = ??? }
Remember that count.index gives the current resource index.
Using count.index accesses the current index in the list, so each subnet resource gets a unique CIDR block from the list.
Which expression correctly adds an ingress rule only if var.enable_ingress is true?
variable "enable_ingress" { type = bool default = false } resource "aws_security_group" "example" { name = "example-sg" ingress = ??? }
Think about how to use a conditional expression to choose between a list and an empty list.
The conditional expression returns the ingress rule list only if enable_ingress is true; otherwise, it returns an empty list, so no ingress rules are added.
What is the output of this Terraform expression?
variable "env" { type = string default = "prod" } locals { config = { prod = "production-config" dev = "development-config" } selected_config = lookup(local.config, var.env, "default-config") }
Check the value of var.env and the keys in local.config.
The lookup function returns the value for the key matching var.env. Since var.env is "prod", it returns "production-config".
Which statement best explains why Terraform expressions add logic to infrastructure definitions?
Think about how expressions influence resource behavior based on inputs.
Terraform expressions let you write logic that changes what resources are created or how they are configured depending on inputs, making your infrastructure code adaptable and reusable.