Medium Terraform Associate practice questions
Applied — put a concept to work in a realistic situation. 132 medium questions available — no sign-up, always free.
A team wants an output that displays a custom DNS name when the variable custom_dns is set to a non-empty string, but otherwise falls back to the auto-generated endpoint from an AWS resource attribute. They want to avoid nested conditional expressions. Which built-in function best returns the first non-empty value from a list of arguments?
A team wants an aws_cloudwatch_metric_alarm to be created only when a boolean variable named enable_monitoring is set to true. When enable_monitoring is false, no alarm resource should exist. Which configuration correctly implements this toggle using the count meta-argument?
A developer manages three AWS S3 buckets in a single Terraform configuration. To decommission just one bucket, they delete its resource block from the .tf file and run `terraform apply`. The other two buckets must remain untouched. What will Terraform do?
You are writing HCL to create three EC2 instances from a variable named `instance_names`, which is a list of strings. You want each instance's `Name` tag to match the corresponding string in the list. Which configuration correctly assigns the right name to each instance?
Your team must always deploy EC2 instances using the most recent official Ubuntu AMI, which changes frequently. You do not manage the AMI itself with Terraform, but you need its ID available in your configuration so the aws_instance resource can reference it. Which HCL construct should you use to obtain the AMI ID?
A DevOps engineer writes Terraform configuration to provision three EC2 instances. After running 'terraform apply' successfully, a teammate runs 'terraform apply' again without changing any code or infrastructure. What behavior should the engineer expect, and which IaC principle explains it?
A new team member argues that Terraform configurations should list the exact sequence of API calls needed to build infrastructure, similar to a shell script. A senior engineer explains that Terraform does not work this way. Which statement best describes how Terraform's approach differs from the shell-script method the new member described?
An engineer manages an AWS EC2 instance that runs a startup script needing an IAM policy to be fully attached before the instance boots. The instance configuration does not reference any attribute of the IAM policy resource, so Terraform sometimes creates them in parallel, causing the script to fail. Which change should the engineer make to guarantee correct ordering?
You are writing a security group resource that must produce a variable number of nested `ingress` blocks based on a list of port objects passed as `var.ingress_rules` (each with `port` and `cidr` attributes). You want to avoid hardcoding repeated `ingress { ... }` blocks. Which construct correctly generates the nested blocks?
You are writing a module that creates 6 EC2 instances using count, and you want to distribute them across a list of 3 availability-zone subnet IDs stored in var.subnet_ids. You need each instance's subnet_id to cycle through the 3 subnets (instance 0 → subnet 0, instance 3 → subnet 0 again, etc.) without going out of bounds. Which expression should you assign to subnet_id?
A teammate wrote the following resource block and Terraform returns an error during plan: ```hcl variable "buckets" { type = list(string) default = ["logs", "backups", "assets"] } resource "aws_s3_bucket" "this" { for_each = var.buckets bucket = each.value } ``` The error states the `for_each` argument must be a map or a set of strings, not a list. Which change fixes the configuration while keeping the same input variable?
A platform engineer wants to guarantee that an AWS S3 bucket created by Terraform always has versioning explicitly enabled before the apply is considered valid. They want the apply to fail with a clear error if the resulting resource does not meet this requirement, evaluated against the resource's own attributes after planning. Which Terraform feature should they use?
A team manages several S3 buckets using a single resource block. They currently use `count` with a list of bucket names in a variable. When they remove a name from the middle of the list and run `terraform plan`, Terraform proposes destroying and recreating multiple buckets that were not intended to change. Which change to the configuration best prevents this behavior while still iterating over the collection?
You are given a variable named subnet_names of type list(string) containing three unique subnet names. You want to create one aws_subnet resource per name using for_each so that resources are keyed by name rather than by numeric index. Which configuration correctly uses for_each with this list variable?
You are creating multiple S3 buckets from a map variable that pairs a logical name with an environment tag. Inside a resource block using for_each over this map, you need to set the bucket name from the map key and apply the environment as a tag from the map value. Which pair of references correctly retrieves the key and value for each iteration?
You maintain a list of instance objects and need an output that contains only the private IP addresses of instances whose 'role' attribute equals "web". The variable is defined as a list of objects, each with 'role' and 'private_ip' attributes. Which expression produces the desired filtered list?
You have a variable `environments = ["dev", "staging", "prod"]` and need to produce a local value that is a map where each key is the environment name and each value is the environment name converted to uppercase (e.g., {"dev" = "DEV"}). Which local expression produces this result?
Your team currently stores Terraform state in a file on a shared network drive that everyone edits from their laptops. You have experienced overwritten state files, lost updates, and exposed credentials stored in plaintext state. Leadership asks you to migrate to HCP Terraform as the remote backend. Which combination of benefits BEST justifies this migration compared to the current setup?
A development team currently spins up identical staging environments by hand each time a new feature branch needs testing. This process takes several hours, and engineers often forget individual configuration steps, leading to environments that behave differently from one another. The team lead wants to adopt Infrastructure as Code specifically to solve the problem of recreating the same environment quickly and reliably on demand. Which IaC advantage most directly addresses this goal?
A platform engineering team currently provisions infrastructure by having individual engineers run shell scripts from their laptops. Changes are frequently applied without any peer review, and the team struggles to understand who changed what and why. Leadership wants to adopt Infrastructure as Code specifically to improve how proposed infrastructure changes are reviewed and discussed before they take effect. Which advantage of IaC most directly addresses this goal?
A platform team manually patches a production web server's firewall rules during an incident, bypassing the Terraform configuration that originally provisioned it. Weeks later, a developer runs `terraform plan` against that infrastructure. Which advantage of Infrastructure as Code most directly helps the team identify this out-of-band change?
A development team repeatedly encounters bugs that appear only in production but cannot be reproduced in their staging environment. Investigation reveals that staging was built by one engineer clicking through the cloud console, while production was set up months earlier by a different engineer using slightly different settings. Which core advantage of Infrastructure as Code most directly addresses this problem?
A finance team complains that cloud spending is unpredictable because operations engineers provision resources ad hoc through cloud consoles, and no one can forecast costs before changes are applied. The engineering lead wants to adopt Infrastructure as Code specifically to help address this concern. Beyond the raw cost tooling, which inherent characteristic of a declarative IaC workflow BEST helps the team review the exact set of resource changes before they are provisioned?
A new engineer on your platform team asks how Terraform decides what actions to take during an apply. Your team writes configuration files that describe the final set of resources they want (three servers, one load balancer, one database) without listing the individual API calls or ordering steps needed to build them. Which characteristic of Terraform's declarative model best explains how this works?
A company's primary data center suffers a catastrophic failure, destroying all running infrastructure. The operations team needs to rebuild the entire environment in a secondary region as quickly as possible. Their infrastructure is fully defined in Terraform configuration files stored in a Git repository. Which advantage of Infrastructure as Code most directly enables the team to recreate this environment reliably?