Skip to main content

Command Palette

Search for a command to run...

Day 71: Let's Prepare for Some Interview Questions on Terraform 🔥

Day#71 Of 90 Days Of DevOps Challenge

Published
6 min read
Day 71: Let's Prepare for Some Interview Questions on Terraform 🔥
V

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!

💥Introduction

Welcome to Day 71 of our journey to become Terraform experts. Today, we're going to dive deep into some interview questions related to Terraform.

These questions will test your knowledge and understanding of Terraform, its components, and best practices. Let's get started!

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

Terraform is an Infrastructure as Code (IaaC) tool that allows you to define and provision infrastructure using a declarative configuration language. It provides a way to create, modify, and manage infrastructure resources such as virtual machines, networks, and databases.

Terraform's key differentiators are:

  • Declarative Syntax: Terraform uses a declarative configuration language, HashiCorp Configuration Language (HCL), which allows you to define what your infrastructure should look like rather than specifying how to achieve it.

  • Resource Graph: Terraform creates a resource graph that determines the order in which resources are created or updated, ensuring dependencies are managed correctly.

  • State Management: Terraform maintains a state file to keep track of the current infrastructure. This allows it to identify changes and plan updates accordingly.

  • Provider Ecosystem: Terraform has a wide range of providers (e.g., AWS, Azure, GCP) that allow you to manage resources on various cloud platforms.

✔2. How do you call a main.tf module?

In Terraform, there is no special main.tf module. The main configuration file can have any name (e.g., main.tf, infrastructure.tf).

Terraform automatically loads and processes all configuration files with a .tf extension in the working directory.

To execute the configuration, you typically run the terraform init, terraform plan, and terraform apply commands.

✔3. What exactly is Sentinel, and can you provide a few examples where we can use Sentinel policies?

Sentinel is a policy as code framework developed by HashiCorp. It is used for defining and implementing policies that can be enforced across various HashiCorp tools, including Terraform.

Examples of where you can use Sentinel policies in Terraform:

  • Security Policies: You can define policies to ensure that only approved security groups or network configurations are applied.

  • Cost Control: Enforce policies that prevent the deployment of expensive resources or enforce budget limits.

  • Naming Conventions: Define naming conventions for resources to maintain consistency across your infrastructure.

  • Compliance and Governance: Implement policies to ensure that all infrastructure configurations comply with regulatory standards.

✔4. You have a Terraform configuration file that defines an infrastructure deployment with multiple instances of the same resource. How would you modify the configuration file to achieve this?

You can use resource blocks with count or for_each to create multiple instances of the same resource. Here's an example using count:

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

In this example, three AWS instances will be created. You can change the value of count to control the number of instances.

✔5. You want to know from which paths Terraform is loading providers referenced in your Terraform configuration (*.tf files). You need to enable debug messages to find this out. Which of the following would achieve this?

The correct option to enable debug messages for provider loading is:

A. Set the environment variable TF_LOG=TRACE

Setting TF_LOG=TRACE will provide detailed debugging information, including provider loading paths, in the Terraform output.

✔6. "Destroy" command will destroy everything that is being created in the infrastructure. Tell us how you would save any particular resource while destroying the complete infrastructure.

The terraform destroy command will indeed destroy all resources defined in your Terraform configuration. To save a particular resource while destroying the complete infrastructure, you can use the -target flag. For example:

terraform destroy -target=aws_instance.example[0]

In this example, only the first instance defined in your configuration will be destroyed, while the rest of the resources will remain intact.

✔7. Which module is used to store .tfstate file in S3?

The module used to store the Terraform state file (.tfstate) in an S3 bucket is called terraform-backend-s3. This module can be configured to store your state file in an S3 bucket, allowing for remote state management.

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

Sensitive data in Terraform can be managed using various methods:

  • Sensitive Input Variables: Use sensitive input variables to securely pass sensitive information to your modules.

  • Vault Integration: HashiCorp Vault can be integrated to store and retrieve secrets securely.

  • Environment Variables: Store sensitive data as environment variables and reference them in your Terraform configuration.

  • Provider-Specific Secrets Management: Some providers offer native ways to manage secrets, like AWS Secrets Manager or Azure Key Vault.

✔9. You are working on a Terraform project that needs to provision an S3 bucket and a user with read and write access to the bucket. What resources would you use to accomplish this, and how would you configure them?

To provision an S3 bucket and a user with read and write access in Terraform, you would typically use the following resources:

resource "aws_s3_bucket" "example_bucket" {
  bucket = "my-example-bucket"
  acl    = "private"
}

resource "aws_iam_user" "example_user" {
  name = "example-user"
}

resource "aws_iam_access_key" "example_user_key" {
  user = aws_iam_user.example_user.name
}

resource "aws_s3_bucket_policy" "example_bucket_policy" {
  bucket = aws_s3_bucket.example_bucket.id

  policy = jsonencode({
    Version = "2012-10-17",
    Statement = [
      {
        Action = "s3:*",
        Effect = "Allow",
        Resource = aws_s3_bucket.example_bucket.arn,
        Principal = {
          AWS = aws_iam_user.example_user.arn
        }
      }
    ]
  })
}

This configuration creates an S3 bucket, an IAM user, and attaches a policy to the bucket that grants the user read and write access.

✔10. Who maintains Terraform providers?

Terraform providers are maintained by the respective cloud service providers or community contributors. HashiCorp, the company behind Terraform, also provides and maintains several official providers. Community providers are typically open-source and maintained by volunteers and contributors.

✔11. How can we export data from one module to another?

In Terraform, you can export data from one module to another using output variables. Here's how you can do it:

In the source module (the one you want to export data from):

output "example_output" {
  value = "This is the data to be exported"
}

In the destination module (where you want to use the exported data):

module "source" {
  source = "./path/to/source_module"
}

variable "imported_data" {
  type = any
}

resource "example_resource" "

example" {
  data_to_use = module.source.example_output
}

You can access the exported data from the source module as module.source.example_output in the destination module.

🌈Conclusion

That wraps up our Terraform interview questions for today.

I hope you found this information useful as you prepare for Terraform-related interviews. Keep practicing and learning, and you'll become a Terraform pro in no time! 🔥

Happy learning & may your DevOps journey be filled with exciting discoveries🎊

Thank you for joining us on this exciting Day 71 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashnode.dev

LinkedIn: linkedin.com/in/vishalphadnis