- Devops Interviews
- Posts
- Terraform Interview Questions [Junior level - S1E5]
Terraform Interview Questions [Junior level - S1E5]
Here’s another set of 10 junior-level Terraform interview questions. These questions focus on key concepts and best practices while keeping things casual and approachable.

1. How does Terraform handle resource updates when the configuration changes?
Approach:
Terraform’s ability to detect configuration changes is crucial. When a change is made to the configuration, Terraform will figure out if the resource needs to be updated, replaced, or left alone based on the change.
Answer:
Terraform compares the current configuration to the state and determines whether to update or recreate a resource. If a resource needs to be updated (e.g., a change in the instance type for an EC2), Terraform will execute an in-place update. However, if the resource cannot be updated in place, it will be replaced. You can always review these changes with terraform plan.
Example:
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro" # Changing this value will trigger a replacement
}
2. What is the purpose of terraform output?
Approach:
You often need to expose specific values from your Terraform configurations after resources are created. terraform output is helpful for this purpose.
Answer:
The terraform output command allows you to retrieve values from the Terraform state, which can be useful for sharing information between different parts of your configuration or for passing to other tools. For example, if you want to fetch the public IP of an EC2 instance after it’s created, you can use terraform output:
Example:
terraform output public_ip
3. What are Terraform variables, and how do you use them?
Approach:
Variables allow you to parameterize your Terraform configurations, making them more flexible and reusable. It's like creating templates for your infrastructure.
Answer:
Terraform variables allow you to define dynamic values in your configuration files. You can specify values when applying your configuration, and use them in your resource definitions. Variables can be defined in .tf files or provided via command-line arguments or .tfvars files.
Example:
variable "region" {
default = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
region = var.region
}
4. What is the difference between terraform init and terraform apply?
Approach:
These two commands are fundamental to any Terraform workflow, but they serve different purposes. Knowing the distinction is key for managing your infrastructure.
Answer:
terraform init: This command initializes your Terraform working directory. It installs required provider plugins, initializes the backend, and prepares your environment for use.terraform apply: This command applies the changes described in your Terraform configuration. It will ask for confirmation before making any changes to your infrastructure.
Example:
terraform init # Initializes the directory and installs providers
terraform apply # Applies the changes to your infrastructure
5. How would you approach using Terraform with version control?
Approach:
Version control is important in infrastructure as code. It helps in managing different versions of your Terraform code and tracking changes.
Answer:
You should store your Terraform configuration files in a version control system like Git. It helps in tracking changes to infrastructure, collaborating with your team, and ensuring that your infrastructure is reproducible.
Things to keep in mind:
State Files: Never commit
terraform.tfstateorterraform.tfstate.backupto version control. These files contain sensitive data. Use.gitignoreto exclude them.Modules: Store reusable modules in separate repositories or directories for versioning.
6. What is a Terraform module and why would you use one?
Approach:
Modules are a key concept for structuring Terraform code. They make your code reusable, maintainable, and easier to scale.
Answer:
A module in Terraform is a container for multiple resources that are used together. Modules help organize your code into reusable pieces. You can define a module in a separate directory and reuse it in multiple places within your configurations.
Example:
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
7. How can you handle different configurations for different environments (dev, prod) in Terraform?
Approach:
Handling multiple environments is essential in real-world scenarios. You can manage different environments by structuring your Terraform code in a way that separates configurations logically.
Answer:
You can use different approaches to handle multiple environments:
Workspaces: Terraform workspaces allow you to isolate different environments while using the same configuration. Each workspace has its own state.
terraform workspace new dev terraform workspace select devSeparate directories: Another way is to create separate directories for each environment, each with its own
.tfvarsfiles or specific variable values.
8. How does Terraform handle state files, and why is state management important?
Approach:
State files track the resources Terraform manages. It’s crucial to understand how Terraform uses state files, especially in team environments or large infrastructures.
Answer:
Terraform uses a state file (terraform.tfstate) to keep track of the resources it manages. The state file is important because it helps Terraform understand what is currently deployed, and it’s used to calculate changes during terraform apply. Managing state is critical, especially in team environments, to avoid conflicts and ensure consistency.
Remote backends (like AWS S3 or Terraform Cloud) are often used to store the state remotely, ensuring it's shared and versioned across teams.
9. What is the terraform destroy command, and when would you use it?
Approach:
The terraform destroy command is used to tear down infrastructure managed by Terraform. It’s important to use it carefully because it will delete all resources.
Answer:
The terraform destroy command is used to destroy all the infrastructure defined in your Terraform configuration. It’s useful when you want to clean up resources or when you’re working in non-production environments. However, be cautious, as it will delete everything.
Example:
terraform destroy
10. How would you handle a situation where Terraform fails to apply changes to a resource?
Approach:
Terraform failures can happen due to various reasons, such as incorrect configurations or issues with the cloud provider. Knowing how to troubleshoot failures is crucial for ensuring smooth deployments.
Answer:
If Terraform fails to apply changes, you can follow these steps:
Check the error message: Terraform usually provides a descriptive error message that explains what went wrong.
Run
terraform planagain: Sometimes, re-runningterraform plancan give you more details on the issue.Check for dependencies: Ensure that your resources are correctly dependent on each other. Sometimes, resources need to be created in a specific order.
Manual intervention: If necessary, you may need to manually fix the issue in the provider’s interface and then run
terraform refreshto sync the state.
This set of 10 questions dives deeper into essential Terraform concepts. Understanding these will help you be better prepared for interviews, especially when it comes to day-to-day Terraform usage and troubleshooting.