Terraform Scenario Based Interview Questions and Answers | DevOps Interview

Cloud Champ
24 Apr 202425:29

Summary

TLDRThis video script offers an in-depth exploration of common Terraform interview questions, providing detailed answers and learning resources. It covers Terraform's basics, state file management, multi-cloud strategies, CI/CD integration, and secret handling. The script also delves into advanced topics like module usage, count feature advantages, and automated testing. Viewers are encouraged to request the document for a comprehensive guide and to suggest future topics like Docker or Kubernetes.

Takeaways

  • 📝 Terraform is an Infrastructure as Code (IaC) tool that allows you to script infrastructure in the cloud using configuration files (TF files).
  • 🔍 The state file in Terraform is crucial as it stores the current state of the managed infrastructure and is used to compare with the desired state to make updates.
  • 🛠 To manage infrastructure created manually, you can use Terraform by first writing the configuration and then using the `terraform import` command to import existing resources into Terraform management.
  • 🔄 Terraform supports multi-environment setups using either modules or workspaces, allowing the same code to be used with different configurations for various environments.
  • 🗂️ The Terraform state file is a JSON or binary file that is vital for Terraform operations, and best practices include using remote storage, state locking, access control, automated backups, and environment separation.
  • 💡 In case of accidental deletion of a state file, recovery from backups is the first step; if not available, manual recreation using `terraform import` is necessary.
  • 🌐 When adopting a multicloud strategy, Terraform's cloud-agnostic nature allows managing resources across different cloud providers like AWS and Azure by defining respective providers.
  • 🔧 Provisioners in Terraform can execute scripts post-resource creation, with options for local and remote execution depending on the requirement.
  • 🚦 Blue-green deployments can be facilitated by Terraform by defining two sets of infrastructure resources and switching traffic between them using load balancers or DNS.
  • 🔄 Integration of Terraform with CI/CD pipelines involves committing Terraform code to a repository, setting up pipelines for validation, planning, applying, and optionally, testing and deployment stages.
  • 🔒 Managing secrets in Terraform is crucial, and best practices include avoiding hardcoding secrets in the code, using secret management tools, and passing sensitive data through input variables or environment variables.

Q & A

  • What is Terraform and how does it work?

    -Terraform is an Infrastructure as Code (IaC) tool that allows you to define your infrastructure using code. You write code for the infrastructure you want to create, then run Terraform commands to have it created in the cloud. Terraform uses a state file to store data on all the infrastructure it manages, comparing this with the actual cloud infrastructure to determine what needs to be created or deleted according to your configuration.

  • How can you import existing infrastructure into Terraform management?

    -To import existing infrastructure into Terraform, you first write the Terraform configuration for the resources. Then, you run the 'terraform import' command for each resource, specifying the resource type and its unique identifier. This adds the infrastructure to the Terraform state file, allowing it to be managed by Terraform.

  • How can you use the same Terraform code for multiple environments?

    -You can use the same Terraform code for multiple environments by using Terraform modules, which are reusable code templates, or Terraform workspaces, which allow for separate state files for different environments using the same codebase. Modules enable the use of different configurations through parameters or variables, while workspaces maintain separate state files for each environment.

  • What is the importance of the Terraform state file?

    -The Terraform state file is crucial as it stores the current state of the managed infrastructure. It acts as a blueprint, helping Terraform understand what's already set up and what changes need to be made by comparing the desired state with the current one in the state file, allowing for accurate updates to the infrastructure.

  • What steps should be taken if a Terraform state file is accidentally deleted?

    -If a Terraform state file is accidentally deleted, the first step is to recover from a backup if available. If no backup exists, you need to manually recreate the state file using the 'terraform import' command for each resource on the cloud, which can be time-consuming. It's essential to review and monitor the recreated state to ensure everything is working correctly.

  • What are some best practices for managing a Terraform state file?

    -Best practices for managing a Terraform state file include using remote storage like S3 or Azure Blob Storage, enabling state locking to prevent conflicts in concurrent operations, ensuring access control to limit access to authorized personnel, setting up automated backups to prevent data loss, and maintaining separate state files for different environments or using Terraform workspaces.

  • How can you structure your Terraform code to handle a multicloud strategy?

    -For a multicloud strategy, you define the providers for each cloud you intend to use, such as AWS and Azure. Then, you write the code for the resources you want to create in each cloud. Terraform being cloud-agnostic allows you to manage resources across different cloud providers simultaneously.

  • How can you run scripts after creating resources with Terraform?

    -You can run scripts after creating resources with Terraform using provisioners. Local and remote exec provisioners can be used to run commands or scripts locally or on remote machines, respectively. You define the provisioner in your Terraform configuration and specify the connection details if running remotely.

  • Can you perform blue-green deployments using Terraform and how?

    -Yes, Terraform can be used to set up blue-green deployments by defining two sets of infrastructure resources with slight variations. You create a new environment alongside the existing one and test it. If everything works as expected, you switch traffic using load balancers or DNS records to direct traffic to the new environment.

  • How can you integrate Terraform with CI/CD pipelines?

    -Terraform can be integrated into CI/CD pipelines by committing the Terraform code to a version control system and setting up a pipeline that executes Terraform commands such as 'init', 'validate', and 'plan'. The pipeline can be configured with stages for applying approved changes and optionally include testing and verification tools for syntax correctness.

  • How can you use Terraform with configuration management tools like Ansible or Chef?

    -Terraform can be used in conjunction with configuration management tools like Ansible or Chef. Terraform focuses on infrastructure provisioning, while tools like Ansible or Chef handle configuration tasks such as software installation, server configuration, and service management. This combination provides a comprehensive solution for infrastructure automation.

  • How can you manage secrets and sensitive data in Terraform?

    -To manage secrets in Terraform, you should never hardcode sensitive information in your Terraform code or push it to version control systems. Instead, use tools like HashiCorp Vault or cloud-specific secret management services like AWS Secrets Manager. Alternatively, you can use Terraform input variables or environment variables to pass sensitive information at runtime.

  • How can you specify dependencies between resources in Terraform?

    -In Terraform, you can specify dependencies between resources using the 'depends_on' attribute within the resource block. This allows you to define that one resource should be created before another, ensuring the correct order of operations.

  • Is it possible to destroy a single resource out of multiple resources using Terraform?

    -Yes, it is possible to destroy a single resource out of multiple resources using Terraform. You can use the 'terraform destroy' command with a '-target' attribute followed by the resource type and name to destroy a specific resource without affecting others.

  • What are the advantages of using Terraform's 'count' feature over resource duplication?

    -Using Terraform's 'count' feature over resource duplication allows you to dynamically create multiple instances of a resource based on a given condition or variable. It reduces code duplication, enables more efficient resource management, and promotes scalability.

  • What is Terraform's module registry and how can it be leveraged?

    -Terraform's module registry is a central repository for sharing and discovering Terraform modules. It allows users to publish and reuse modules, which are reusable and sharable components of Terraform configurations. By leveraging the module registry, you can easily discover existing modules that address your infrastructure needs, reducing the need for duplication of work.

  • How can you implement automated testing for Terraform code?

    -Automated testing for Terraform code can be implemented using tools like Terratest, TFLint, Kitchen-Terraform, and others. These tools help validate syntax, detect issues early, ensure the desired state matches the actual state, and involve creating test fixtures, defining test scenarios, and executing Terraform operations.

  • What considerations should be taken when migrating from one Terraform version to another?

    -When migrating Terraform versions, review the upgrade guide to understand changes, deprecations, and new features. Update configuration files accordingly, ensure thorough testing, monitor changes in non-prod environments before moving to production, document any changes, and provide training to team members.

Outlines

00:00

📘 Terraform Interview Questions Overview

This paragraph introduces the video's focus on the most frequently asked Terraform interview questions along with their answers and learning resources. The speaker presents a collection of real-time scenario-based and advanced Terraform questions, such as those related to CI/CD integration, automated testing, and version upgrades. Viewers are encouraged to watch the video in its entirety and request a PDF document containing the questions and answers for further study.

05:00

🛠 Terraform Basics and Importing AWS Resources

The speaker explains the fundamentals of Terraform as an Infrastructure as Code (IaC) tool, detailing its workflow from writing code to applying changes on cloud infrastructure using state files. A scenario-based question is addressed, illustrating how to import manually created AWS resources into Terraform's management through the configuration and execution of the 'terraform import' command.

10:02

🌐 Managing Multiple Environments with Terraform

This section discusses strategies for using the same Terraform code across multiple environments, such as development and production stages. Two approaches are highlighted: Terraform modules, which allow for code reuse with different configurations, and Terraform workspaces, which enable separate state files for each environment using the same code base. The importance of understanding these methods for efficient infrastructure management is emphasized.

15:03

🗂 Understanding the Importance of Terraform State Files

The role of the Terraform state file is explored, describing it as a critical component that stores information about managed infrastructure. The paragraph covers the consequences of a junior DevOps engineer accidentally deleting a state file and the steps to recover or recreate it using backups or the 'terraform import' command. Best practices for managing state files, including remote storage, state locking, and automated backups, are also presented.

20:04

🔌 Terraform in a Multicloud Strategy

The paragraph delves into structuring Terraform code to manage resources across multiple cloud providers, such as AWS and Azure, in the context of a multicloud strategy. It explains how to define providers and write code for resources on different clouds, emphasizing Terraform's cloud-agnostic nature and its ability to manage infrastructure across various platforms.

25:04

🔧 Running Bash Scripts Post-Terraform Provisioning

This section describes how to execute bash scripts after resource creation with Terraform using provisioners. It differentiates between local and remote exec provisioners and provides an example of using a remote exec provisioner to run commands on a remote machine via SSH. The importance of defining connection details for remote execution is also highlighted.

🚀 Blue-Green Deployments with Terraform

The concept of blue-green deployments is introduced as a strategy for high availability, where two identical environments are maintained. The paragraph explains how Terraform can be used to define and manage infrastructure for both environments, allowing for traffic switching between them using load balancers or DNS records to ensure minimal downtime during updates.

🔄 Integrating Terraform with CI/CD Pipelines

The importance of automating Terraform through CI/CD pipelines is discussed, with a focus on best practices for integrating Terraform with GitLab CI/CD. The process involves committing Terraform code to a remote repository, setting up a CI/CD pipeline with stages for planning, applying, and destroying infrastructure, and ensuring thorough testing and monitoring at each stage.

🤝 Combining Terraform with Configuration Management Tools

This paragraph explores the synergy between Terraform and configuration management tools like Ansible or Chef. It explains how Terraform can be used for infrastructure provisioning while Ansible and Chef handle configuration management tasks. The benefits of using these tools in conjunction for a comprehensive automation solution are emphasized.

🔒 Managing Secrets and Sensitive Data in Terraform

The paragraph addresses the critical issue of managing secrets and sensitive data within Terraform, outlining best practices to protect this information. It advises against hardcoding secrets in Terraform code and suggests using external tools like HashiCorp Vault or cloud-specific secret management services, as well as input variables or environment variables for secure handling of sensitive information.

➡️ Specifying Dependencies Between Terraform Resources

The concept of specifying dependencies between Terraform resources is explained, using the example of creating an EC2 instance before an RDS database. The 'depends_on' meta-argument is introduced as a method to define the order of resource creation, ensuring that dependent resources are managed correctly.

🗑️ Destroying Individual Resources in Terraform

This section discusses the possibility of selectively destroying individual resources within a Terraform configuration, rather than deleting all resources. It explains how to use the 'terraform destroy' command with a target attribute to specify the exact resource to be destroyed, avoiding the deletion of multiple resources unintentionally.

🔢 Advantages of Terraform's 'count' Feature

The benefits of using Terraform's 'count' feature over resource duplication are highlighted. The 'count' meta-argument allows for the dynamic creation of multiple instances of a resource based on conditions or variables, reducing code duplication and enabling more efficient resource management. The advantages of this approach are compared to manually rewriting code blocks multiple times.

📚 Terraform Module Registry

The Terraform Module Registry is introduced as a central repository for discovering and sharing reusable Terraform modules. The paragraph explains how leveraging the module registry can reduce the need for writing custom code by allowing users to reference and incorporate existing modules into their Terraform configurations, streamlining the infrastructure setup process.

🧪 Implementing Automated Testing for Terraform Code

The importance of automated testing in Terraform code is emphasized, with a discussion on the available tools and methods for implementing testing practices. The paragraph covers the use of Terraform's built-in commands like 'terraform validate' and 'terraform format', as well as third-party tools for linting, unit testing, integration testing, and static analysis to ensure code quality and infrastructure accuracy.

🔄 Migrating Terraform Infrastructure Across Versions

This paragraph addresses the process of migrating Terraform infrastructure from one version to another, specifically from version 1.7 to 1.8. It outlines the steps for a successful migration, including reviewing the upgrade guide, updating configuration files, conducting thorough testing, monitoring changes, and providing training to team members to ensure a smooth transition.

🗣️ Closing Remarks and Future Content Suggestions

The video concludes with an invitation for viewers to share their thoughts, ask questions, or request the document containing the interview questions and answers. The speaker also seeks feedback on what topics to cover in future videos, such as Docker or Kubernetes interview questions, demonstrating a commitment to providing valuable and relevant content.

Mindmap

Keywords

💡Terraform

Terraform is an Infrastructure as Code (IaC) tool that enables the creation and management of infrastructure through code. It is central to the video's theme, as it discusses various Terraform interview questions and answers. The script mentions Terraform's ability to define infrastructure in configuration files and interact with cloud providers to bring that infrastructure to life.

💡State File

A state file in Terraform is a crucial component that stores the current state of the managed infrastructure. It is used by Terraform to understand what changes need to be made to the infrastructure based on the desired state defined in the configuration files. The script emphasizes its importance and discusses how to handle scenarios where the state file is deleted or needs to be recreated.

💡Terraform Import

The Terraform import command is used to bring existing infrastructure under Terraform management. The script explains how to use this command to manage infrastructure that was previously created manually, by writing the necessary Terraform configuration and then using the import command with the resource type and unique ID.

💡Terraform Modules

Terraform modules are reusable code templates for infrastructure components. They allow for the creation of a single module that can be used with different configurations for various environments. The script discusses using modules as a way to use the same code for multiple environments by passing different parameters or variables.

💡Terraform Workspaces

Terraform workspaces provide a way to manage separate states for the same set of configuration files. Each workspace maintains its own state file, allowing for concurrent work on different environments without interference. The script mentions workspaces as an alternative approach to using modules for managing multiple environments.

💡CI/CD

CI/CD, or Continuous Integration/Continuous Deployment, is a practice of automating the software development process. The script discusses integrating Terraform with CI/CD pipelines to automate infrastructure provisioning. It provides an example of using GitLab CI/CD to automate Terraform's resource creation and deletion.

💡Provisioners

In Terraform, provisioners are used to run scripts or commands on a machine after resource creation. The script explains how to use local and remote exec provisioners to run bash scripts after creating resources with Terraform, highlighting the difference between running commands locally or on remote machines.

💡Blue-Green Deployment

Blue-green deployment is a strategy that involves maintaining two identical environments, one active (blue) and one idle (green). The script discusses using Terraform to set up such an environment, allowing for zero-downtime deployments by switching traffic between the two environments using load balancers or DNS records.

💡Count Meta Argument

The count meta argument in Terraform is used to specify the number of instances of a resource to create. The script explains the advantages of using the count feature over resource duplication, such as reducing code duplication and enabling more efficient resource management by dynamically creating multiple instances based on a variable or condition.

💡Terraform Registry

The Terraform Registry is a central repository for sharing and discovering Terraform modules. The script describes it as a place to find pre-built modules for various infrastructure components, which can be used instead of writing custom code, thus reducing development time and effort.

💡Automated Testing

Automated testing in Terraform involves using tools to validate the syntax and desired state of the infrastructure. The script mentions tools like Terratest, TFLint, and Kitchen-Terraform for implementing automated testing to ensure that the Terraform code is correct and that infrastructure changes are properly validated.

💡Terraform Version Migration

The process of migrating from one Terraform version to another is discussed in the script. It involves reviewing the upgrade guide, updating configuration files, ensuring thorough testing, monitoring changes, and providing training to team members. The script uses the migration from version 1.7 to 1.8 as an example to illustrate the steps involved in version migration.

Highlights

Introduction to Terraform interview questions and answers, along with learning resources.

Explanation of Terraform as an Infrastructure as Code (IaC) tool and its basic workflow.

How to import existing infrastructure into Terraform for management.

Using Terraform modules and workspaces for managing multiple environments with the same code.

The importance and function of the Terraform state file in managing infrastructure.

Steps to resolve the accidental deletion of a Terraform state file.

Best practices for managing the Terraform state file, including remote storage and state locking.

Structuring Terraform code for multicloud strategies involving AWS and Azure.

Executing bash scripts post-resource creation using Terraform provisioners.

Implementing blue-green deployments for high availability with Terraform.

Integrating Terraform with CI/CD pipelines for automated infrastructure provisioning.

Combining Terraform with configuration management tools like Ansible or Chef.

Managing secrets and sensitive data in Terraform without hardcoding.

Specifying dependencies between resources in Terraform to control creation order.

Destroying a single resource out of multiple using Terraform's target attribute.

Advantages of using Terraform's count feature over resource duplication.

Introduction to Terraform's module registry for discovering and reusing modules.

Implementing automated testing for Terraform code using various tools.

Considerations for migrating from one Terraform version to another, such as from 1.7 to 1.8.

Transcripts

play00:00

in this video we look at most asked

play00:02

terraform interview questions along with

play00:04

answers and also learning resources to

play00:06

help you out all right so I'm here on my

play00:08

computer screen and I have all this

play00:10

different terraform interview questions

play00:12

along with answers and also learning

play00:15

materials to help you understand each

play00:17

topic properly now in this document I

play00:19

have mostly realtime scenario based

play00:21

terraform interview questions but I've

play00:23

also included some Advanced terraform

play00:24

interview questions like cicd with

play00:26

terraform automated testing in terraform

play00:29

upgrading terraform version and lot more

play00:31

so make sure you watch this video till

play00:32

the end and if you want me to share this

play00:34

document with you comment down terrafirm

play00:37

interview questions and answers and I

play00:39

will share this document as a PDF on

play00:40

LinkedIn and also share the link of this

play00:43

document in the video description now

play00:46

with that being said let's get started

play00:47

with this terraform interview questions

play00:49

and answers video let's go so whenever

play00:52

you sit for a terraform interview the

play00:53

first typical question that you will get

play00:55

is going to be what is terraform and how

play00:58

it works so we all know terraform is an

play01:00

infrastructure ased code tool that lets

play01:02

you write the code for the

play01:03

infrastructure that you want to create

play01:05

in the cloud when you write the code you

play01:07

then run the terraform commands and

play01:09

terraform will create this

play01:10

infrastructure in the cloud terraform

play01:12

does this using the state file so a

play01:14

state file in terraform is a file that

play01:16

will store the data of all the

play01:18

infrastructure managed through terraform

play01:20

and terraform uses the state file to

play01:22

compare with the actual infrastructure

play01:24

on the cloud to tell us what is going to

play01:26

be created or deleted according to the

play01:28

configuration you have in these files so

play01:30

this is how terraform works and if you

play01:32

look at the answer here terraform is an

play01:34

IC tool that lets you write code to

play01:36

Define and manage your infrastructure

play01:37

you describe your desired infrastructure

play01:39

in the configuration files which are

play01:41

these files here the TF

play01:43

files and then terraform figures out

play01:46

what needs to be done to achieve the

play01:47

state and then make it happen by

play01:49

interacting with Cloud providers or

play01:51

other infrastructure platform here is

play01:53

the workflow you you first write the

play01:55

code you then plan to check what is

play01:57

going to be created or deleted and then

play01:59

you apply to have that infrastructure on

play02:01

the cloud so this is how terraform Works

play02:04

moving on the next question is a

play02:06

scenario based question where it says a

play02:08

devops engineer manually created

play02:10

infrastructure on AWS and now there is a

play02:12

requirement to use terraform to manage

play02:14

it how would you import this resources

play02:16

in the terraform code okay so according

play02:19

to this question engineer has created

play02:21

some infrastructure on AWS manually and

play02:24

now they want this infrastructure to be

play02:26

managed through terraform so how can you

play02:28

do that so let's consider either this is

play02:30

an infrastructure or an ec2 instance

play02:33

created manually through AWS console and

play02:35

now you want this to be managed through

play02:37

terraform so for an infrastructure to be

play02:39

managed through terraform it has to be

play02:41

present inside the state file which is

play02:43

this state file here so we will start

play02:45

with creating the configuration first it

play02:47

will create the code which is going to

play02:49

be a resource block for this particular

play02:51

instance specifying the instance type

play02:53

specifying uh the Ami used and all the

play02:56

other things once you write the code for

play02:58

this to be inside the state file we will

play03:00

run the terraform import command so if

play03:02

you look at the answer here we we first

play03:05

write the terraform configuration for

play03:07

the resources that we want to be managed

play03:08

through terraform once we write the code

play03:11

then we run the terraform import command

play03:12

and here is the command here so

play03:14

terraform import with the resource type

play03:16

and the unique ID so we run terraform

play03:18

import command for each resource

play03:20

specifying the resource type and its

play03:22

unique identifier once you follow these

play03:24

two steps then the infrastructure will

play03:27

be managed through terraform and not

play03:28

manually so we have to repeat these

play03:30

steps or repeat this process for each

play03:32

resource that we want to manage through

play03:34

terraform now if you want to learn more

play03:36

about how terraform import works here is

play03:38

a great blog by terraform itself where

play03:40

they will tell you how you can use

play03:42

terraform uh import command to manage

play03:45

infrastructure through terraform rather

play03:47

than

play03:49

manually moving on the next scenario

play03:51

based question is you have multiple

play03:52

environments Dev stage fraud for your

play03:55

application and you want to use the same

play03:57

code for all these environments how can

play03:59

you do that

play04:00

so according to the question there are

play04:01

different environments for an

play04:02

application and you want to use the same

play04:04

terraform code for all these different

play04:06

environments how can you do that to

play04:08

achieve this we have actually two

play04:09

different approaches the first is using

play04:11

terraform modules and the second is

play04:13

using terraform workspaces so what is

play04:15

terraform module a terraform model is a

play04:17

block of code or a code template for

play04:19

infrastructure components and you define

play04:21

them once and then you can use them with

play04:24

different configurations for various

play04:25

environments by passing in different

play04:27

parameters or different variables so

play04:30

this is the first approach where you can

play04:31

use the same code for different

play04:32

environments the second one is using

play04:34

terraform workspaces using workspaces

play04:36

you will have different state files for

play04:38

different environments using the same

play04:40

code so you terraform workspace provides

play04:43

a way to manage separate states for same

play04:45

set of configuration file each workspace

play04:48

maintains its own safe file allowing you

play04:50

to work on different environments

play04:52

concurrently without interfering with

play04:53

each other here is a great block that

play04:56

will help you understand how you can use

play04:58

the same code with with different

play05:00

multiple environments using uh the

play05:02

terraform module approach and also using

play05:04

the terraform workspace approach I

play05:06

recommend you checking this out if you

play05:07

want to learn more about how you can use

play05:09

the same code with different

play05:11

environments moving on the next question

play05:13

is what is terraform State file and why

play05:15

it is important so we already know State

play05:17

file is something that is going to have

play05:19

information about the resources managed

play05:21

through terraform and this is usually a

play05:24

Json file so if you look here terraform

play05:27

State file is a Json or a binary file

play05:29

that stores the current state of the

play05:31

managed infrastructure State file is

play05:33

like a blueprint that stores information

play05:35

about the infrastructure you manage and

play05:37

it is crucial because it helps teram

play05:39

understand what's already set up what

play05:41

changes need to be made by comparing the

play05:43

desired state with the current one in

play05:45

the state file terraform can make

play05:46

accurate updates to your infrastructure

play05:49

so this means terraform State file is

play05:50

very important for terraform to work

play05:52

because using State file terraform

play05:54

actually compares the configuration with

play05:56

what you have on the cloud and then it

play05:58

will actually create to delete stuff so

play06:00

terraform State file is a Json file

play06:03

which is very important for terraform to

play06:04

work so next question is a scenario

play06:08

based question a junior devops engineer

play06:10

accidentally deleted a state file what

play06:12

step should we take to resolve this so

play06:13

we already know State file is very very

play06:15

important and in this question a junior

play06:17

devops engineer has accidentally deleted

play06:19

a state file what step should we take to

play06:21

recreate it or resolve this so as we

play06:24

know state is import State file is

play06:26

important it is always recommended to

play06:27

take backups so the first step is going

play06:29

to recover backup if there is a backup

play06:32

available try to restore the state file

play06:34

from the backup if there is no backup

play06:36

then you need to manually create State

play06:38

file which is very timec consuming and

play06:40

you can do this by using terraform

play06:41

import command so you will check what

play06:44

resources are there on the cloud and

play06:45

using terraform import command you will

play06:47

import every single resource and put

play06:50

them into the state file to recreate the

play06:51

state file here is another great blog

play06:54

where the author accidentally deleted

play06:56

the state file and then using terao

play06:58

import command they recreated the state

play07:00

file so here is the terraform import

play07:03

command on how they use terraform import

play07:05

to recreate State file so make sure you

play07:07

always take backups if there are no

play07:08

backups you will have to recreate it

play07:10

manually using terraform import command

play07:12

checking each resource on the cloud once

play07:14

you do that make make sure to review and

play07:16

monitor if everything is working fine if

play07:18

you get this question in the interview

play07:19

the followup question would be what are

play07:21

some best practices to manage terraform

play07:23

State file so as we know terraform State

play07:25

file is important and I've been

play07:27

continuously saying this here are some

play07:28

best practices to make make sure that

play07:30

you properly manage your state file the

play07:32

first best practice is to use remote

play07:33

storage so rather than storing your

play07:35

state file locally in your local machine

play07:37

like this you can store your state file

play07:39

on remote backends like S3 Azure blob

play07:41

storage console and lot more so you

play07:44

should always store the state file

play07:46

remotely for safety collaboration and

play07:49

Version Control second is State locking

play07:51

when you store your state files on

play07:53

remote backends you will obviously have

play07:55

collaboration which means multiple

play07:56

people will constantly update the state

play07:58

file by running different terraform

play08:00

commands so you need to enable State

play08:02

locking to make sure there are no

play08:04

conflicts whenever multiple people are

play08:06

changing the state file uh so State

play08:09

locking will prevent conflicts in

play08:10

concurrent operations next is to access

play08:13

control make sure only authorized people

play08:16

or Services have access to the state

play08:18

file to avoid deletion or Corruption of

play08:21

the state file next is automated backups

play08:24

make sure to set up automated backups to

play08:26

prevent data loss for the state file and

play08:28

lastly

play08:30

environment separation maintain separate

play08:32

State files for each environments or

play08:34

utilize terraform workspace to manage

play08:36

multiple State file so here are some

play08:37

different best practices to manage

play08:39

terraform State five let let's move on

play08:42

the next question is your team is

play08:43

adopting a multicloud strategy and you

play08:45

need to manage resources on both AWS and

play08:48

Azure using terraform so how do you

play08:50

structure your terraform code to handle

play08:52

this so according to the question your

play08:54

team is adopting multicloud strategy

play08:55

where you want to create code on AWS as

play08:58

well as on azure so how can you do that

play09:00

so we already know terraform is a cloud

play09:02

agnostic which means terraform can work

play09:04

on different clouds at once so you can

play09:07

also create resources on AWS also on

play09:09

Azure also on

play09:11

gcp so you will do that by first

play09:13

defining the providers if you want to

play09:15

create resources in AWS you will have to

play09:17

Define AWS as a provider in this case we

play09:20

are creating resources in AWS and Azure

play09:22

so we will Define provider for AWS and

play09:24

Azure as well once you define the

play09:26

provider you will then start with

play09:28

writing the code for the resources you

play09:30

want to create in AWS and Azure so here

play09:32

are two steps first you define the

play09:34

provider then you write the code for

play09:36

different resources you want to

play09:38

create moving on the next scenario based

play09:41

question is there are some bash scripts

play09:43

that you want to run after creating your

play09:44

resources with terraform how how would

play09:47

you achieve this so you have some bat

play09:49

scripts that you want to run after the

play09:50

resources are created you can do this

play09:52

using provisioners in terraform so there

play09:54

are three different types of

play09:55

provisioners in terraform there is file

play09:58

provisioner local ex provisional and

play10:00

remote exact provisional for us to run

play10:02

bash scripts we can use local exact

play10:04

provisional and remote exec provisional

play10:06

the difference between these two are

play10:08

local exec provisioners are used to run

play10:10

commands in your local machine remote

play10:12

exec provisioners are used to run

play10:14

commands or run scripts inside your

play10:17

remote machines so here is a simple

play10:19

example we are using a provisioner which

play10:22

is a remote exec provisioner and we

play10:23

using we are running some commands here

play10:25

the first command is to give permissions

play10:27

for the script that you want to run and

play10:29

the second command is to actually run

play10:30

the script when you use remote or file

play10:32

provisional you also need to define the

play10:34

connection block which is how you will

play10:36

connect to the remote machine where you

play10:37

want to run your scripts so you can see

play10:39

the explanation here in this

play10:40

configuration we using a remote exit

play10:42

provisioner which executes A bash

play10:44

command on a remote machine via

play10:46

SSH you need to provide the necessary

play10:48

connection details such as SSH user

play10:51

private key and the host so this is how

play10:54

you can run bash scripts uh using remote

play10:57

exact provisioner if you want to run it

play10:58

on remote machine if you want to run it

play11:00

toally you can use local exact provision

play11:01

as

play11:02

well moving on the next question is your

play11:05

company is looking ways to enable High

play11:07

availability how can you perform blue

play11:09

green deployments using terraform this

play11:11

is a very nice question and I have got

play11:13

this question a lot of times when

play11:15

sitting for interviews so what is Blu

play11:17

green deployment a Blu green deployment

play11:19

is a strategy where you have two

play11:20

identical environments a blue one and a

play11:23

green one like this according to the

play11:25

question the question is asking how can

play11:27

you use terraform to set up a blue green

play11:30

environment so you can use terraform you

play11:32

can actually you can obviously use

play11:34

terraform to do this so terraform

play11:35

facilitates this by defining two sets of

play11:38

infrastructure Resources with slight

play11:40

variation so maybe you can have two

play11:42

different Autos scaling groups or two

play11:44

different Azure virtual machine scale

play11:46

sets what you can do is you can create a

play11:48

new environment alongside with the

play11:49

existing one which is going to be the

play11:51

green one and you will test if

play11:53

everything is working in the new

play11:54

environment if everything works properly

play11:56

you can then switch the traffic either

play11:58

using uh load balances or using DNS

play12:01

records so you can see you first create

play12:03

the new environment alongside with the

play12:05

existing one you validate if everything

play12:07

is working fine in the green environment

play12:09

and if it is working fine you can then

play12:10

use application Road balancer or DNS

play12:12

records to switch traffic between these

play12:14

two again to understand this more

play12:16

clearly I also have another uh blog or

play12:20

documentation

play12:21

by by terraform itself and they have

play12:23

defined how you can use terraform for

play12:25

bluprint deployment or for Canary and

play12:27

rolling deployment as well so go through

play12:29

this to understand more on how you can

play12:31

use terraform to set this up moving on

play12:34

the next question is your company wants

play12:36

to automate terraform through cicd

play12:38

pipelines how can you integrate

play12:39

terraform with cicd pipelines this is

play12:42

again very very important because every

play12:43

company is using cicd pipelines to

play12:46

automate the terraform infrastructure

play12:48

provisioning so make sure you go through

play12:50

this answer very properly I have also

play12:52

explained this very thoroughly in this

play12:54

particular video where we have used

play12:55

gitlab cicd to automate terraform

play12:58

resource creation and deletion so inside

play13:00

this video we have created a cicd

play13:02

pipeline using gitlab in this cicd

play13:04

pipeline we have different stages for in

play13:06

it for a plan apply and Destroy so

play13:10

similarly for you to set up or automate

play13:12

your terraform through cicd pipeline you

play13:14

need to First push the terraform code

play13:17

this code onto uh remote repository like

play13:20

GitHub or gitlab wherever you want to

play13:21

set up your cicd pipeline once you have

play13:23

your code there you can then start with

play13:25

writing the cicd script uh which will

play13:28

have different stages for in it for

play13:30

validate for plan for apply for apply

play13:32

you can either do it manually where you

play13:34

will create uh where you will create

play13:36

merge request every time there's a

play13:38

change in the terraform code and once it

play13:40

is approved only then resource should be

play13:42

created also you can set up approval for

play13:45

deletion as well so here are the

play13:47

different steps you first commit the

play13:49

code to Version Control System set up a

play13:51

cicd pipeline that will check every time

play13:53

you make a change inside your terraform

play13:54

code and you push it to this repository

play13:57

terraform the CD pipeline will run

play13:59

in the pipeline you can execute

play14:00

terraform commands such as init validate

play14:02

and plan to ensure the configurations

play14:04

are valid and generate an execution plan

play14:06

you can do this by defining different

play14:07

stages in the

play14:09

pipeline use Terra forly command to

play14:12

create or modify infrastructure based on

play14:14

approved changes so whenever whenever

play14:17

you make a change you can then create a

play14:18

merge request when this merge request is

play14:20

accepted only then the apply stage

play14:23

should be done optionally you can also

play14:24

use testing and verification tool to

play14:27

validate and deploy infrastructure so

play14:28

you can include different uh different

play14:31

Frameworks or different tools to include

play14:33

testing or to make sure your your

play14:35

terraform syntax is correct finally

play14:38

trigger additional pipeline stages for

play14:40

application deployment and testing and

play14:41

release optionally you can also have

play14:43

other cicd pipelines where you can use

play14:46

you can also add other cicd pipelines

play14:48

for application deployment testing and

play14:50

release so this is how you can integrate

play14:51

cicd pipelines with terraform I highly

play14:54

recommend you checking out this video

play14:56

where I've explained step by step on how

play14:57

you can use cicd with terraform to

play15:00

automate deployment on the

play15:02

cloud moving on the next question is

play15:05

describe how you can use terraform with

play15:06

infrastructure deployment tools like

play15:08

anible or Chef so in this question it is

play15:10

asking how you can use terraform with

play15:12

configuration management tools like

play15:14

Terra like anible or Chef so we all know

play15:17

terraform is an infrastructure

play15:18

provisioning tool which is used to

play15:20

create infrastructure whereas anible or

play15:22

Chef are configuration management tools

play15:24

which are used to configure stuff so

play15:26

using terraform you can create E2

play15:28

instances but using anible you can

play15:30

install something on it so terraform can

play15:32

be used in conjunction with

play15:33

infrastructure deployment tools like

play15:35

anible or Chef to manage infrastructure

play15:37

provisioning and configuration anible

play15:39

and Chef can handle tasks such as

play15:41

installing a software configuring

play15:43

servers managing Services while

play15:45

terraform focuses on infrastructure

play15:47

provisioning and orchestration so it is

play15:49

a very good practice to use these two

play15:51

tools together to create or achieve a

play15:54

comprehensive infrastructure automation

play15:56

solution now if you have confusion

play15:58

between what what is terraform and what

play15:59

is anible and how they are different you

play16:01

can check out this video where I've

play16:03

explained how terraform and anible are

play16:04

different from each other and what is

play16:07

what do they actually

play16:08

do so you can use terraform with anible

play16:12

and that we have answered here moving on

play16:14

the next question is your infrastructure

play16:17

contains database passwords and other

play16:19

sensitive information how can you manage

play16:21

secrets and sensitive data in terraform

play16:23

this is a very very important question

play16:25

compan companies are very concerned

play16:27

about how you manage Secrets when

play16:28

working with terraform so you need to

play16:30

know how to manage Secrets like database

play16:32

passwords credentials when working with

play16:34

terraform here are some best practices

play16:37

that you can follow to manage Secrets

play16:39

when working in terraform the first best

play16:41

practice is never hard Cod secrets in

play16:42

your terraform code never put your uh

play16:45

database passwords never put your

play16:47

credentials inside terraform code

play16:49

because anyone will be able to see it

play16:51

even if you push it on uh repositories

play16:53

everyone will be able to see it so never

play16:55

hardcoded in your code you can store

play16:58

Secrets outside Version Control files

play17:00

never push them on GitHub as well and

play17:02

you can rather than rather than storing

play17:05

it inside the code you can use tools

play17:06

like hashiko Vault or also Cloud

play17:09

specific secret Management Services like

play17:11

Secrets manager in AWS also opt you can

play17:14

also use terraform input variables or

play17:16

environment variables where you can pass

play17:18

this information while you run the

play17:20

commands rather than storing them in the

play17:21

code so here are few uh different best

play17:24

practices that you can use to make sure

play17:25

that you can protect sensitive

play17:27

information and minimize the risk of

play17:29

exposing Secrets

play17:31

intentionally or unintentionally so

play17:34

moving on the next question is you have

play17:36

RDS database and ec2 instance created

play17:39

using terraform ec2 instance should be

play17:41

created before RDS how can you specify

play17:44

dependencies between two resources so

play17:47

you have RDS database and you also have

play17:49

ec2 instances you want this ec2 instance

play17:51

to be created before the RDS is created

play17:54

so to do that you can use a meta

play17:55

argument which is a depends on meta

play17:57

arguments using this depends on met

play17:59

argument you can Define that the ec2

play18:00

should be created first and then the Rd

play18:02

should be created so in terraform you

play18:04

can specify dependencies between

play18:05

resources using dependon attribute

play18:08

within the resource block and I've

play18:10

explained this again in this video where

play18:12

I've shown how you can use dependon

play18:14

argument to create resources first so

play18:16

make sure to watch this video if you

play18:18

want to learn how you can use dependon

play18:19

meta argument to create resources before

play18:22

any other

play18:24

resource moving on the next question is

play18:26

you have 20 servers you have 20 servers

play18:29

created through terraform but you want

play18:31

to delete one of them is it possible to

play18:33

destroy a single resource out of

play18:34

multiple resources using terraform so

play18:37

this is again a very tricky question

play18:39

whenever you want to delete stuff

play18:40

through terraform you will run the

play18:42

terraform destroy command but when you

play18:44

run terraform destroy command everything

play18:46

that is defined inside your

play18:47

configuration will be deleted so let's

play18:49

say you have 20 servers how can you

play18:51

delete one of them so if you want to

play18:53

delete just one of the resources defined

play18:55

among the 20 resources in the terraform

play18:58

you can use terraform destroy command

play18:59

with a Target attribute here so it is

play19:02

possible we can use terraform destroy

play19:04

Target command followed by the resource

play19:06

type and the name to destroy a specific

play19:08

resource so you can use Target with the

play19:10

instance that you want to create that

play19:12

you want to delete and it will only

play19:13

delete the particular instance rather

play19:15

than deleting all the 20

play19:17

servers and again there's a

play19:19

documentation by ashiko which will tell

play19:21

you how you can use terraform destroy

play19:23

with a Target attribute here so make

play19:26

sure you go through this if you want to

play19:28

understand more on how you can destroy a

play19:29

particular resource rather than

play19:31

destroying

play19:33

everything now moving on the next

play19:35

question is what are advantages of using

play19:37

terraforms count feature over resource

play19:40

duplication so in this question they're

play19:42

asking you what is the advantage of

play19:44

using count instead of resource

play19:46

duplication so a count feature or count

play19:48

meta argument is used to define how many

play19:51

resources you want to create for example

play19:53

you can check here here is the code to

play19:55

create AWS instance in this block of

play19:58

code we are defining count as four which

play20:01

means it will create four instances of

play20:03

these configuration other way to do this

play20:05

is to rewrite the code four time with

play20:08

this I'm not writing this code four

play20:09

times so this is the benefit using count

play20:12

you are you can create as many resources

play20:15

as you want without actually writing the

play20:16

code so using using terraforms count

play20:19

feature provides advantage over resource

play20:21

duplication by allowing you to

play20:22

dynamically create multiple instances of

play20:24

a resource based on given condition or a

play20:27

variable with count you can define a

play20:29

resource block with the count value that

play20:30

evaluates an expression such as variable

play20:33

or conditional statement this reduces

play20:35

code duplication and enable more

play20:38

efficient resource management and

play20:39

scalability so it is always always

play20:41

advised to use count rather than

play20:44

defining your code blocks multiple times

play20:47

you can also add some Advanced

play20:48

Expressions to enhance your terraform

play20:49

code here is the official documentation

play20:52

by terraform on count met argument I

play20:54

also have a short video on how you can

play20:56

use count and count. index so if you

play20:58

want you can check it

play20:59

out moving on the next question is what

play21:02

is terraforms module registry and how

play21:04

can you leverage it this is again

play21:06

something that you need to know if you

play21:07

are working on terraform module registry

play21:09

is a place where you can find different

play21:11

modules that you can use rather than

play21:12

writing the code yourself so here is the

play21:14

module registry where are different

play21:16

where you have different modules there

play21:17

is a module for IM IM there is a module

play21:19

for VPC for S3 bucket for eks everything

play21:21

so let's say you want to create eks

play21:23

cluster rather than writing the code

play21:25

yourself you can just go ahead and use

play21:27

this module if you want to check the

play21:29

code for the module you can also check

play21:30

the code here this module is created by

play21:32

Anton uh and then the code is present

play21:35

here on the GitHub so module registry is

play21:37

like a place where you can find all the

play21:38

different modules that you can reuse

play21:40

rather than writing the code yourself so

play21:43

terraforms module registry is a central

play21:45

repository for sharing and discovering

play21:47

terraform modules the module registry

play21:49

allow users to publish their modules

play21:51

which are reusable and sharable

play21:52

components of terraform configuration by

play21:55

leveraging the module register you can

play21:56

easily discover existing modules that

play21:58

address your your infrastructure need

play22:00

reducing the duplication of f word and

play22:02

you can reference modules in your

play22:03

terraform code using their registry URL

play22:07

andion for you to use any particular

play22:09

module you can see a block here inside

play22:11

the module block you can define a source

play22:12

as the module that you want to

play22:14

use now moving on the next question is

play22:17

how can you implement automated testing

play22:19

for terraform code this is again very

play22:21

important security is something that you

play22:23

need to put everywhere even in your

play22:25

terraform code so how can you implement

play22:27

automated testing by default terraform

play22:29

provides you with terraform validate and

play22:31

terraform format command which will

play22:33

check the syntax and also format the

play22:35

code according to terraform best

play22:37

practice but along with this you can

play22:39

also use other code other tools like

play22:41

terat test uh TF lint there's also

play22:44

kitchen terraform and a lot more I have

play22:46

defined all the tools here so for you to

play22:49

a for you to enable or Implement

play22:51

automated testing uh you can use all

play22:53

these different tools

play22:55

so why do we use automated testing we

play22:57

use automated t tting to validate if the

play23:00

syntax is correct or if there are any

play23:02

infrastructure changes to validate if

play23:04

the syntax is correct to detect issues

play23:06

earlier and also to ensure the desired

play23:08

State matches the actual State uh this

play23:10

involves creating text fixtures test

play23:13

fixtures defining test scenarios

play23:15

executing terraform operations and all

play23:16

this so different tools that are used

play23:19

for unit testing you can use terraform

play23:21

Terra compliance or Terra test for

play23:23

integration testing you can use Terra

play23:24

Test Kitchen terraform for linting you

play23:26

can use TF lint or check off and then

play23:29

other tools as well for static analysis

play23:32

for mocking for everything else here is

play23:34

another great block on medium uh for

play23:37

testing on terraform so you can go and

play23:40

check it out if you want to know how to

play23:42

enable automated

play23:44

testing moving on the last question we

play23:47

have is you are tasked with migrating

play23:49

your existing infrastructure from

play23:50

terraform version 1.7 to version 1.8

play23:53

which is the latest version as of now so

play23:55

what kind of considerations would you

play23:57

take so whenever you are tasked with

play24:00

migration how would you do the migration

play24:02

from a particular version to another

play24:03

version so whenever upgrading a

play24:05

terraform version from a particular

play24:07

version to another version always the

play24:09

first step is to always review the

play24:11

upgrade guide whenever terraform

play24:12

releases a new version there is always

play24:14

going to be a documentation similar to

play24:16

this so you can see you can find how you

play24:19

can upgrade a terraform to a particular

play24:20

version which is version 1.8 the latest

play24:23

one as of now so first step is to review

play24:26

upgrade guide to understand what changes

play24:28

are there what what has been deprecated

play24:30

and then new features once you do that

play24:32

you will then update your configuration

play24:34

files according to the changes of the

play24:36

new syntax and to handle deprecated

play24:38

features as well ensure thorough testing

play24:41

monitor after you make the change

play24:43

monitor if everything is working fine

play24:45

always check on the nonpr environments

play24:47

before you shift to the prod environment

play24:49

obviously and then document any changes

play24:51

and also provide training to your Terra

play24:53

to your team members this this is how

play24:55

you should be upgrading a terraform

play24:56

version from one version to another so

play24:58

now in this video we have now covered a

play25:00

very important terraform interview

play25:02

questions I hope this video was

play25:03

informative if you have any questions

play25:05

any doubt do let me know in the comment

play25:07

section also comment down if you want me

play25:09

to share this document with you and also

play25:11

comment down what should we cover next

play25:14

should we

play25:15

cover Docker interview questions or

play25:17

kubernetes interview question let me

play25:19

know in the com let me know in the

play25:21

comment section thank you and have a

play25:22

good day

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
TerraformInterview QuestionsInfrastructureCloud ManagementDevOpsCICD IntegrationState FileMulticloud StrategyTerraform ModulesSecrets Management
Benötigen Sie eine Zusammenfassung auf Englisch?