Terraform Interview Questions and Answers

Terraform Interview Questions and Answers

Q1: What happens if you remove an EC2 instance from the Terraform state file after its creation and run terraform apply?

Once you remove a resource entry from the state file, Terraform will no longer track it. On the next terraform apply, Terraform will attempt to create the resource again, as it doesn’t recognize the resource in its state.

Q2: What is the role of the state file in Terraform?

The Terraform state file is where Terraform records all the infrastructure it manages. It keeps track of resource metadata, including the current state of the infrastructure, allowing Terraform to make appropriate updates during subsequent executions.

Q3: How should the Terraform state file be stored for optimal collaboration?

The recommended approach is to store the state file in a remote backend, such as Amazon S3 or GitLab’s Terraform state management. This enables team members to collaborate efficiently, preventing conflicts and resource duplication.

Q4: Can you explain state file locking in Terraform?

State file locking occurs when Terraform locks the state file during operations like planapply, or destroy. This prevents multiple users or processes from making simultaneous changes, reducing the risk of conflicting actions that could damage the infrastructure.

Q5: What exactly is a Terraform backend?

A backend in Terraform refers to the configuration that determines where and how the state file is stored. It manages the persistence of state data, enabling Terraform to track and update resources consistently.

Q6: What is the function of a Terraform plugin?

A plugin is a crucial component in Terraform that translates the high-level configuration (HCL) into API calls. These calls interact with the underlying cloud providers like AWS, Azure, or Google Cloud, allowing Terraform to manage resources.

Q7: What is a null resource in Terraform?

A null resource in Terraform doesn’t correspond to any actual infrastructure component. It is typically used for tasks such as running shell commands, integrating with provisioners, or handling logic without provisioning a physical resource in the cloud. For example, it can be used in a local-exec provisioner, with modules, or for output blocks.

Q8: What are the different types of provisioners available in Terraform?

  • Remote-exec: Executes commands on a remote system.
  • Local-exec: Executes commands locally on the machine running Terraform.

These provisioners can be used to execute scripts or perform tasks after the resource creation process.

Q9: Why should you use Terraform modules?

Modules in Terraform allow you to create reusable components for common infrastructure patterns. They help in:

  • Reducing code duplication.
  • Standardizing infrastructure definitions.
  • Enabling versioning for better change management.

Q10: How can you recover from a deleted Terraform state file (if not backed up to S3 or DynamoDB)?

If the state file is lost, you can use the terraform import command to re-import the existing resources into the state file. This will allow Terraform to manage those resources again without requiring re-creation.

Q11: How does Terraform determine the order of resource deployment when multiple modules are used (e.g., EC2, VPC, security group)?

Terraform automatically determines the dependency order based on resource references within the code. It constructs a dependency graph and uses this to manage resource creation in the correct sequence. For explicit control over the order, you can use the depends_on parameter.

Q12: How do you delete a specific resource without affecting other infrastructure in Terraform?

You can use the terraform taint command to mark a resource as needing replacement. Then, use the terraform destroy -target command to destroy only the targeted resource without impacting others.

Q13: How can you rename a resource in Terraform without deleting it?

To rename a resource in Terraform without destroying it, use the terraform mv command. This will update the state file with the new resource name without losing any configuration.

Q14: What happens if you make manual changes to a resource managed by Terraform and then run terraform plan?

When manual changes are made to a resource, Terraform detects the discrepancy between the resource’s current state and the state defined in the configuration. During the next terraform plan, it will propose changes to bring the resource back to the state defined in the .tf files.

Q15: What is the difference between locals and variables in Terraform?

  • Variables are defined in variables.tf or with the variable block and can be overridden by the user.
  • Locals are used to define values that are computed within a module or configuration and cannot be overridden by external input. Locals are helpful when you want to restrict changes and maintain internal consistency.