First 50 of 199 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1Infrastructure as Code (IaC) with Terraform
What is the main purpose of Terraform state?
- To keep a version control history of every configuration change made
- To cache plan output so that teammates can reuse it later
- To track which real infrastructure objects each declared resource represents Correct answer.
- To store the provider credentials used during the most recent apply run
Explanation
State is Terraform's record of the objects it created and the attribute values it last observed, keyed by resource address. Every plan compares configuration against this record to decide what to create, update, or delete. Credentials and version history live elsewhere, not in state.
Question 2Infrastructure as Code (IaC) with Terraform
Which command initializes a working directory by downloading the required providers and modules?
- terraform plan
- terraform validate
- terraform init Correct answer.
- terraform apply
Explanation
terraform init prepares a working directory: it installs provider plugins, downloads referenced modules, and configures the backend. It must be run before plan or apply in a new directory or after changing provider or module requirements.
Question 3Infrastructure as Code (IaC) with Terraform
Why is Terraform described as a declarative tool?
- Because it only reads existing infrastructure and never changes it
- Because it requires you to write ordered steps for each resource
- Because you describe the end state and Terraform derives the actions Correct answer.
- Because it records every command you run in a script file
Explanation
In a declarative model you write the desired end state of the infrastructure, and Terraform computes an ordered set of actions from the dependency graph. You do not script the individual creation steps. This is what makes the same configuration idempotent across repeated runs.
Question 4Infrastructure as Code (IaC) with Terraform
What does terraform plan do?
- It validates the provider credentials against the remote platform API
- It applies the configuration immediately without asking for approval
- It previews the changes needed to reach the desired state Correct answer.
- It deletes every resource that is currently recorded in the state file
Explanation
terraform plan refreshes known objects, compares them with the configuration, and prints the actions it would take. It does not modify infrastructure. The resulting plan can be saved and later applied.
Question 5Infrastructure as Code (IaC) with Terraform
What is a Terraform provider?
- A file that stores the current state of managed infrastructure
- A plugin that manages resources through a platform API Correct answer.
- A script that runs on a new server after it is created
- A module published in the public registry of reusable configurations
Explanation
A provider is a plugin that maps resource types and data sources onto the API of a target platform, such as a cloud or a SaaS service. Providers are installed by terraform init and declared with source and version requirements.
Question 6Infrastructure as Code (IaC) with Terraform
Which command removes all objects managed by the current configuration?
- terraform init -upgrade
- terraform state rm
- terraform destroy Correct answer.
- terraform plan
Explanation
terraform destroy plans and then deletes every object tracked in state for the current configuration, after confirmation. It is equivalent to planning with a destroy flag and applying the result.
Question 7Infrastructure as Code (IaC) with Terraform
What is a Terraform module?
- A file that records the provider version constraints
- A command-line flag that groups plan output by resource
- A single resource block with no dependencies
- A reusable set of configuration files in a directory Correct answer.
Explanation
A module is any directory containing Terraform configuration files, and it becomes reusable when called from another configuration with a module block. The root configuration is itself a module. Modules can be local paths or downloaded from a registry.
Question 8Infrastructure as Code (IaC) with Terraform
Which block declares an input variable named region?
- variable "region" { default = "us-east-1" } Correct answer.
- locals { region = "us-east-1" }
- provider "region" { value = "us-east-1" }
- output "region" { value = "us-east-1" }
Explanation
The variable block declares an input that a caller can supply, with an optional default. References use var.region. Outputs, locals, and providers use different block types and serve different purposes.
Question 9Infrastructure as Code (IaC) with Terraform
Why is state locking important when a backend supports it?
- It keeps a backup of each previous state version automatically
- It compresses the state file before uploading it to the backend
- It encrypts the state file so that operators cannot read resource attributes
- It stops two runs from writing to the same state concurrently Correct answer.
Explanation
A lock ensures only one operation writes state at a time, preventing lost updates and corruption when several people or pipelines run Terraform. If a lock cannot be acquired, Terraform stops the run rather than proceeding unsafely.
Question 10Infrastructure as Code (IaC) with Terraform
What does terraform import do?
- It copies the state file from one backend to another
- It adds an existing resource to state so Terraform manages it Correct answer.
- It downloads a module from the public registry into the working directory
- It reads provider schemas and writes them to the lock file
Explanation
terraform import takes an existing real object's identifier and records it at a given resource address in state, after which Terraform treats it as managed. It does not modify the object itself, and the configuration must already describe the resource to keep the plan clean.
Question 11Infrastructure as Code (IaC) with Terraform
Why is for_each often preferred over count for a set of similar resources?
- It creates the resources in parallel, while count creates them serially
- Instances are keyed by value, so removing one item leaves the others intact Correct answer.
- It allows each instance to use a different provider configuration
- It does not require the values to be known at plan time
Explanation
With for_each, each instance is addressed by a stable key such as a map key or set member, so inserting or removing an item does not renumber the remaining instances. count addresses instances by position, so removing a middle element shifts the indexes and can cause unrelated resources to be replaced.
Question 12Infrastructure as Code (IaC) with Terraform
What is a main benefit of a remote backend such as an object storage bucket?
- It converts the configuration into a declarative language
- It gives teams shared and locked access to one state file Correct answer.
- It removes the need to run terraform init
- It stores the provider credentials so they need not be configured
Explanation
A remote backend stores state outside the local machine, so everyone works from the same copy and can rely on locking to avoid concurrent writes. Many backends also keep version history, allowing recovery of earlier state.
Question 13Infrastructure as Code (IaC) with Terraform
What does terraform validate check?
- Syntax and internal consistency of the configuration, without remote API calls Correct answer.
- Whether the provider plugins match the lock file checksums
- Whether the real infrastructure still matches the state file
- Whether the plan can be applied without any resource replacement
Explanation
validate parses the configuration, resolves references between blocks, and reports errors such as invalid argument names or missing required values. It runs offline and does not compare against real infrastructure. Checksums are verified during init.
Question 14Infrastructure as Code (IaC) with Terraform
How does Terraform infer that resource B must be created after resource A?
- Because A declares depends_on for B
- Because both resources are declared in the same file
- Because B's arguments reference an attribute of A Correct answer.
- Because A appears earlier in the directory listing
Explanation
Any expression that references another resource's attribute creates an implicit dependency edge in the graph, so the referenced resource is handled first. File layout and declaration order do not establish dependencies.
Question 15Infrastructure as Code (IaC) with Terraform
What is the role of a locals block?
- It declares values that the user must supply at apply time
- It marks values so they are hidden from CLI output
- It exports values for other configurations to read from state
- It names an expression that is reused within the module Correct answer.
Explanation
Locals assign a name to a computed expression so it can be referenced as local.<name> in several places without repetition. They are not inputs and cannot be set by a caller. Values are evaluated from the configuration, variables, and resource attributes.
Question 16Infrastructure as Code (IaC) with Terraform
What is the purpose of saving a plan with terraform plan -out=tfplan?
- It records a backup copy of the current state before any change
- It uploads the plan to the remote backend for automatic approval
- It lets the reviewed plan be applied exactly as shown Correct answer.
- It locks the state so no other run can start until it is applied
Explanation
A saved plan file captures the exact set of actions and is applied with terraform apply tfplan without recomputing them. This suits review workflows where the approved plan is the one that runs. Applying a saved plan does not permit new variables or a different target.
Question 17Infrastructure as Code (IaC) with Terraform
What does terraform state rm do?
- It drops a resource from state but leaves the real object intact Correct answer.
- It marks a resource for replacement on the next apply
- It destroys the object and then removes it from state
- It deletes the entire state file from the backend
Explanation
state rm removes the given address from the state file so Terraform stops tracking it, leaving the real object untouched and unmanaged. It is commonly used when handing a resource over to another configuration. It does not call the provider to delete anything.
Question 18Infrastructure as Code (IaC) with Terraform
Where do you set a version constraint for a provider?
- In the .terraform.lock.hcl file by hand
- In the required_providers block of a terraform block Correct answer.
- In the variable block for the provider
- In the provider block's version argument
Explanation
Provider source addresses and version constraints are declared in the terraform block's required_providers map. Terraform selects a version satisfying those constraints and records the choice in the dependency lock file. Older versions of Terraform accepted a version argument in the provider block, which is now deprecated.
Question 19Infrastructure as Code (IaC) with Terraform
What effect does setting sensitive = true on an input variable have?
- It prevents the value from ever being sent to the provider
- It encrypts the value in both the state file and the plan file
- It forces the value to be supplied through an environment variable
- It redacts the value in CLI output but keeps it in state Correct answer.
Explanation
The sensitive flag suppresses the value in plan and apply output so it is not displayed in logs or terminals. The value is still written to state, which is why state storage must be protected. Encryption is a backend concern, not something this flag provides.
Question 20Infrastructure as Code (IaC) with Terraform
Which command rewrites configuration files into Terraform's canonical formatting?
- terraform state push
- terraform fmt Correct answer.
- terraform validate
- terraform plan -refresh-only
Explanation
terraform fmt reformats files to the standard style, such as aligning equals signs and normalizing indentation. It edits files in place unless run with -check or -diff. It does not evaluate expressions or contact providers.
Question 21Infrastructure as Code (IaC) with Terraform
You want to see whether real infrastructure has drifted from configuration without changing any infrastructure. Which command fits best?
- terraform plan -refresh-only Correct answer.
- terraform destroy -refresh=false
- terraform apply -auto-approve
- terraform state pull
Explanation
A refresh-only plan updates the in-memory view of real objects and reports discrepancies, then proposes only state updates rather than infrastructure changes. Approving it would write refreshed values to state without altering resources. Other commands either change infrastructure or do not compare it with configuration.
Question 22Infrastructure as Code (IaC) with Terraform
You renamed a resource block in configuration while the real object still exists. What keeps the existing object instead of replacing it?
- terraform refresh after the rename
- terraform state mv from the old address to the new one Correct answer.
- terraform import using the new address
- terraform apply -replace with the new address
Explanation
state mv rewrites the address of an existing state entry, so the renamed block now maps to the same real object and no create or destroy is planned. It can also move entries between modules or into a child module. Importing is for objects not yet in state.
Question 23Infrastructure as Code (IaC) with Terraform
Which lifecycle setting creates a replacement object before the existing one is destroyed?
- ignore_changes = [tags]
- create_before_destroy = true Correct answer.
- prevent_destroy = true
- replace_triggered_by = [aws_instance.web]
Explanation
create_before_destroy reverses the default destroy-then-create order so the new object is provisioned first, which reduces downtime for resources that must always exist. It requires the provider to allow two objects with the same identifying name. The other settings control protection, attribute drift, and replacement triggers.
Question 24Infrastructure as Code (IaC) with Terraform
What is the role of the .terraform.lock.hcl file?
- It stores the outputs of the last successful apply
- It lists the modules installed in the .terraform directory
- It records the state file's serial number and lineage
- It pins provider versions and records checksums for verification Correct answer.
Explanation
The dependency lock file records the exact provider versions selected and their checksums so later runs on other machines install identical packages. It should be committed to version control. Provider upgrades are then deliberate, done with terraform init -upgrade.
Question 25Infrastructure as Code (IaC) with Terraform
Which command schedules a single resource for replacement without editing the configuration?
- terraform state rm aws_instance.web
- terraform plan -refresh-only
- terraform destroy -target=aws_instance.web
- terraform apply -replace=aws_instance.web Correct answer.
Explanation
The -replace option on plan or apply tells Terraform to plan a destroy-and-create cycle for the named address in that run only, leaving the configuration unchanged. The older taint and untaint commands served this purpose and are deprecated. The other commands either stop tracking, skip changes, or delete without recreating.
Question 26Terraform fundamentals
What is the main effect of running `terraform init` in a working directory that contains Terraform configuration?
- Apply the configuration to the target platform
- Install the required providers and modules Correct answer.
- Generate an execution plan for the pending changes
- Destroy the resources tracked in the state file
Explanation
Initialization prepares a working directory for use: it downloads provider plugins, installs referenced modules, and configures the backend that stores state. It does not compute or apply any infrastructure changes. Plan and apply are separate commands run after initialization completes.
Question 27Terraform fundamentals
Which command rewrites configuration files into Terraform's canonical formatting and indentation?
- terraform validate
- terraform fmt Correct answer.
- terraform show
- terraform plan
Explanation
The `terraform fmt` command reformats configuration files to the canonical style Terraform expects, adjusting alignment and indentation. It changes file content rather than reporting errors or producing plans.
Question 28Terraform fundamentals
By default, which filename does Terraform use for local state in the working directory?
- variables.tf
- terraform.tfstate Correct answer.
- main.tf
- terraform.tfvars
Explanation
When no remote backend is configured, Terraform stores state locally in a file named `terraform.tfstate` in the working directory. This file records the mapping between configuration addresses and real remote objects.
Question 29Terraform fundamentals
Which configuration block declares an input value that callers can supply to the configuration?
- resource
- variable Correct answer.
- output
- locals
Explanation
A `variable` block declares an input parameter, optionally with a type, default, and description, whose value is referenced as `var.<name>`. Values come from files, environment variables, or command-line flags.
Question 30Terraform fundamentals
Which command previews the changes Terraform would make without performing them?
- terraform validate
- terraform apply
- terraform plan Correct answer.
- terraform refresh
Explanation
`terraform plan` compares configuration against recorded state and current remote objects, then prints the actions it would take. Passing a plan to `terraform apply` is what actually makes changes.
Question 31Terraform fundamentals
What does `terraform validate` confirm about a configuration?
- That the state file is encrypted at rest
- That every provider plugin is already installed in the working directory
- That the files are syntactically valid and internally consistent Correct answer.
- That the remote objects match the configuration exactly
Explanation
Validation parses the configuration and checks references, argument names, and types for internal consistency without contacting any remote service. It therefore cannot detect drift or confirm that plugins are installed.
Question 32Terraform fundamentals
A managed resource is declared as `resource "aws_instance" "web" {}`. How is its computed ID referenced elsewhere in the same configuration?
- aws_instance.web[id]
- resource.aws_instance.web.id
- aws_instance.web.id Correct answer.
- var.aws_instance.web.id
Explanation
Splatted references are not used here: an attribute is reached with the resource type, the local name, and the attribute separated by dots, as in `aws_instance.web.id`. The type and name together form the resource address, and the trailing element names an exported attribute.
Question 33Terraform fundamentals
What does `terraform destroy` do to the infrastructure described by the configuration?
- Deletes the configuration files from the working directory
- Deletes the managed infrastructure recorded in state Correct answer.
- Removes provider plugins from the plugin cache
- Forgets resources from state without touching real infrastructure
Explanation
Destroy builds a plan to remove every object managed by the current configuration and state, then calls the providers to delete those objects. The configuration files themselves remain on disk.
Question 34Terraform fundamentals
A variable is declared as `variable "region" {}`. How is its value referenced inside the configuration?
- variable.region
- local.region
- ${region}
- var.region Correct answer.
Explanation
Input variables are read through the `var` namespace followed by the declared name, for example `var.region`. The `local` namespace is reserved for values defined in a `locals` block.
Question 35Terraform fundamentals
What is the role of an `output` block?
- It exposes a value from the configuration for display or reuse Correct answer.
- It configures provider-specific settings for the platform
- It prevents the state file from being modified
- It declares a value that the user must supply before apply
Explanation
Outputs publish selected values after an apply, showing them in the CLI and making them available to parent modules or remote state consumers. They never accept input values.
Question 36Terraform fundamentals
What is a data source used for in Terraform configuration?
- Storing sensitive values outside of state
- Reading information about objects Terraform does not manage Correct answer.
- Creating a new managed object in the target platform
- Declaring a reusable child configuration
Explanation
Data sources query a provider for facts about existing infrastructure, such as an existing VPC's ID or the latest machine image, and expose the results as read-only attributes. They do not create, update, or delete anything.
Question 37Terraform fundamentals
Which file does Terraform load automatically to supply input variable values without any command-line flag?
- main.tf
- terraform.tfstate
- terraform.tfvars Correct answer.
- variables.tf
Explanation
Terraform automatically reads `terraform.tfvars` and files named `*.auto.tfvars` from the working directory to populate input variables. Other filenames must be passed explicitly with `-var-file`.
Question 38Terraform fundamentals
What does the `create_before_destroy` lifecycle setting change about a replacement?
- The object is recreated on every apply regardless of changes
- Creation is deferred until all other resources are destroyed
- The replacement object is provisioned before the existing object is removed Correct answer.
- The object becomes permanently exempt from destruction
Explanation
By default Terraform destroys the old object first; `create_before_destroy` reverses that order so the new object exists before the old one is removed, reducing downtime. It does not force replacement on its own.
Question 39Terraform fundamentals
Two resources have no attribute reference between them, but one must be created before the other. What expresses that ordering?
- Add a lifecycle block with ignore_changes
- Place both resources in the same module
- Reference the other resource inside a local value
- Use the depends_on meta-argument Correct answer.
Explanation
Terraform derives ordering from references; when no reference exists, `depends_on` declares the dependency explicitly by resource address. Modules and locals do not by themselves create an ordering edge between two unrelated resources.
Question 40Terraform fundamentals
What is the primary purpose of Terraform state?
- To map declared resources to real remote objects Correct answer.
- To record which users ran each apply
- To cache provider API responses for faster plans
- To keep a backup copy of the configuration files
Explanation
State stores the binding between configuration addresses and real object identifiers, along with attribute values and dependency metadata. Terraform relies on it to compute diffs and decide what to create, update, or delete.
Question 41Terraform fundamentals
Why does Terraform lock state during operations that write to it?
- To give priority to interactive runs over CI runs
- To encrypt sensitive attribute values on disk
- To prevent concurrent writes to the same state Correct answer.
- To compress the state file before uploading it
Explanation
Locking ensures only one Terraform process at a time can modify a given state, protecting against corruption and conflicting applies. The lock is released when the operation completes.
Question 42Terraform fundamentals
Why save a plan with `terraform plan -out=tfplan` and then run `terraform apply tfplan`?
- To let the plan keep running after the apply finishes
- To write the plan into state without creating resources
- To apply exactly the changes that were reviewed in that plan Correct answer.
- To apply changes without consulting any state file
Explanation
A saved plan captures the precise set of actions computed at review time, and applying it executes exactly those actions. Terraform also refuses to apply a saved plan if state has changed since it was created.
Question 43Terraform fundamentals
What is the job of a provider block such as `provider "aws" { region = "us-east-1" }`?
- To store cloud credentials inside the state file
- To configure the plugin used to talk to that platform Correct answer.
- To name the workspace used for deployments
- To declare an AWS resource that Terraform will manage
Explanation
Provider blocks supply settings such as region, endpoint, or authentication options to the plugin that implements resource types. The plugin then handles all API calls for those types.
Question 44Terraform fundamentals
Which argument creates one resource instance per element of a map, with each instance addressable by its key?
- depends_on
- count
- for_each Correct answer.
- dynamic
Explanation
`for_each` accepts a map or set and produces one instance per element, reachable via `each.key` and `each.value`. Instances are addressed by key, so removing one element does not shift the others.
Question 45Terraform fundamentals
A resource was renamed in configuration, so its state entry still holds the old address. Which command updates the state to the new address without destroying anything?
- terraform apply -refresh-only
- terraform state mv Correct answer.
- terraform state rm
- terraform refresh
Explanation
`terraform state mv` rewrites an object's address in state, for example from the old resource name to the new one. Because the object is not destroyed, it continues to be managed under the new address.
Question 46Terraform fundamentals
Which lifecycle setting makes a plan fail when the resource it belongs to would be destroyed?
- create_before_destroy = true
- ignore_changes = all
- deletion_protection = true
- prevent_destroy = true Correct answer.
Explanation
`prevent_destroy` causes Terraform to error during planning whenever the resource would be destroyed, acting as a guard against accidental deletion. Any such destruction must be explicitly allowed by removing the setting first.
Question 47Terraform fundamentals
In Terraform's architecture, which component actually makes the API calls that create and update remote objects?
- Terraform Core, which talks to each platform directly
- The backend, which proxies requests to the target platform
- The registry, which brokers connections to cloud APIs
- The provider plugins invoked by Core Correct answer.
Explanation
Core is responsible for parsing configuration, building the dependency graph, and computing diffs, but every create, read, update, and delete is delegated to a provider plugin. Providers implement a defined interface that Core calls.
Question 48Terraform fundamentals
What must be true before `terraform import aws_instance.web i-0123456789abcdef0` can succeed?
- The resource block must already be in the configuration Correct answer.
- The state must be stored in a backend that supports locking
- The object must already be recorded in state
- The object must first be deleted by Terraform
Explanation
Import attaches an existing object to an address that must already be declared in configuration, so the resource block has to exist. Import only writes the object to state; it does not generate configuration for it.
Question 49Terraform fundamentals
Which argument to `terraform apply` marks a single existing object for replacement without editing the configuration?
- -target=aws_instance.web
- -refresh=false
- -replace=aws_instance.web Correct answer.
- -parallelism=1
Explanation
The `-replace` flag tells Terraform to plan destruction and re-creation of the named address in the current run, replacing the older taint workflow. It affects only the specified address, leaving the rest of the plan intact.
Question 50Terraform fundamentals
How does a configuration call a child module stored in a local directory?
- module "vpc" { source = "./modules/vpc" } Correct answer.
- resource "module" "vpc" { source = "./modules/vpc" }
- include "vpc" { path = "./modules/vpc" }
- import "vpc" from "./modules/vpc"
Explanation
A `module` block with a `source` argument instantiates a child module, and its outputs are read through `module.vpc.<output>`. The source may be a local path, a registry address, or a VCS URL.