Hey,
My name is Sarvar, and I am working as Senior Developer at Luxoft India. With years of experience working on cutting-edge technologies, I have honed my expertise in Cloud Operations (Azure and AWS), Big Data, and DevOps. Throughout my career, I’ve worked with clients from all around the world, delivering excellent results, and going above and beyond expectations. I am passionate about learning the latest and treading technologies.
This post will explain how to install Terraform on your local system and will also go over some of Terraform’s fundamental commands and features. If you haven’t already, please read part 1 of my article where I explain what terraforming is and how it may be useful in your everyday life. Feel free to skip it and begin here if you are already familiar with it.
Link: Terraform Beginner to Advance: Using AWS Cloud (Part-1)
Let’s Install Terraform on your local Machine:
Terraform installation requires only a few easy Linux commands, and then your local machine will be ready to use. Please install Terraform by following the steps below.
1. Update Package Repositories:
sudo apt update -y
2. Install Terraform:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update -y
sudo apt install terraform -y
If you want to install it for other OS please follow this link: Click Here
You have successfully installed Terraform as seen here. We will now take a closer look at the Terraform folder, where we will notice some of the most significant files and directories. let’s see, then.
terraform init Command:
When working on a new or existing Terraform configuration, the Terraform init command is often the first command you perform. It is a vital step in using Terraform. Its main objective is to set up several necessary components in your working directory in order to become ready for Terraform operations. The terraform init command does the following and accomplishes these goals:
Installing Plugins:
Terraform employs provider plugins to communicate with different infrastructure systems and cloud service providers such as AWS, Azure, GCP, etc. Terraform examines your configuration files for provider requirements when you run terraform init, and it then installs the required plugins into the .terraform/plugins directory in your working directory.
Module Retrieval:
Terraform init fetches any external Terraform modules that are used by your setup and places them in the .terraform/modules directory. Infrastructure configurations can be managed and shared more easily by using modules to encapsulate and reuse configurations.
Backend Configuration:
Backend configuration is another thing the Terraform init tool checks for. Terraform’s state files are stored in the backend, which can be local, remote (such as in an S3 bucket), or utilize another supported storage method. Initializing the backend guarantees adequate infrastructure state management.
Locking Dependencies:
Terraform init makes ensuring that the required provider versions are compatible with what is indicated in the lock file if a lock file .lock.hcl or .lock.json is present. This promotes stability and prevents unforeseen provider updates.
Initializing State:
Terraform init creates an initial state file to keep track of the state of the present infrastructure when you start a new Terraform setup or project. The backend state setting is checked and updated if you’re working on an existing project.
.terraform Folder:
When you run terraform init in your project directory, Terraform creates a directory called “.terraform.” Various files pertaining to Terraform’s internal operations and dealings with third parties are kept in this area. Its goal is to keep information current and to specify what Terraform requires to efficiently manage your infrastructure as code.
The typical contents of the .terraform folder are as follows:
plugins Directory:
This directory contains the provider plugins required for communicating with the infrastructure system or cloud provider of your choice. A subdirectory of this directory will house the plugin for each provider you use with Terraform.
modules Directory:
If your project makes use of Terraform modules, this directory houses the cached data for those modules. This facilitates quicker retrieval and reuse of modules.
providers File:
The information about the installed provider plugins, including their versions, is stored in this JSON file. Terraform uses it to control and monitor the initialized provider plugins.
terraform.tfstate & terraform.tfstate.backup:
The current status of your infrastructure is stored in the files terraform .tfstate and terraform.tfstate.backup. They keep track of the resources Terraform has added, changed, or removed to fit the infrastructure configuration you want. Particularly in a collaborative or multi-environment situation, it is imperative to manage and store these files safely.
Lock Files:
Depending on the version of Terraform you are using, lock files such as.lock.hcl & or.lock.json may also be present. These files are used to restrict provider dependencies’ version ranges and stop inadvertent provider version upgrades.
terraform Variables:
The .terraform folder is often created in the same directory as the .tf and .tfvars files that make up your Terraform configuration. As Terraform controls this folder internally, it’s crucial to avoid manually altering its contents. It is advised to utilize Terraform commands like Terraform destroy or to work with the state management tools offered by Terraform if you need to reset or reconstruct the state.
Always make a backup of your .terraform folder and terraform .tfstate files because they are essential for keeping track of and understanding the state of your infrastructure.
Conclusion: here we have seen how to install terraform on your local machine and it requires only 3–4 commands along with this we have seen in detailed information about the terraform init command and .terraform folder. we are keep exploring the terraform in upcoming articles.
What we will see in the upcoming Article?
In upcoming article we are looking at the some of the advance topics and some of the most useful commands we are using on daily bases. so stay tuned the series of Terraform Beginner to Advance: Using AWS Cloud.
Link: Terraform Beginner to Advance: Using AWS Cloud (Part-3)
— — — — — — — —
Here is the End!
Thank you for taking the time to read my article. I hope you found this article informative and helpful. As I continue to explore the latest developments in technology, I look forward to sharing my insights with you. Stay tuned for more articles like this one that break down complex concepts and make them easier to understand.
Remember, learning is a lifelong journey, and it’s important to keep up with the latest trends and developments to stay ahead of the curve. Thank you again for reading, and I hope to see you in the next article!
Happy Learning!