Terraform, a popular infrastructure as code (IaC) tool, may be asked in DevOps Interview or cloud engineer interviews. Terraform lets you declaratively manage virtual machines, containers, and databases using code. We’ve put together a list of 40 Terraform interview questions on everything from Terraform basics to multi-cloud environments and continuous delivery pipelines to help you prepare. These questions will test your Terraform knowledge and help you understand this powerful tool, whether you’re new or experienced.
Terraform is HashiCorp’s open-source infrastructure as code (IaC) tool. It lets you define and manage your infrastructure as code, so you can write code to provision and configure servers, databases, and networks instead of doing it manually.
Basic Terraform Interview Questions
Q.1: What is Terraform?
Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision data center infrastructure using a declarative configuration language.
Q.2: What are the benefits of using Terraform?
The benefits of using Terraform include infrastructure as code, immutability, declarative syntax, modularity, and the ability to manage both cloud and on-premises resources. It also supports multiple providers and enables version-controlled, repeatable, and automated setups.
Q.3: What is HashiCorp Configuration Language (HCL)?
HashiCorp Configuration Language (HCL) is a configuration language developed by HashiCorp that is used by Terraform. It is designed to be human-readable and machine-friendly, and it provides a flexible syntax for describing infrastructure as code.
Q.4: How does Terraform maintain the state of infrastructure?
Terraform maintains the state of infrastructure through a state file that tracks the IDs and properties of resources it manages. This file allows Terraform to map real-world resources to your configuration, keep track of metadata, and improve performance for large infrastructures.
Q.5: What are Terraform Providers?
Terraform providers are plugins that implement resource types. They are responsible for understanding API interactions and exposing resources. Providers can be used for major technology vendors and service providers.
Q.6: What is a Terraform Resource?
A Terraform resource is a component of your infrastructure that Terraform can manage, such as a physical server, a virtual machine, a network switch, or a higher-level service hosted by a cloud provider.
Q.7: What is a Terraform Module?
A Terraform module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.
Q.8: What is the purpose of a terraform init
command?
The terraform init command is used to initialize a working directory containing Terraform configuration files. This command is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It installs all necessary plugins and prepares the directory for other commands.
Q.9: What is a Terraform Plan?
A Terraform plan is a command that creates an execution plan. It determines what actions are necessary to achieve the desired state specified in the configuration files.
Q.10: How do you apply changes with Terraform?
Changes are applied in Terraform by using the terraform apply
command. This command executes the actions proposed in a Terraform plan.
Q.11: What is the difference between terraform plan
and terraform apply
?
Terraform plan
creates an execution plan, showing what actions Terraform intends to take to change the infrastructure to match the configuration. Terraform apply
actually performs the proposed actions.
Q.12: What is a Terraform State file?
A Terraform state file is a file that Terraform uses to map real-world resources to your configuration and keep track of metadata. This file is crucial for Terraform’s operations and is updated after every command that modifies the infrastructure.
Q.13: How do you manage state in Terraform?
State in Terraform can be managed locally or remotely. For team environments or complex setups, it’s recommended to use remote state, which allows state to be stored in a remote data store which supports locking and consistency.
Q.14: What is a Terraform Variable?
A Terraform variable is a way to input values into your Terraform configuration. Variables can be pre-defined in files or injected via command-line flags.
Q.15: What are Input Variables in Terraform?
Input variables in Terraform are parameters that are intended to be passed into modules and configurations. They allow you to customize aspects of Terraform modules without altering the module’s own source code.
Q.16: How do you define a variable in Terraform?
A variable in Terraform can be defined using variable blocks in your configuration files. Each block declares a variable and sets parameters like type constraints, default values, and descriptions.
Q.17: What is a Terraform Output?
A Terraform output is a way to extract information about the infrastructure resources managed by Terraform. Outputs can be useful for integrating Terraform with other scripts or systems.
Q.18: How do you declare output values in Terraform?
Output values in Terraform are declared using the output
block in your configuration files. Each block specifies an output name and the value associated with it.
Q.19: What is the purpose of terraform fmt?
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.
Q.20: What is a Terraform Provider plugin?
A Terraform Provider plugin is a binary that Terraform uses to interact with the API of a service provider. Each provider offers a collection of resource types and associated data sources.
Intermediate Terraform Interview Questions
Q.21: What is the difference between Terraform modules and resources?
Terraform resources are individual components that define one specific piece of infrastructure, like a virtual machine or a network configuration. Terraform modules, on the other hand, are containers for multiple resources that are used together, allowing you to group and reuse configurations.
Q.22: How do you use Terraform modules?
To use Terraform modules, you can either create your own by defining a group of resources together in a folder, or use pre-built modules from the Terraform Registry. Modules are included in Terraform configurations using the module
block, where you specify the source and the input variables.
Q.23: What are Terraform workspaces?
Terraform workspaces are used to manage separate instances of a single configuration within the same state file. This is useful for maintaining different environments like staging and production within the same Terraform setup.
Q.24: How do you use Terraform workspaces?
You use Terraform workspaces by initializing them with terraform workspace new [name]
and switching between them with terraform workspace select [name]
. This allows you to apply configurations to different environments without altering the infrastructure of others.
Q.25: What is a Terraform data source?
A Terraform data source allows you to fetch and compute data from external resources that can be used in your Terraform configuration. It lets you use information defined outside of Terraform, like a public IP or a specific image ID.
Q.26: How do you use data sources in Terraform?
You use data sources in Terraform by declaring them in your configuration with the data
block. This declaration includes the type of data source and the specific parameters needed to retrieve the data.
Q.27: What are Terraform provisioners? Name a few.
Terraform provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction. Some common types are local-exec
(executes on the local machine) and remote-exec
(executes on a remote machine).
Q.28: What are lifecycle rules in Terraform?
Lifecycle rules in Terraform are used to specify how certain actions should be handled within the lifecycle of a resource, such as preventing destruction or changing the way updates are performed.
Q.29: How do you manage dependencies between resources in Terraform?
Dependencies in Terraform are primarily managed automatically by Terraform through the relationships defined in the configuration. You can also explicitly set dependencies using the depends_on attribute within a resource.
Q.30: What is a terraform.tfvars
file?
A terraform.tfvars file is used to set input variables in Terraform. It’s a way to pass external values into your Terraform configuration.
Advanced Terraform Interview Questions
Q.31: How do you use Terraform with remote backends?
To use Terraform with remote backends, you specify a backend type in your Terraform configuration under the backend
block. This configuration might include settings for AWS S3, Azure Storage, or Terraform Cloud, where state files are stored remotely and accessed by team members.
Q.32: Explain how to use Terraform Cloud/Enterprise features.
Terraform Cloud and Enterprise provide features like remote state management, team access controls, private module registry, policy enforcement, and workspace management. To use these features, set up your Terraform configurations to connect to Terraform Cloud as the backend, and manage your infrastructure through the web UI or API.
Q.33: What is the difference between terraform plan
and terraform refresh
?
terraform plan creates an execution plan and shows what actions Terraform will take to change the infrastructure to match the configuration. terraform refresh updates the state file with the current status of the resources in the real environment without making any changes to the infrastructure.
Q.34: How do you write custom providers in Terraform?
Writing custom providers in Terraform involves coding in Go, defining resource schemas, and implementing CRUD (Create, Read, Update, Delete) operations using the APIs of the service you’re interfacing with. This code is then compiled into a binary that Terraform can invoke.
Q.35: How can you use Terraform to deploy a multi-tier application?
To deploy a multi-tier application using Terraform, define each tier as a set of resources or modules. For example, one module could manage the database tier, another the application server, and another the load balancer and networking. These modules are then orchestrated by Terraform to handle dependencies and configuration.
Q.36: What are Terraform modules, and how do you create them?
Terraform modules are containers for multiple resources that are used together. To create a module, write a set of Terraform configuration files in a directory. This directory can then be referenced by other configurations using the module
block.
Q.37: How do you handle variable defaults and override them?
Variable defaults are set in the variable declaration using the default
keyword. These can be overridden by specifying variable values in .tfvars
files, as environment variables, or directly on the command line when running Terraform commands.
Q.38: How do you use Terraform with Terraform Enterprise?
Using Terraform with Terraform Enterprise involves setting up Terraform configurations to use the Terraform Enterprise instance as the backend. This allows leveraging enterprise features like enhanced security, governance, and collaboration tools.
Q.39: Explain Terraform’s terraform graph
command.
The terraform graph command generates a visual representation of either a configuration or execution plan in DOT format. This can be used to understand the dependency graph of the resources Terraform manages.
Q.40: What are Terraform Workspaces, and how do they help in multi-environment management?
Terraform Workspaces allow you to manage multiple states with the same configuration, facilitating the management of multiple environments such as development, staging, and production. Each workspace is isolated from the others, which helps in environment-specific configurations without resource clashes.