Mastering Terraform : Interview Questions and Answers

Mastering Terraform : Interview Questions and Answers

ยท

4 min read


1. What is Terraform and how is it different from other IaaC tools?

Answer : Terraform is a tool for building and managing infrastructure.

Unlike other tools, it uses a language called HCL (HashiCorp Configuration Language) to define the desired state of your infrastructure. It can manage resources across multiple cloud providers, which some tools cannot.

2. How do you call a module in Terraform?

Answer : You call a module by using the module block in your configuration file. Specify the path to the module and pass any needed variables.

Example:

module "example" {
  source = "./path_to_module"
  variable1 = "value1"
  variable2 = "value2"
}

3. What is Sentinel in Terraform?

Answer: Sentinel is a tool that helps enforce rules and policies for your Terraform configurations.

For example, it can ensure all resources have specific tags or restrict certain resource types.

4. How do you create multiple instances of the same resource in Terraform?

Answer: Use the count parameter to specify how many instances of a resource you want to create.

Example:

resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

5. How do you enable debug messages in Terraform to find out from which paths Terraform is loading providers?

Answer: Set the environment variable TF_LOG=TRACE.

Example:

export TF_LOG=TRACE

6. How do you save a particular resource while destroying the complete infrastructure?

Answer: Use the -target option to specify which resource to destroy.

Example:

terraform destroy -target=aws_instance.example

7. How do you store the .tfstate file in an S3 bucket?

Answer: Use the backend configuration in your Terraform code.

Example:

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "path/to/my/key"
    region = "us-west-2"
  }
}

8. How do you manage sensitive data in Terraform, such as API keys or passwords?

Answer: Use environment variables or secret management services like HashiCorp Vault or AWS Secrets Manager.

Example using environment variables:

variable "db_password" {
  type = string
}

resource "aws_db_instance" "example" {
  engine         = "mysql"
  instance_class = "db.t2.micro"
  username       = "admin"
  password       = var.db_password
}

Set the environment variable:

export TF_VAR_db_password="mysecretpassword"

9. How do you create an S3 bucket and a user with read and write access in Terraform?

Answer: Use aws_s3_bucket, aws_iam_user, aws_iam_policy, and aws_iam_policy_attachment resources.

Example:

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-name"
  acl    = "private"
}

resource "aws_iam_user" "my_user" {
  name = "my_user"
}

resource "aws_iam_policy" "s3_policy" {
  name = "s3_policy"
  policy = jsonencode({
    Version = "2012-10-17",
    Statement = [
      {
        Effect = "Allow",
        Action = ["s3:ListBucket"],
        Resource = [aws_s3_bucket.my_bucket.arn]
      },
      {
        Effect = "Allow",
        Action = ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
        Resource = ["${aws_s3_bucket.my_bucket.arn}/*"]
      }
    ]
  })
}

resource "aws_iam_policy_attachment" "s3_attach" {
  policy_arn = aws_iam_policy.s3_policy.arn
  users      = [aws_iam_user.my_user.name]
}

10. Who maintains Terraform providers?

Answer: Terraform providers are maintained by HashiCorp, the community, and third-party organizations.

11. How can you export data from one module to another in Terraform?

Answer: Use output in the source module and reference it in the calling module.

Example:

Source Module (module_a)

output "instance_id" {
  value = aws_instance.example.id
}

Calling Module (main.tf)

module "module_a" {
  source = "./module_a"
}

module "module_b" {
  source      = "./module_b"
  instance_id = module.module_a.instance_id
}

Additional Questions:

12. What is a Terraform Provider?

Answer: A provider is a plugin that Terraform uses to interact with APIs of cloud providers and other services.

13. How do you handle dependencies between resources in Terraform?

Answer: Terraform automatically handles dependencies based on resource references.

You can also use depends_on to specify explicit dependencies.

Example:

resource "aws_instance" "example" {
  # Configuration...
}

resource "aws_eip" "ip" {
  instance = aws_instance.example.id
  depends_on = [aws_instance.example]
}

14. What is Terraform state file?

Answer: The state file tracks the current state of your infrastructure, mapping your configurations to real-world resources.

15. How do you upgrade Terraform providers?

Answer: Update the version in the required_providers block and run terraform init -upgrade.

Example:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.0.0"
    }
  }
}
ย