26 Most Popular Terraform Interview Questions and Answers (2024)

Blog / 26 Most Popular Terraform Interview Questions and Answers (2024)
blog image

Terraform is one of the most popular IAC tools around.

Terraform allows you to provision infrastructure resources across various cloud providers, data centers, and other infrastructure platforms.

Understanding the fundamentals of Terraform and beyond is slowly becoming a need to have for many tech interviews.

This blog will get you up to speed with the latest Terraform interview questions and help you nail your next interview.

Q1.

What is Terraform?

Junior
  • Terraform is an open-source tool by HashiCorp for infrastructure as code (IaC). It enables users to define and manage data center infrastructure using a high-level configuration language.
  • Terraform automates the creation, modification, and maintenance of infrastructure in various environments like public clouds, ensuring consistency and reproducibility.
Q2.

What are the key features of Terraform?

Junior
  • Infrastructure as Code: Allows defining infrastructure with configuration files for consistency and repeatability.
  • Declarative Syntax (HCL): Uses HashiCorp Configuration Language, which is human-readable and writable, for resource declaration.
  • Provider Ecosystem: Supports numerous providers, enabling management of a wide range of cloud services and platforms.
  • State Management: Tracks and manages the state of infrastructure, ensuring alignment between configuration and real-world resources.
  • Modularity: Offers modules for reusable, composable, and shareable infrastructure definitions.
  • Change Automation and Orchestration: Automates the process of applying changes to the infrastructure in a safe, incremental manner.
  • Plan and Apply Workflow: Allows previewing of changes before applying them, enhancing predictability and safety.
  • Multi-Cloud Deployment: Capable of managing resources across different cloud providers from a single configuration.
Q3.

Define IAC.

Junior
  • Infrastructure as Code (IaC) is a method of managing and provisioning computing infrastructure through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.
  • It enables automation, consistency, and version control in infrastructure management.
Q4.

What are the most useful Terraform commands?

Junior
  • terraform init: Initializes a Terraform working directory, setting up the environment.
  • terraform plan: Shows a preview of the changes that Terraform will make to your infrastructure.
  • terraform apply: Applies the changes to reach the desired state of the infrastructure.
  • terraform destroy: Removes all resources managed by Terraform.
  • terraform fmt: Automatically formats Terraform code to a standard layout.
  • terraform validate: Checks for syntax errors and inconsistencies in the configuration files.
  • terraform refresh: Updates the local state file to match the actual state of the infrastructure.
  • terraform output: Displays output variables.
  • terraform import: Brings existing infrastructure under Terraform management.
  • terraform workspace: Manages and switches between multiple workspaces for different environments.
Q5.

Does Terraform support multi-provider deployments?

Junior
  • Yes, Terraform supports multi-provider deployments. It allows you to manage a wide range of resources across different cloud providers and services within a single configuration, enabling complex, multi-cloud infrastructure management.

Don't Let One Question Ruin Your Interview...

Q6.

How does Terraform differ from other infrastructure as code tools like Ansible or Puppet?

Junior
  • Terraform specializes in infrastructure provisioning with a declarative approach, focusing on creating and managing cloud infrastructure.
  • Ansible and Puppet are more geared towards configuration management and software deployment on existing systems, often using a procedural style.
  • Terraform is cloud-agnostic, whereas Ansible and Puppet are more closely tied to specific environments.
Q7.

Explain the concept of a Terraform Provider.

Junior
  • A Terraform Provider is responsible for managing the lifecycle of a specific cloud or infrastructure resource. Providers communicate with the API of a service (e.g., AWS, Azure) to create, read, update, and delete resources.
  • They are defined in Terraform configuration files and linked to specific resource types using the "resource" block.
Q8.

How do you manage secrets and sensitive data in Terraform?

Junior
  • In Terraform, secrets and sensitive data are managed using environment variables, the sensitive attribute in variables, and secure secret storage solutions like HashiCorp Vault.
Q9.

What is a Terraform State file?

Mid
  • The Terraform State file (.tfstate) is a JSON file that keeps track of the current state of your infrastructure. It stores resource mappings and their configuration.
  • It's crucial for Terraform to manage and update resources correctly, as it uses this file to understand what resources are currently deployed.
Q10.

What is a remote backend in Terraform?

Mid
  • In Terraform, a remote backend is used to store the state file in a remote, shared location, which is essential for team collaboration. It supports features like state locking and versioning, ensuring safe and synchronized operations among team members.
  • Popular remote backends include services like AWS S3, Azure Blob Storage, Google Cloud Storage, and Terraform Cloud.
Q11.

How do you handle infrastructure updates or changes without causing downtime?

Mid
  • To handle infrastructure updates without causing downtime, use Terraform's blue-green deployment strategy, where you spin up a new, updated version of your infrastructure (green) alongside the old version (blue). After testing and ensuring the new version is running smoothly, traffic is gradually shifted from the old to the new version. This approach minimizes downtime and risk during updates.
  • Terraform also supports rolling updates through resource replacement and the use of immutable infrastructure. Proper planning and testing are essential to avoid downtime.
Q12.

What are Terraform Modules?

Mid
  • Terraform Modules are reusable, self-contained units of configuration that help organize and encapsulate infrastructure components.
  • They promote code reuse, maintainability, and separation of concerns, making it easier to manage and scale complex infrastructure.
Q13.

Explain the remote state feature in Terraform.

Mid
  • Remote state allows teams to store and share the Terraform state file in a central location (e.g., AWS S3, HashiCorp Terraform Cloud) to collaborate on infrastructure projects. It enables concurrent collaboration and prevents state file conflicts.
Q14.

How do you manage dependencies between resources in Terraform?

Mid
  • Terraform automatically manages dependencies between resources based on the order they are declared in the configuration file.
  • However, you can use depends_on and count attributes to control and express explicit dependencies.
Q15.

What are Sentinel policies?

Mid
  • Sentinel policies in the context of HashiCorp products, like Terraform, are a set of rules used for policy-as-code. They provide a framework to enforce fine-grained, logic-based policies on Terraform configurations, states, and plans, ensuring that only compliant infrastructure changes are applied.
  • Sentinel policies enable automated enforcement of organizational standards and best practices for security, compliance, and operational efficiency.
Q16.

Provide a few examples where Sentinel policies can be used?

Mid
  • Enforcing Minimum Instance Sizes: Ensuring that cloud instances (like AWS EC2, Azure VMs) are not below a certain size to meet performance standards.
  • Restricting Cloud Regions: Limiting resource deployment to specific cloud regions for compliance with data residency laws.
  • Limiting Resource Costs: Preventing the creation of resources that exceed a certain cost threshold to control cloud expenses.
  • Mandatory Tags: Requiring specific tags on resources for better resource management and cost allocation.
  • Restricting Provider Usage: Limiting the use of certain cloud providers or services not approved for use.
Q17.

What are the different levels of Sentinel enforcement?

Mid
  • Advisory (Soft-Mandatory): Policies at this level are not strictly enforced. A violation triggers a warning, but it doesn't prevent the Terraform run. It's useful for recommendations or guidelines.
  • Mandatory (Hard-Mandatory): Violations of these policies result in the prevention of the Terraform plan or apply. This level is used for critical rules that must be enforced.
  • Soft-Mandatory with Override: Policies are enforced, but certain privileged users can override the policy failure. This level balances strict enforcement with flexibility for exceptions.
Q18.

What is the Terraform Registry?

Mid
  • The Terraform Registry is a platform for sharing and discovering Terraform modules and providers. It provides a centralized location for finding pre-built infrastructure modules, which can save time and promote best practices.
Q19.

How do you manage different environments (e.g., dev, staging, production) using Terraform?

Mid
  • To manage different environments like development, staging, and production in Terraform, you typically use one of the following approaches:
    • Workspaces: Terraform workspaces allow you to use the same configuration for multiple environments by changing state files based on the workspace. This approach is simple but can lead to configuration drift if not carefully managed.
    • Module Reuse: Define the infrastructure as modules and reuse these modules with different parameters for each environment. This method involves separate directories or configuration files for each environment, where the environment-specific values are defined.
    • Variable Files: Use variable files to define environment-specific settings. You can have a common Terraform configuration and switch between different variable files when applying configurations for different environments.
Q20.

Explain how you can automate the deployment of Terraform configurations using CI/CD pipelines.

Mid
  • Integrating Terraform into CI/CD pipelines involves using tools like Jenkins, CircleCI, or GitLab CI/CD to automatically run terraform init, terraform plan, and terraform apply based on code changes.
  • This ensures that infrastructure updates are made as part of your software development process.
Q21.

What is Terraform Core?

Mid
  • Terraform Core is the main part of Terraform, a command-line application that performs the primary functions of Terraform. It reads Terraform configurations, maintains the state, and interacts with providers to manage resources.
  • Terraform Core is responsible for parsing and executing Terraform scripts, planning and applying infrastructure changes, and maintaining the state of managed resources.
Q22.

What is Terraform Cloud?

Mid
  • Terraform Cloud is a HashiCorp-hosted service that provides advanced features and collaboration tools for teams working with Terraform. It offers remote execution of Terraform runs in a consistent environment, along with features like workspace management, version control integration, team access controls, and a private module registry.
  • Terraform Cloud helps in automating the Terraform workflow, providing a secure and scalable platform for managing infrastructure as code, especially beneficial for teams and larger organizations.
Q23.

What is the Resource Graph in Terraform?

Mid
  • In Terraform, the Resource Graph is a visual representation of all the resources defined in the configuration and their interdependencies. It shows how different resources are related and the order in which Terraform will create, update, or destroy them.
  • This graph is used internally by Terraform to optimize the provisioning process and ensure that dependencies are correctly resolved, allowing Terraform to safely parallelize operations where possible.
Q24.

What is a tainted resource?

Mid
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q25.

Explain how you can manage the lifecycle of a Terraform resource with the lifecycle block.

Mid
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q26.

What is Terraform state locking, and why is it important in a multi-user environment?

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q27.

Explain the concept of remote-exec and local-exec provisioners in Terraform.

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q28.

How do you handle resource drift or changes that were made outside of Terraform?

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q29.

How does Terraform ensure the idempotency of resource provisioning?

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q30.

How does Terraform manage resource dependencies, and what is the role of the Terraform graph in this process?

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Q31.

Can Terraform be used for on-prem infrastructure?

Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.