Terraform
Q: What is Terraform?
Ans:
Terraform is an infrastructure as code (IaC) tool that lets you build, edit, and version infrastructure in a secure and efficient manner.
This covers both low-level and high-level components, such as compute instances, memory, and networking, as well as DNS records, SaaS services, and so on.
Terraform is capable of managing both third-party services and unique in-house solutions.
Terraform uses configuration files to tell it which components are needed to run a single application or a whole datacenter. Amazon Web Services,
IBM Cloud, Google Cloud, Platform, DigitalOcean, Linode, Microsoft Azure, Oracle Cloud Infrastructure, OVH, VMware, vSphere, OpenNebula, and OpenStack all use Terraform.
Q: What are the key features of Terraform?
Ans:
- Following are the key features of Terraform:
- Infrastructure as Code: Terraform's high-level configuration language is used to describe your infrastructure in declarative configuration files that are human-readable. You may now generate a blueprint that you can edit, share, and reuse.
- Execution Strategies: Before making any infrastructure modifications, Terraform develops an execution plan that describes what it will do and asks for your agreement. Before Terraform produces, upgrades, or destroys infrastructure, you can evaluate the changes.
- Graph of Resources: Terraform develops or alters non-dependent resources while simultaneously building a resource graph. This allows Terraform to construct resources as quickly as possible while also providing you with more information about your infrastructure.
- Automation of Change: Terraform can automate the application of complex changesets to your infrastructure with little to no human intervention. Terraform identifies what happened when you update configuration files and provides incremental execution plans that take dependencies into account.
Q:What do you mean by terraform init in the context of Terraform?
Ans:
The terraform init command creates a working directory in which Terraform configuration files can be found. After creating a new Terraform configuration or cloning an old one from version control, run this command first. It is safe to use this command more than once. Despite the fact that successive runs may result in errors, this command will never overwrite your current settings or state.
Syntax:
terraform init [options]
The following options can be used in conjunction with the init command :
-input=true: This option is set to true if the user input is mandatory. If no user input is provided, an error will be thrown.
-lock=false: This option is used to disable the locking of state files during state-related actions.
-lock-timeout=<duration>: This option is used to override the time it takes Terraform to get a state lock. If the lock is already held by another process, the default is 0s (zero seconds), which results in an immediate failure.
-no-color: This option disables the color codes in the command output.
-upgrade: This option can be chosen to upgrade modules and plugins throughout the installation process.
Q: What are the different terraform Commands?
Ans:
- terraform init: Initializes remote backends; downloads providers and remote modules defined in your configuration.
- terraform init -upgrade: used to upgrade the existing downloaded providers.
- terraform plan: generates the execution plan for the infrastructure creation or updation.
- terraform apply: creates or updates the infrastructure after requesting confirmation from user.
- terraform apply –auto-approve: creates or updates the infrastructure; user approval stage is skipped.
- terraform destroy: deletes the infrastructure after requesting confirmation from user.
- terraform destroy –auto-approve: deletes the infrastructure; user approval stage is skipped.
- terraform fmt: scans the current directory for configuration files and formats them according to the HCP canonical style and format.
- terraform fmt –recursive: scans the current directory as well as the sub directories for configuration files and formats them according to the HCP canonical style and format.
- terraform show: provides a human-readable output from a state or plan file.
Q: How can you use the same provider in Terraform with different configurations?
Q: You have a Terraform configuration file with no resources. What happens when you run the terraform apply command?
Q: What happens if a resource was created successfully in terraform but failed during provisioning?
Q: How can you import existing resources under Terraform Management?
Q: Which command can be used to preview the terraform execution plan?
Q: Which command can be used to reconcile the Terraform state with the actual real-world infrastructure?
Q: Which command can be used to switch between workspaces when using Terraform Cloud?
Q: Which command is used to perform syntax validation on terraform configuration files?
Q: Which command is used to create new workspaces in the Terraform cloud?
Q: Write a terraform script to launch an EC2 instance in AWS?
Q: How import command is used in Terraform?
Use Case | Explanation |
---|---|
Import old resources | Organizations can import resources created with alternative tools or methods |
Import resources created outside Terraform. | When Terraform was deployed, it may not have been universally adopted. As such, there may have been infrastructure additions/amendments made outside of Terraform |
Loss of Terraform state file. | The state file can be deleted or become irreversibly corrupt |
Re-factoring / Amending Terraform code structure | As an environment scales, there may be a need to re-factor or re-structure Terraform modules and other constructs |
Q: what is module in Terraform ?
- source: The source location of the module. This can be a local path or a URL.
- name: The name of the module. This is used to reference the module in other parts of the configuration.
- version: The version of the module to use. This is optional and can be used to specify a specific version of the module.
Q: what is state file in Terraform?
Q: What is terraform provisioner?
- Running custom shell script on the local machine
- Running custom shell script on the remote machine
- Copy file to the remote machine
- Generic Provisioners (file, local-exec, and remote-exec)
- Vendor Provisioners (chef, habitat, puppet, salt-masterless)
Comments
Post a Comment