Terraform Interview Questions and Answers for freshers & Experienced.
What is Terraform?
Terraform is an open-source infrastructure as a code software tool created by HashiCorp, itโs a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.
What are the key features of Terraform?
- Infrastructure as Code: This feature used to infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.
- Execution Plans: High-level configuration syntax is used to describe the Infrastructure. This shows the blueprint of the datacenter that needs to be versioned. In addition, infrastructure can be re-used and shared.
- Graphing: Terraform build infrastructure efficiently by creating the graph of all the resources and identify the non-dependent resources to modify them.
- Change Automation: Complex changesets can be applied to your infrastructure with minimal human interaction. With the previously mentioned execution plan and resource graph
Can you explain Terraform AWS and Its features?
Terraform is an โinfrastructure as codeโ tool. It is like AWS CloudFormation which allows you to create, update, and version your Amazon Web Services Infrastructure. It is a tool that allows you to build, change, and version the infrastructure safely and efficiently. It is used to manage existing and popular service providers. It can also manage custom in-house solutions
Some of the major features of Terraform are:
- Friendly custom syntax, but also has support for JSON.
- Visibility into changes before they actually happen.
- Built-in graphing feature to visualize the infrastructure.
- Understands resource relationships. One example is failures are isolated to dependent resources while non-dependent resources still get created, updated, or destroyed.
- Open source project with a community of thousands of contributors who add features and updates.
- It has the ability to break down the configuration into smaller chunks for better organization, re-use, and maintainability.
What is use of Terraform CLI?
CLI in terraform is a well-mannered command line application. This application then takes a subcommand such as โapplyโ or โplanโ. The complete list of subcommands is in the navigation to the left. To get help for any particular command, pass the -h flag to the relevant sub-command.
List out the Some of Terraform commands?
Common commands:
- apply: Builds or changes infrastructure
- console: Interactive console for Terraform interpolations
- destroy: Destroy Terraform-managed infrastructure
- env: Workspace management
- fmt: Rewrites config files to canonical format
- get: Download and install modules for the configuration
- graph: Create a visual graph of Terraform resources
- import: Import existing infrastructure into Terraform
- init: Initialize a Terraform working directory
- output: Read an output from a state file
- plan: Generate and show an execution plan
- providers: Prints a tree of the providers used in the configuration
- refresh: Update local state file against real resources
- show: Inspect Terraform state or plan
- taint: Manually mark a resource for recreation
- untaint: Manually unmark a resource as tainted
- validate: Validates the Terraform files
- version: Prints the Terraform version
- workspace: Workspace management
Can you define Modules in Terraform?
A module in Terraform is a jug for numerous resources that are used jointly. Modules are collections of .tf files containing resources, input variables, and outputs, which exist outside the root folder of your configuration. The root module is required for every Terraform that includes resources mentioned in the .tf files.
Explain Terraform cloud and Its Features?
Terraform Cloud is an application that helps teams use Terraform together. It manages Terraform runs in a consistent and reliable environment, and includes easy access to shared state and secret data, access controls for approving changes to infrastructure, a private registry for sharing Terraform modules, detailed policy controls for governing the contents of Terraform configurations, and more.
Features of the Terraform cloud are:
- Audit logging
- SAML single sign-on
Can you define Terraform provider and List Terraform providers?
Terraform is used to manage and inform infrastructure resources such as bodily machines, VMs, network switches, containers, and more. A provider is responsible for understanding API interactions and exposing resources.
Some Terraform providers are:
- IaaS services: Examples are Alibaba Cloud, AWS, GCP, Microsoft Azure, OpenStack.
- PaaS services: Examples are Heroku.
- SaaS services: Examples are Terraform Cloud, Cloudflare, DNSimple.
Can you explain Terraform state?
Terraform state is a command that is used for advanced state management. The state is a necessary requirement for Terraform to function. Terraform must store state about your managed infrastructure and configuration. This state is used by Terraform to map real-world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures. This state is stored by default in a local file named โterraform.tfstateโ, but it can also be stored remotely, which works better in a team environment.
//syntax
terraform state [options] [args]
Can you explain Terraform init?
The Terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.
//syntax
terraform init [options] [DIR]
Can you explain Terragrunt?
Terragrunt is a thin wrapper that provides extra tools for keeping your configurations DRY, working with multiple Terraform modules, and managing remote state. You can also execute Terraform commands on multiple modules at once using Terragrunt.
How do you Identify the installed version of Terraform?
To check the Current Installed version of Terraform, Use the below command
//syntax
terraform [-version] [-help] [args]
Explain the use of Terraform fmt command?
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.
//syntax
terraform fmt [options] [DIR]
If the directory argument is not passed, then the command scans the current directory for the configuration files.
The list of flags available to this command is -list, -write, -diff, -check, -recursive.
Why provisioners are used in Terraform?
Provisioners can be used to model specific actions on the local machine or on a remote machine in order to prepare servers or other infrastructure objects for service.
Explain Some of the Provisioners available in Terraform?
Generic Provisioners
- File Provisioner: The file provisioner is used to copy files or directories from the machine executing Terraform to the newly created resource. The file provisioner supports both ssh and winrm type connections.
- local-exec Provisioner: The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource. See the remote-exec provisioner to run commands on the resource.
- remote-exec Provisioner: The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to run a configuration management tool, bootstrap into a cluster, etc. To invoke a local process, see the local-exec provisioner instead. The remote-exec provisioner supports both ssh and winrm type connections.
Vendor Provisioners
- Chef Provisioner: The chef provisioner installs, configures and runs the Chef Client on a remote resource. The chef provisioner supports both ssh and winrm type connections.
- Habitat Provisioner: The habitat provisioner installs the Habitat supervisor and loads configured services. This provisioner only supports Linux targets using the ssh connection type at this time.
- Puppet Provisioner: The puppet provisioner installs, configures and runs the Puppet agent on a remote resource. The puppet provisioner supports both ssh and winrm type connections.
- Salt Masterless Provisioner: The salt-masterless Terraform provisioner provisions machines built by Terraform using Salt states, without connecting to a Salt master. The salt-masterless provisioner supports ssh connections.
Explain Terraform Backend and its benefits?
A backend in terraforming is used to determine how the state is loaded and how an operation is executed. By default, terraform uses local backend. An important thing to know is that Backend in Terraform is optional.
Terraform can be used without learning or using the Backend, But Backend has some benefits such as using it to store sensitive information off the disk, executing remote operations, and storing the state remotely and preventing it from corruption.
Here are some of the benefits of backends:
- Working in a team: Backends can store their state remotely and protect that state with locks to prevent corruption. Some backends such as Terraform Cloud even automatically store a history of all state revisions.
- Keeping sensitive information off disk: State is retrieved from backends on demand and only stored in memory. If youโre using a backend such as Amazon S3, the only location the state ever is persisted is in S3.
- Remote operations: For larger infrastructures or certain changes, terraform apply can take a long, long time. Some backends support remote operations which enable the operation to execute remotely.
Can you explain Null Resource in Terraform?
The null_resource resource implements the standard resource lifecycle but takes no further action.
The triggers argument allows specifying an arbitrary set of values that, when changed, will cause the resource to be replaced.
Explain Resource Graph in Terraform?
Terraform builds a graph of all the resources, and parallelizes the creation and modification of any non-dependent resources. Because of this, Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure.
Define Terraform Plugins?
Terraform Plugins are executable binaries written in Go programming language. Plugins are basically the providers and provisioners in Terraform configurations. Terraform has various in-built provisioned plugins, and users have to discover provider plugins dynamically according to their requirements. The Terraform plugins help in domain-specific implementation of the service they represent.