Terraform Interview Questions

Use the filter to quickly find topics like providers, modules, state/backends, workspaces, lifecycle & drift, plans/applies, and CI/CD.

Showing 50 of 50
  1. What is Terraform and what is it used for?
    Terraform is an infrastructure as code (IAC) tool that allows you to manage and provision infrastructure resources in a repeatable, version-controlled way. It can be used to manage cloud resources, as well as on-premises resources.
  2. What are the main components of Terraform configuration files?
    The main components of Terraform configuration files are providers, resources, variables, data sources, and outputs.
  3. How does Terraform handle rollbacks and disaster recovery?
    Terraform maintains a state file that tracks the resources it manages, and it has built-in support for versioning and rollbacks. In the event of a disaster, you can use Terraform to recreate your infrastructure in a new location, or you can use the state file to revert to a previous version of your infrastructure.
  4. What are the benefits of using Terraform?
    The benefits of using Terraform include: infrastructure management as code, repeatable, version-controlled infrastructure deployment, support for multiple cloud and on-premises providers, and improved collaboration and documentation.
  5. What is a Terraform module and how is it used?
    A Terraform module is a collection of Terraform configuration files that define infrastructure resources. Modules can be reused across multiple Terraform configurations, making it easier to manage and maintain your infrastructure. They can be used to create reusable, abstract building blocks for your infrastructure.
  6. How does Terraform handle dependencies between resources?
    Terraform handles dependencies between resources by automatically determining the correct order to create, update, or delete resources based on the dependencies specified in the Terraform configuration files. This ensures that resources are created and updated in the correct order to avoid errors and potential data loss.
  7. What is a Terraform state file and what is its purpose?
    The Terraform state file is a record of the resources Terraform manages. It keeps track of the desired state of your infrastructure, as defined in your Terraform configuration files, and the actual state of your infrastructure. Terraform uses the state file to determine what actions need to be taken to reach the desired state, and it updates the state file whenever it makes changes to your infrastructure.
  8. What is the difference between Terraform and other infrastructure as code tools, such as Ansible and Puppet?
    Terraform, Ansible, and Puppet are all infrastructure as code (IAC) tools, but they differ in their scope and focus. Terraform is focused on managing and provisioning infrastructure resources, whereas Ansible and Puppet are focused on automating configuration management and deployment tasks. Terraform is best suited for provisioning and managing infrastructure resources, while Ansible and Puppet are better suited for automating configuration management and deployment tasks.
  9. What is a Terraform provider and what is its role?
    A Terraform provider is a plugin that allows Terraform to interact with a specific cloud provider or other service. Providers are responsible for defining the set of resources that can be created and managed by Terraform, as well as the actions that can be performed on those resources. Providers also manage authentication and authorization to access the resources being managed.
  10. How does Terraform handle secrets and sensitive data?
    Terraform does not store secrets and sensitive data directly in its configuration files. Instead, it provides mechanisms for securely storing and retrieving secrets and sensitive data, such as environment variables, command-line arguments, or external data sources like Vault. This helps to keep sensitive data separate from the configuration and out of version control systems.
  11. What is the role of Terraform variables and how are they used?
    Terraform variables are a way to pass input values into Terraform configuration files. They can be used to make your configurations more flexible and reusable, allowing you to parameterize your infrastructure. Variables can be set as environment variables, command-line options, or specified in a separate variables file.
  12. What is the purpose of Terraform data sources and how are they used?
    Terraform data sources allow Terraform to retrieve data from external sources, such as cloud APIs or databases, and use that data in Terraform configuration files. Data sources are used to retrieve information about existing infrastructure, or to get values that are used elsewhere in your Terraform code. They help to keep your Terraform configurations flexible and reusable by allowing you to use data from external sources in your infrastructure.
  13. What is the purpose of Terraform workspaces and how are they used?
    Terraform workspaces are used to separate Terraform state files and variables for different environments, such as development, testing, and production. They allow you to use the same Terraform configuration files to manage multiple instances of the same infrastructure, each with its own state and variable values. Workspaces can be used to easily switch between different environments, making it easier to manage and maintain your infrastructure.
  14. What is the purpose of Terraform remote state and how is it used?
    Terraform remote state is used to store the Terraform state file in a remote location, such as a shared file system or a cloud storage service. Storing the state file remotely makes it easier to share state information between Terraform configurations and between team members, and helps to ensure that state information is not lost if a local machine is destroyed. Terraform remote state can be configured to use different storage backends, such as S3 or Azure Blob Storage, depending on the needs of your infrastructure.
  15. What is the purpose of Terraform input and output variables and how are they used?
    Terraform input and output variables are used to pass data between Terraform configuration files. Input variables allow you to pass data into Terraform configurations from the command line or external sources, making your configurations more flexible and reusable. Output variables allow you to pass data from one Terraform configuration to another, making it easier to connect and manage complex infrastructure.
  16. What is the purpose of Terraform modules and how are they used?
    Terraform modules are collections of Terraform configurations that define infrastructure resources. Modules can be reused across multiple Terraform configurations, making it easier to manage and maintain your infrastructure. They can be used to create reusable, abstract building blocks for your infrastructure, and to encapsulate complex resource configurations into a single, easy-to-use component.
  17. What is the purpose of Terraform backend and how is it used?
    Terraform backend is used to configure the location where Terraform state files are stored and managed. By default, Terraform state is stored locally on the machine that is running Terraform, but it can also be stored in a remote location, such as a shared file system or a cloud storage service. The backend is used to manage the storage and retrieval of Terraform state, and helps to ensure that state information is not lost if a local machine is destroyed.
  18. What is the purpose of Terraform variables.tf file and how is it used?
    The Terraform variables.tf file is used to define and declare Terraform variables. Variables can be used to make Terraform configurations more flexible and reusable, allowing you to parameterize your infrastructure. The variables.tf file is used to specify the names, types, and default values of variables, and can be used to set the values of variables either from the command line or from an external file.
  19. What is the purpose of Terraform providers.tf file and how is it used?
    The Terraform providers.tf file is used to configure and manage Terraform providers. Providers are plugins that allow Terraform to interact with specific cloud providers or other services. The providers.tf file is used to specify the versions of providers that Terraform should use, as well as any authentication or authorization settings required to access the resources being managed.
  20. What is the purpose of Terraform main.tf file and how is it used?
    The Terraform main.tf file is the main Terraform configuration file and is used to define the infrastructure resources that Terraform should manage. The main.tf file specifies the provider to use, the resources to create, and the values for the variables used in the Terraform configuration. The main.tf file is the starting point for Terraform when it is run, and is used to generate the Terraform state and plan the changes to be made to the infrastructure.
  21. What is the purpose of Terraform variables and how are they used?
    Terraform variables allow you to parameterize your Terraform configurations and make them more flexible and reusable. Variables can be used to pass data into Terraform configurations from the command line, environment variables, or external files, and can be used to modify the behavior of Terraform configurations based on the values set for the variables. Variables are defined in the Terraform code and can be set or overridden when Terraform is run.
  22. What is the difference between Terraform apply and Terraform plan?
    The Terraform apply and Terraform plan commands are used to manage the state of Terraform-managed infrastructure. The Terraform plan command generates an execution plan that outlines the changes that Terraform will make to the infrastructure, without actually making the changes. The Terraform apply command takes the execution plan generated by Terraform plan and applies the changes to the infrastructure, bringing it into the desired state. Terraform apply should only be run after a successful Terraform plan, and should be carefully reviewed before being run, to ensure that the changes being made are correct and desired.
  23. What is the purpose of Terraform state and how is it used?
    Terraform state is a representation of the Terraform-managed infrastructure, including the resources that have been created and their current state. Terraform state is used to keep track of the changes made to the infrastructure, and to ensure that Terraform can make changes to the infrastructure in the correct order. Terraform state is stored in a state file, and can be stored locally or in a remote location, depending on the Terraform backend configuration.
  24. What is the purpose of Terraform resource blocks and how are they used?
    Terraform resource blocks are used to define and create Terraform-managed infrastructure resources. Resource blocks specify the type of resource to be created, such as an AWS S3 bucket or a Google Cloud Storage bucket, as well as any properties or configuration settings that are required for that resource. Resource blocks are defined in the Terraform code, and Terraform uses the information in the resource blocks to create and manage the resources.
  25. What is the purpose of Terraform modules and how are they used?
    Terraform modules are collections of Terraform configurations that are grouped together to create reusable components. Modules can be used to encapsulate common infrastructure patterns and reduce duplication of code. Modules can be imported and used as building blocks within Terraform configurations, and can be shared and reused across multiple Terraform projects. Modules can be used to simplify the organization and management of Terraform code, and to promote consistency and best practices across Terraform configurations.
  26. What is the purpose of Terraform providers and how are they used?
    Terraform providers are plugins for Terraform that provide the necessary APIs and functionality to manage specific types of infrastructure resources. Providers are used by Terraform to interact with the infrastructure APIs of different service providers, such as AWS, Google Cloud, or Azure. Providers are defined in the Terraform code, and Terraform uses the information in the provider configuration to communicate with the infrastructure APIs and manage the resources.
  27. What is the purpose of Terraform outputs and how are they used?
    Terraform outputs are used to expose information about the Terraform-managed infrastructure to other parts of the Terraform code or to external tools. Outputs can be used to pass information from Terraform configurations to other Terraform configurations, or to pass information from Terraform configurations to external tools or scripts. Outputs are defined in the Terraform code and can be displayed when Terraform is run, allowing the values of the outputs to be easily accessed and used in other parts of the Terraform code or in external tools.
  28. What is the purpose of Terraform data sources and how are they used?
    Terraform data sources are used to retrieve information about existing infrastructure resources and make it available to the Terraform code. Data sources can be used to gather information about resources that were created outside of Terraform, or to retrieve information about resources that were created by other Terraform configurations. Data sources are defined in the Terraform code and the information retrieved from the data sources can be used in Terraform resource blocks, allowing Terraform to manage the resources and make changes to the infrastructure.
  29. What is the purpose of Terraform variables and how are they used?
    Terraform variables are used to store values that can be used in Terraform configurations and referenced throughout the Terraform code. Variables provide a way to input values into Terraform configurations dynamically, allowing the same Terraform code to be used in different environments or with different input values. Variables can be defined in the Terraform code and assigned values through the Terraform command line, environment variables, or a Terraform variable file.
  30. What is the purpose of Terraform workspaces and how are they used?
    Terraform workspaces are used to manage multiple separate sets of Terraform state in the same Terraform configuration. Workspaces allow Terraform users to maintain different states for different environments, such as development, staging, and production, or for different customer accounts, without having to maintain separate Terraform configurations. Workspaces can be created, selected, and managed using Terraform commands, and Terraform state is automatically associated with the selected workspace.
  31. What is the purpose of Terraform remote state and how is it used?
    Terraform remote state is used to store Terraform state in a remote location, rather than in the local Terraform configuration directory. Remote state allows Terraform users to share Terraform state between multiple Terraform configurations, or to store Terraform state in a centralized location for collaboration and version control. Remote state can be stored in a variety of remote storage locations, including Terraform Cloud, S3, or Google Cloud Storage.
  32. What is the difference between Terraform state and Terraform state management?
    Terraform state refers to the data that Terraform maintains about the infrastructure resources that it manages. Terraform state is used to keep track of the state of the infrastructure, including the desired state defined in the Terraform code and the actual state of the resources in the infrastructure. Terraform state management refers to the process of managing the Terraform state, including storing the state, updating the state as changes are made to the infrastructure, and ensuring the state is consistent and up-to-date.
  33. What is the purpose of Terraform providers and how are they used?
    Terraform providers are used to manage the interaction between Terraform and the underlying infrastructure resources. Providers are responsible for creating, updating, and deleting resources, as well as for reading the current state of resources. Terraform supports a wide range of providers for various infrastructure services, including AWS, Google Cloud, Microsoft Azure, and many others. To use a provider in Terraform, you must declare the provider in the Terraform configuration and configure its credentials.
  34. What is the difference between Terraform and other infrastructure as code (IAC) tools, such as Ansible or Puppet?
    Terraform is a tool for provisioning and managing infrastructure resources, while Ansible and Puppet are configuration management tools used to manage and configure software on existing infrastructure. Terraform is used to create and manage infrastructure resources, such as virtual machines, network devices, and databases, while Ansible and Puppet are used to configure software on these resources after they have been provisioned. Terraform focuses on the provisioning and management of infrastructure resources, while Ansible and Puppet focus on the configuration of software on these resources.
  35. What is the purpose of Terraform modules and how are they used?
    Terraform modules are used to package Terraform code into reusable, composable units. Modules provide a way to organize and share Terraform code, as well as to reuse common configurations in multiple Terraform configurations. Modules can be defined as separate Terraform configurations, stored in a version control system, and then referenced in other Terraform configurations using module blocks. Modules can accept input parameters, just like variables, allowing them to be customized and configured dynamically.
  36. What is the purpose of Terraform outputs and how are they used?
    Terraform outputs are used to display values from the Terraform state to the user. Outputs can be used to expose information about the infrastructure resources managed by Terraform, such as the IP address of a load balancer or the DNS name of a database. Outputs can be defined in the Terraform configuration and displayed using the Terraform output command. Outputs can be used for debugging, testing, and documentation purposes, and can also be used to share information between Terraform configurations.
  37. You are working on a project that requires you to create a Terraform module for a web server infrastructure. The infrastructure should consist of an EC2 instance, a security group, and an Elastic IP. The EC2 instance should be able to run a custom web application that requires certain dependencies. How would you go about creating this module in Terraform?
    To create this Terraform module, you would start by defining the EC2 instance resource in the Terraform configuration. You would specify the Amazon Machine Image (AMI) that the EC2 instance should be based on, as well as the instance type and other instance-specific settings. Next, you would define the security group resource and specify the inbound and outbound rules for the security group. Finally, you would define the Elastic IP resource and associate it with the EC2 instance. To ensure that the custom web application and its dependencies can run on the EC2 instance, you would use Terraform's `provisioner` block to execute shell scripts or use other provisioning tools like Ansible to install the required dependencies on the EC2 instance.
  38. You are working on a project that requires you to manage an existing AWS VPC infrastructure using Terraform. The VPC already contains several subnets, security groups, and EC2 instances. You are tasked with adding a new subnet to the VPC and launching a new EC2 instance in the new subnet. How would you go about accomplishing this using Terraform?
    To accomplish this task using Terraform, you would start by writing a Terraform configuration that defines the new subnet resource. Next, you would write a Terraform configuration that defines the EC2 instance resource and associates it with the new subnet. You would also specify the security group that the EC2 instance should be associated with. Finally, you would run Terraform's `apply` command to apply the changes and launch the new EC2 instance in the new subnet. Terraform would use the existing VPC infrastructure and only create the new subnet and EC2 instance, preserving the rest of the VPC infrastructure.
  39. You are working on a project that requires you to manage an AWS S3 bucket using Terraform. The S3 bucket should be configured with versioning enabled and with a lifecycle policy that moves objects to the S3 Glacier storage class after 30 days. How would you go about configuring this using Terraform?
    To configure this using Terraform, you would start by writing a Terraform configuration that defines the S3 bucket resource. In the Terraform configuration, you would enable versioning for the S3 bucket by setting the `versioning` argument to `true`. Next, you would write a Terraform configuration that defines the lifecycle policy for the S3 bucket. The lifecycle policy would specify that objects in the S3 bucket should be transitioned to the S3 Glacier storage class after 30 days. Finally, you would run Terraform's `apply` command to apply the changes and configure the S3 bucket with versioning and the specified lifecycle policy.
  40. You are working on a project that requires you to manage an AWS Auto Scaling group and its associated EC2 instances using Terraform. The Auto Scaling group should scale out if the average CPU utilization of the EC2 instances exceeds 60% for a period of 5 minutes, and scale in if the average CPU utilization drops below 40% for a period of 5 minutes. How would you go about accomplishing this using Terraform?
    To accomplish this task using Terraform, you would start by writing a Terraform configuration that defines the EC2 instance resource. Next, you would write a Terraform configuration that defines the Auto Scaling group resource and associates it with the EC2 instance. In the Terraform configuration for the Auto Scaling group, you would specify the desired number of EC2 instances, the minimum and maximum number of EC2 instances, and the scaling policies. The scaling policies would be configured to scale out if the average CPU utilization of the EC2 instances exceeds 60% for a period of 5 minutes and scale in if the average CPU utilization drops below 40% for a period of 5 minutes. Finally, you would run Terraform's `apply` command to apply the changes and create the Auto Scaling group and its associated EC2 instances.
  41. You are working on a project that requires you to manage an AWS RDS database instance using Terraform. The RDS database instance should be publicly accessible, but only from specific IP addresses. How would you go about configuring this using Terraform?
    To configure this using Terraform, you would start by writing a Terraform configuration that defines the RDS database instance resource. In the Terraform configuration, you would set the `publicly_accessible` argument to `true` to make the RDS database instance publicly accessible. Next, you would define a security group resource and specify the inbound rules for the security group. The inbound rules would allow traffic from the specific IP addresses and only for the required port for the RDS database instance. Finally, you would associate the security group with the RDS database instance and run Terraform's `apply` command to apply the changes and configure the RDS database instance.
  42. You are working on a project that requires you to manage a multi-tier application using Terraform. The application consists of a web tier and a database tier, and should be deployed across multiple AWS regions for high availability. How would you go about accomplishing this using Terraform?
    To accomplish this task using Terraform, you would start by writing Terraform configurations for the web tier and the database tier. The Terraform configurations would define the AWS resources required for each tier, such as EC2 instances, security groups, and load balancers. Next, you would use Terraform's `module` feature to encapsulate the Terraform configurations for each tier into reusable, composable units. Finally, you would use Terraform's `for_each` expression and `count` argument to deploy multiple instances of each tier across multiple AWS regions, achieving high availability for the multi-tier application.
  43. How would you configure Terraform to store the state of your infrastructure in an S3 bucket, and how would you manage access to the state file in the bucket?
    To configure Terraform to store the state of your infrastructure in an S3 bucket, you would use the `terraform { backend "s3" { ... } }` block in your Terraform configuration. The block would specify the details of the S3 bucket, such as the bucket name and region, as well as any other required configuration options. To manage access to the state file in the S3 bucket, you would either use an IAM user with the necessary permissions to read and write the state file, or you could use an IAM role that provides the necessary permissions. You can also control access to the S3 bucket using a bucket policy.
  44. How would you go about testing Terraform code before applying it to production, and what tools would you use?
    To test Terraform code before applying it to production, you could use a combination of the following tools: - Terraform's built-in `plan` command to preview the changes that would be made to your infrastructure - Terraform's `validate` command to check the syntax of your Terraform configuration files - Terraform's `taint` command to manually mark a resource as needing to be recreated - Terraform's `refresh` command to update the state of your Terraform-managed infrastructure to match the real-world infrastructure - Terraform's `import` command to import existing infrastructure into Terraform - Terraform modules and workspaces to create isolated testing environments - Integration testing frameworks such as Test Kitchen to test Terraform configurations as part of a Continuous Integration/Continuous Deployment (CI/CD) pipeline - Automated testing frameworks such as Goss or Serverspec to test the actual infrastructure deployed by Terraform.
  45. How would you go about debugging a Terraform error, and what tools would you use?
    To debug a Terraform error, you could use the following steps: - Review the error message to get a sense of what went wrong - Use the Terraform log file, which is located in the same directory as the Terraform configuration file, to get additional information about the error - Use the `debug` log level by adding the `-log=debug` flag to the Terraform command line to get more detailed information about what Terraform is doing - Use Terraform's built-in variables, such as `var.foo` and `terraform.workspace`, to get additional information about the state of your Terraform-managed infrastructure - Use the `terraform state` command to get information about the state of your Terraform-managed infrastructure, such as which resources have been created and what their current state is - If necessary, reach out to the Terraform community or consult the Terraform documentation for additional assistance.
  46. What is the difference between a Terraform module and a Terraform provider?
    A Terraform module is a self-contained package of Terraform configurations that are used to create a specific set of infrastructure resources. Modules can be reused and composed to create more complex infrastructure. A Terraform provider is a plugin for Terraform that implements the logic to interact with a specific type of infrastructure, such as AWS or Google Cloud. Providers are used to create, manage, and update the resources in that infrastructure.
  47. How does Terraform determine the order in which it creates resources?
    Terraform determines the order in which it creates resources based on the dependencies between the resources. Terraform uses a directed acyclic graph (DAG) to calculate the order in which resources should be created, updated, or deleted. The DAG is created based on the relationships between resources defined in the Terraform configuration files. Terraform will only create resources that do not have any dependencies on other resources until all of their dependencies have been created.
  48. What is the purpose of the Terraform state file, and what information does it contain?
    The Terraform state file is a record of all the infrastructure resources that Terraform knows about and the current state of those resources. It contains information such as the ID of each resource, its type, the properties of the resource, and the current state of the resource. The state file is used by Terraform to determine the differences between the actual state of the infrastructure and the desired state described in the Terraform configuration files. Terraform uses this information to determine what changes, if any, need to be made to the infrastructure to bring it into the desired state.
  49. What is the purpose of the `terraform plan` command, and what does it output?
    The `terraform plan` command is used to preview the changes that Terraform will make to the infrastructure. The command outputs a plan file that shows the changes that will be made, including the creation, modification, or deletion of resources. The plan file is used to confirm that the changes Terraform will make are what you expect before applying the changes to the actual infrastructure. The `terraform plan` command is an important step in the Terraform workflow, as it allows you to catch and correct any mistakes in the Terraform configuration files before the changes are applied to the infrastructure.
  50. What is the purpose of the `terraform apply` command, and how does it work?
    The `terraform apply` command is used to apply the changes described in the Terraform configuration files to the actual infrastructure. When you run the `terraform apply` command, Terraform reads the configuration files and the state file, calculates the changes that need to be made to the infrastructure, and then makes those changes. The command outputs a detailed report of the changes made, including the creation, modification, or deletion of resources. The `terraform apply` command is the final step in the Terraform workflow and is used to actually create or update the infrastructure resources.
Tip: Mention state safety (locks & remote backends), module versioning, provider pinning, plan review, and import/refactor strategies.