Top Terraform CLI Commands, State Management, and Configuration Logic Questions for Interview
Prepare for your Terraform and DevOps interviews with these carefully selected Terraform CLI Commands, State Management, and Configuration Logic MCQs. This set covers core Terraform CLI Commands, State Management, and Configuration Logic. Each question includes clear answers and explanations to help you strengthen your understanding and boost interview confidence.
By the end of this article, you’ll be well-equipped to handle a variety of Terraform CLI Commands, State Management, and Configuration Logic interview questions and demonstrate your proficiency in automating IT tasks efficiently and effectively.
1. Which command is used to mark a specific resource instance as not fully functional ?
A) terraform delete
B) terraform taint
C) terraform untaint
D) terraform mark
Correct Answer: B
Explanation: The taint command manually marks a resource as “dirty,” forcing it to be destroyed and recreated on the next apply. This is useful when a resource has failed or is in an undesirable state.
2. How can you skip the interactive “yes” prompt during terraform apply ?
A) –force
B) -auto-approve
C) -confirm=false
D) -silent
Correct Answer: B
Explanation: Using the -auto-approve flag allows changes to be applied immediately without manual user confirmation. This is commonly used in automated CI/CD pipelines.
3. What is the default filename for the state file ?
A) terraform.tfstate
B) main.tfstate
C) config.tfstate
D) state.json
Correct Answer: A
Explanation: Terraform automatically names its local state file terraform.tfstate. It also creates a terraform.tfstate.backup file during operations as a safety measure.
4. Which command reloads the state file with the latest changes from the cloud without changing resources ?
A) terraform apply -refresh-only
B) terraform reload
C) terraform refresh
D) terraform update
Correct Answer: C
Explanation: The refresh command synchronizes the local state file with the real-world infrastructure to ensure accurate planning. While it is now mostly automated within plan and apply, it can still be run manually.
5. Which keyword is used to create local variables within a module to reduce repetition ?
A) variable
B) local
C) locals
D) define
Correct Answer: C
Explanation: The locals block defines internal values that can be reused throughout a module. They act like temporary variables within a function’s scope to improve readability.
6. What does the -target flag do when used with terraform apply ?
A) It limits the apply operation to a specific resource or module
B) It specifies which cloud region to target
C) It targets a specific developer for notifications
D) It sets the target budget for the infrastructure
Correct Answer: A
Explanation: The -target flag allows you to apply changes only to a specific part of your configuration, which is useful for urgent fixes. However, it should be used with caution to avoid state corruption.
7. Which command would you use to move an item within the state file ?
A) terraform state mv
B) terraform state move
C) terraform mv
D) terraform migrate
Correct Answer: A
Explanation: The state mv subcommand is used to rename or move items in the state file, such as when you rename a resource in your HCL code. This ensures Terraform knows the resource still exists under a new name.
8. How do you mark an output as hidden from the console log ?
A) private = true
B) sensitive = true
C) hidden = true
D) secret = true
Correct Answer: B
Explanation: Setting the sensitive attribute to true in an output block prevents the value from being displayed in the terminal during an apply. It is used to protect passwords or API keys.
9. What is a “.tfvars” file used for ?
A) To store Terraform binary settings
B) To list all the providers required by the project
C) To document the purpose of different cloud regions
D) To assign values to input variables in a structured way
Correct Answer: D
Explanation: A .tfvars file contains variable assignments that Terraform can automatically load to populate the values defined in your .tf files. This helps separate environment-specific settings from the core logic.
10. What is the command to create a new workspace ?
A) terraform workspace create <name>
B) terraform new workspace <name>
C) terraform workspace new <name>
D) terraform workspace add <name>
Correct Answer: C
Explanation: The workspace new command creates a fresh workspace with its own separate state file. This is helpful for managing different stages like “development” or “production”.
11. Which command lists all resources currently tracked in the state file ?
A) terraform state list
B) terraform show list
C) terraform resource list
D) terraform list
Correct Answer: A
Explanation: The state list subcommand provides a simple list of all resource addresses managed by the current state. It is a quick way to see what Terraform is currently tracking.
12. In HCL, how do you specify multiple instances of a resource using a map or set ?
A) count
B) for_each
C) iterate
D) loop
Correct Answer: B
Explanation: The for_each meta-argument creates a resource instance for each item in a map or set of strings. It is more flexible than count when creating non-identical resources.
13. Which command clears an active state lock manually ?
A) terraform unlock
B) terraform release-lock
C) terraform force-unlock
D) terraform break-lock
Correct Answer: C
Explanation: If a Terraform run is interrupted and the lock remains “stuck,” you can use force-unlock with the provided lock ID to release it. This should be used carefully to avoid conflicts.
14. What is the purpose of the terraform validate command ?
A) To confirm that the cloud credentials are correct
B) To check the configuration for syntax and internal logic errors
C) To verify that all servers are currently running
D) To format the code according to HCL standards
Correct Answer: B
Explanation: validate is a local check that ensures the syntax is correct and that required arguments are present before you run a plan. It does not interact with remote APIs.
15. What is the terraform console command used for ?
A) To access an interactive environment for evaluating HCL expressions
B) To open a support ticket with HashiCorp
C) To view the live terminal of an AWS instance
D) To manage Terraform Enterprise users
Correct Answer: A
Explanation: The console command allows you to test HCL code, math, and resource parameters in real-time. It is an excellent tool for experimenting with expressions.
16. How can you set a variable value using an environment variable ?
A) Set an environment variable named VARIABLE_name
B) You cannot use environment variables for Terraform
C) Set an environment variable named TERRAFORM_name
D) Set an environment variable named TF_VAR_name
Correct Answer: D
Explanation: Terraform automatically reads any environment variable prefixed with TF_VAR_ to populate the corresponding variable in your code. For example, TF_VAR_region maps to the region variable.
17. Which command generates a visual dependency graph ?
A) terraform graph
B) terraform dependency-map
C) terraform tree
D) terraform visualize
Correct Answer: A
Explanation: terraform graph outputs a DOT-formatted graph of your resources and their relationships. This can be used with tools like Graphviz to create visual diagrams.
18. Which keyword allows you to refer to attributes of other resources ?
A) reference
B) link
C) interpolation (Implicit reference)
D) pointer
Correct Answer: C
Explanation: By referencing one resource’s attribute (like aws_instance.web.id) in another resource’s configuration, you create an implicit dependency. This tells Terraform the correct order to create resources.
19. What does the terraform state rm command do ?
A) It deletes the actual resource from the cloud
B) It removes a resource from the state file without deleting it from the cloud
C) It removes the entire state file from the project
D) It removes a specific version of a module
Correct Answer: B
Explanation: state rm is used when you want Terraform to stop managing a resource but you do not want to destroy the resource in the cloud. This is useful for migrating or manually managing specific items.
20. Which command is used to show the current version and check for updates ?
A) terraform status
B) terraform check
C) terraform version
D) terraform update
Correct Answer: C
Explanation: The version command displays the current Terraform version and alerts you if a newer one is available for download.
21. What is a “Null Resource” in Terraform ?
A) A resource that follows the lifecycle but does nothing in the cloud
B) A resource that has been deleted
C) A placeholder for a cloud provider that hasn’t been added yet
D) A resource that contains no data
Correct Answer: A
Explanation: A null_resource behaves like a normal resource but doesn’t actually create any infrastructure. It is often used as a container to trigger provisioners.
22. What is the command to list all the current workspaces ?
A) terraform list workspaces
B) terraform workspaces
C) terraform show workspaces
D) terraform workspace list
Correct Answer: D
Explanation: The workspace list command shows all available workspaces in the current project, highlighting the active one.
23. Which file extension is used for HCL configuration files ?
A) .hcl
B) .tf
C) .terraform
D) .conf
Correct Answer: B
Explanation: Terraform configuration files use the .tf extension. Files named with .tf.json are also recognized for JSON configurations.
24. How do you define an explicit dependency between two resources ?
A) depends_on
B) after_resource
C) wait_for
D) needs
Correct Answer: A
Explanation: The depends_on meta-argument is used to force an execution order when Terraform cannot automatically infer the dependency. It tells Terraform to finish one resource before starting another.
25. What is the command to switch to a different workspace ?
A) terraform workspace change <name>
B) terraform workspace select <name>
C) terraform workspace switch <name>
D) terraform use workspace <name>
Correct Answer: B
Explanation: The workspace select command is used to switch the active context to a different workspace.
26. Which command is used to see the details of a single resource in the state file ?
A) terraform show <address>
B) terraform state view <address>
C) terraform state show <address>
D) terraform inspect <address>
Correct Answer: C
Explanation: The state show subcommand provides the exact current attributes of a specific resource tracked in the state file.
27. What does the -recursive flag do with terraform fmt ?
A) It formats the code over and over until it is perfect
B) It only formats files that have changed recently
C) It deletes all files and starts over
D) It formats configuration files in the current directory and all subdirectories
Correct Answer: D
Explanation: Using terraform fmt -recursive ensures that your entire project structure, including modules in subfolders, follows the standard style.
28. What is the purpose of the “provider alias” ?
A) To give a provider a shorter name
B) To manage multiple instances of the same provider (e.g., in different regions)
C) To hide the identity of the cloud provider for security
D) To allow two different developers to use the same provider
Correct Answer: B
Explanation: Aliases allow you to configure a provider multiple times with different settings (like different AWS regions) within the same HCL project.
29. Which command can produce JSON output for downstream automation tools ?
A) terraform apply -json
B) terraform json-output
C) terraform output -json
D) terraform export -json
Correct Answer: C
Explanation: Both terraform output -json and terraform show -json can be used to generate machine-readable data that CI/CD pipelines or scripts can parse.
30. What is a “Taint” in Terraform terms ?
A) A manual marker on a resource that tells Terraform to recreate it
B) A security vulnerability in the code
C) A mistake in the HCL syntax
D) A corrupted part of the state file
Correct Answer: A
Explanation: When you “taint” a resource, you tell Terraform that the next time you run an apply, it must delete that resource and create a new one from scratch.
31. Which command allows you to view the outputs of a specific state file by path ?
A) terraform output -state=<path>
B) terraform read-output <path>
C) terraform view-output <path>
D) terraform state output <path>
Correct Answer: A
Explanation: You can specify a path to a specific state file using the -state flag to see its recorded outputs.
32. What meta-argument is used to create a fixed number of identical resource copies ?
A) for_each
B) count
C) multiplier
D) repeat
Correct Answer: B
Explanation: The count argument takes a simple integer and creates that many instances of a resource, using an index to track them.
33. What is “Implicit Dependency” ?
A) A dependency that you must manually write in the code.
B) A dependency that does not actually exist.
C) A dependency that is only used for private modules.
D) A dependency that Terraform discovers automatically by looking at resource references.
Correct Answer: D
Explanation: Implicit dependencies are created when you use one resource’s output as an input for another. Terraform sees this link and automatically knows the correct creation order.
34. Which command can be used to manually back up the state file before a risky move ?
A) terraform backup
B) terraform state backup
C) Simple file copy (cp terraform.tfstate terraform.tfstate.backup)
D) terraform save-state
Correct Answer: C
Explanation: While backends handle backups, developers can manually copy the state file as a safety measure before performing advanced state manipulations.
35. In HCL, how are comments written ?
A) // or # for single lines, /* */ for blocks
B) Only using #
C) Using <!– — >
D) Using —
Correct Answer: A
Explanation: HCL supports both # and // for single-line comments, as well as /* */ for multi-line comment blocks.
36. Which command lists all available Terraform commands ?
A) terraform commands
B) terraform help
C) terraform list
D) terraform –help
Correct Answer: D
Explanation: Running terraform –help or terraform -help provides a complete list of available subcommands and global options.
37. What is a “Data” block used for ?
A) To store sensitive passwords in HCL
B) To fetch information from the cloud provider without creating anything new
C) To define a new database resource
D) To export data to a CSV file
Answer: B
Explanation: The data block defines a data source used to retrieve specific attributes from existing cloud resources, like finding the ID of a VPC created by another team.
38. What is the purpose of the terraform validate -json flag ?
A) To convert HCL into JSON
B) To validate only JSON configuration files
C) To output validation errors and warnings in a machine-readable format
D) To create a JSON execution plan
Correct Answer: C
Explanation: Using the -json flag with validate makes it easier for scripts or CI/CD tools to parse the number and types of errors found in the configuration.
39. How do you delete a specific workspace ?
A) terraform workspace remove <name>
B) terraform workspace delete <name>
C) terraform workspace destroy <name>
D) terraform workspace rm <name>
Correct Answer: B
Explanation: The workspace delete command removes a workspace and its associated state file.
40. What is the command to view the current workspace name ?
A) terraform workspace current
B) terraform show workspace
C) terraform workspace whoami
D) terraform workspace show
Correct Answer: D
Explanation: The workspace show command simply prints the name of the workspace you are currently using.
41. In Terraform, what is “Interpolation” ?
A) The process of evaluating expressions and embedding values into strings
B) A way to translate HCL into other languages
C) A method for encrypting the state file
D) A feature for automatically upgrading providers
Correct Answer: A
Explanation: Interpolation (often seen as ${var.name}) allows you to dynamically insert variable values or resource attributes into your configuration strings.
42. Which command is used to show a list of providers and their versions ?
A) terraform version
B) terraform plugins
C) terraform list
D) terraform providers
Correct Answer: D
Explanation: The providers command lists all the provider plugins used in your project and the versions that Terraform has installed.
43. What is a “Backend Migration” ?
A) Moving all servers from AWS to Azure
B) Moving the Terraform state file from one backend to another (e.g., local to S3)
C) Moving the HCL code to a new Git repository
D) Hiring a new team to manage the back-end servers
Correct Answer: B
Explanation: Migration occurs when you update your backend configuration and run terraform init, allowing Terraform to move your state to the new storage location.
44. Which flag is used with init to ignore interactive prompts ?
A) -force
B) -auto-approve
C) -input=false
D) -silent
Correct Answer: C
Explanation: The -input=false flag disables interactive prompts, which is essential for running Terraform in non-interactive CI/CD environments.
45. What is the “Terraform Registry” used for ?
A) To register the serial number of your Terraform software
B) To find and use publicly shared providers and modules
C) To pay for HashiCorp Enterprise support
D) To list all the developers in your company
Correct Answer: B
Explanation: The Registry is a central hub where developers can find community or official modules and providers to use in their projects.
46. How do you define an internal name for a resource in HCL ?
A) Using the second string after the resource type (e.g., resource “type” “internal_name”)
B) Using the name argument inside the block
C) By naming the .tf file after the resource
D) Terraform assigns internal names randomly
Correct Answer: A
Explanation: In the syntax resource “aws_instance” “web”, “web” is the internal name Terraform uses to track that resource within the state and graph.
47. Which command opens the Terraform documentation in a browser ?
A) terraform docs
B) terraform manual
C) There is no command; you must go to the website manually
D) terraform help –web
Correct Answer: C
Explanation: Terraform does not have a CLI command to open documentation; you typically access it at developer.hashicorp.com.
48. What does the -reconfigure flag do with terraform init ?
A) It deletes the configuration files and starts over
B) It tells Terraform to ignore the existing backend configuration and start fresh
C) It re-formats the code automatically
D) It re-configures the cloud provider’s API keys
Correct Answer: B
Explanation: This flag is used when you want to change backends and do not want to migrate any existing state.
49. What is the command to untaint a resource ?
A) terraform untaint <address>
B) terraform taint -undo
C) terraform delete-taint <address>
D) terraform clean <address>
Correct Answer: A
Explanation: If you change your mind after tainting a resource, the untaint command removes the “dirty” flag so it won’t be recreated on the next run.
