Terraform Interview Questions

  1. What is Terraform?
    Terraform is an infrastructure as code (IaC) tool for building, changing, and versioning infrastructure efficiently.
  2. What are the advantages of using Terraform or IaC in general?
  • Automation of infrastructure provisioning
  • Consistency and standardization
  • Scalability
  • Collaboration facilitation
  • Error reduction
  • Version control integration
  1. What are some of Terraform features?
  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation
  • Modularity with Modules
  • State Management
  1. What language does Terraform use?
    Terraform uses its own language called HCL (HashiCorp Configuration Language).
  2. What’s a typical Terraform workflow?
  • Write Terraform definitions: .tf files written in HCL that described the desired infrastructure state (and run terraform init at the very beginning)
  • Review: With command such as terraform plan you can get a glance at what Terraform will perform with the written definitions
  • Apply definitions: With the command terraform apply Terraform will apply the given definitions, by adding, modifying or removing the resources
  1. What are some use cases for using Terraform?
  • Multi-cloud deployments
  • Self-service clusters
  • Development environment setup
  • Resource scaling
  • Infrastructure audit and compliance
  1. What’s the difference between Terraform and technologies such as Ansible, Puppet, Chef, etc.?
    Terraform is primarily focused on infrastructure provisioning while Ansible, Puppet, Chef, etc., are configuration management tools focused on software and configuration on existing servers. Terraform can be used to provision the servers that configuration management tools then configure. Terraform is immutable, whereas the others can be mutable.

8.Explain the following block of Terraform code

resource "aws_instance" "some-instance" {
  ami           = "ami-201720221991yay"
  instance_type = "t2.micro"
}

This Terraform code defines an AWS EC2 instance resource named "some-instance" with a specified AMI ID "ami-201720221991yay" and instance type "t2.micro".

9. What do you do next after writing the following in main.tf file?

resource "aws_instance" "some-instance" {
  ami           = "ami-201720221991yay"
  instance_type = "t2.micro"
}

Run terraform init. This will scan the code in the directory to figure out which providers are used (in this case AWS provider) and will download them.

10. You’ve executed terraform init and now you would like to move forward to creating the resources but you have concerns and would like to make be 100% sure on what you are going to execute. What should you be doing?

Execute terraform plan. That will provide a detailed information on what Terraform will do once you apply the changes.

11. You’ve downloaded the providers, seen the what Terraform will do (with terraform plan) and you are ready to actually apply the changes. What should you do next?

Run terraform apply. That will apply the changes described in your .tf files.

12. Explain the meaning of the following strings that seen at the beginning of each line When you run terraform apply

  • ‘+’
  • ‘-‘
  • ‘-/+’
  • ‘+’ – The resource or attribute is going to be added
  • ‘-‘ – the resource or attribute is going to be removed
  • ‘-/+’ – the resource or attribute is going to be replaced

13. How to cleanup Terraform resources? Why the user should be careful doing so?
terraform destroy will cleanup all the resources tracked by Terraform.

A user should be careful with this command because there is no way to revert it. Sure, you can always run again “apply” but that can take time, generates completely new resources, etc.

Dependencies

14. Sometimes you need to reference some resources in the same or separate .tf file. Why and how it’s done?

Why: Resources are referenced to establish dependencies and relations, such as attaching a security group to an EC2 instance to control its traffic.

How it’s done: Use the resource type and name to reference attributes of another resource.

Example:

resource "aws_security_group" "example_sg" {
  # ... security group configuration ...
}

resource "aws_instance" "example" {
  ami                    = "some-ami"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.example_sg.id]  # Reference to the security group's ID
}

In this example, the security group example_sg is defined first, and its ID is referenced in the aws_instance to associate the two resources.

15. Does it matter in which order Terraform creates resources?
Yes, when there is a dependency between different Terraform resources, you want the resources to be created in the right order and this is exactly what Terraform does.

To make it ever more clear, if you have a resource X that references the ID of resource Y, it doesn’t makes sense to create first resource X because it won’t have any ID to get from a resource that wasn’t created yet.

16. Is there a way to print/see the dependencies between the different resources?
Yes, with terraform graph

Providers

17. Explain what is a “provider”

Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs…Each provider adds a set of resource types and/or data sources that Terraform can manage. Every resource type is implemented by a provider; without providers, Terraform can’t manage any kind of infrastructure.

18. Where can you find publicly available providers?

In the Terraform Registry

19. What are the names of the providers in this case?

terraform {
    required_providers {
      aws = {
        source  = "hashicorp/aws"
      }
      azurerm = {
        source  = "hashicorp/azurerm"
        version = "~> 3.0.2"
      }
    }
  }

azurerm and aws

20. You write a provider block like the following one and run terraform init

provider "aws" {
  region = "us-west-1"
}

21. Write a configuration of a Terraform provider (any type you would like)
AWS is one of the most popular providers in Terraform. Here is an example of how to configure it to use one specific region and specifying a specific version of the provider

terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.0”
}
}
}

# Configure the AWS Provider

provider “aws” {
region = “us-west-2”
}

22. Where Terraform installs providers from by default?

By default Terraform providers are installed from Terraform Registry

23. What is the Terraform Registry?

The Terraform Registry provides a centralized location for official and community-managed providers and modules.

24. Where providers are downloaded to? (when for example you run terraform init)

.terraform directory.

23. Describe in high level what happens behind the scenes when you run terraform init on on the following Terraform configuration

terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.0”
}
}
}

One thought on “Terraform Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *