what is terraform?
Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.
How to Install Terraform on Linux, macOS, and Windows
Below are the step by step procedures on how to install Terraform on Linux, macOS, and Windows.
- Installing Terraform on Linux
Using apt
(in case of Debian/Ubuntu-based installations)
- Update the package index
sudo apt update
- Install the required software
sudo apt install -y gnupg software-properties-common curl
- Add the ‘hashicorp’ GPG key -
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
- Now add the Hashi Corp chronological deposit
sudo apt-add-repository "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
- Update the package index once again
sudo apt update
- Install terraform
sudo apt install terraform
- And once you've successfully done this, you can verify the installation with the command below:
terraform version
Using yum
(for RHEL/CentOS/Fedora systems)
Install dependencies using:
sudo yum install -y yum-utils
Add the Hashi Corp repository:
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Next, install terraform:
sudo yum install terraform
And finally, verify the installation with:
terraform version
- Installing Terraform on macOS
Using Homebrew (recommended):
First, you need to install Homebrew if it’s not installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
And then install Terraform using Homebrew:
brew install terraform
- Check the installation:
terraform version
Manual Installation:
Download the latest Terraform package:
Visit the Terraform download page and choose the macOS version.
Alternatively, you can use
curl
:curl -LO https://releases.hashicorp.com/terraform/<version>/terraform_<version>_darwin_amd64.zip
Replace <version>
with the version you want to install. Criteria for Terraform package unzipp**ing**:
unzip terraform_<version>_darwin_amd64.zip
- The Terraform binary should be moved to /usr/local/bin:
sudo mv terraform /usr/local/bin/
- To check the installation status:
terraform --version
3. Installing Terraform on Windows
Install with Chocolatey: Exegesis:
- Obtain Chocolatey (in case of lack thereof): -a. Launch PowerShell in Administrator mode and execute the subsequent command:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
- Terraform installation by way of Chocolatey:
choco install terraform
- To verify the installation is working:
terraform version
Manual Installation:
- Get the terraform binary:
- Visit the page and get the windows version of Terraform
- Make sure the ZIP file that was downloaded is extracted:
Right-click on the zip file and click on Extract All.
Save the extracted file to any folder that one desires.
- Integrate Terraform in the path of System path:
One needs to right-click on This pc (macOS-computer alternatively) and hit properties.
Proceed by clicking on Advanced system settings and navigate to the System Properties window thereafter.
Under System Variables, click on Environment Variables and locate the variable labeled Path.
Click Edit, then New and specify the directory that the
terraform.exe
file was unpacked into.
Confirm the installation:
Launch a new instance of the command prompt by pressing the
cmd
key on the keyboard, then running the command:terraform version
Summary:
Linux: Either use a package manager like
apt
oryum
(depending on your distro) or alternatively download and install terraform manually.macOS: Either install via Homebrew, or manually download the binary.
Windows: The preferred method is to use Chocolatey, but it is also possible to download terraform and install it manually.
After each and every step taken, the last action which is terraform version
is run to validate the installation and confirm that it has been done correctly and most importantly, has been adequately set to start functioning.
How to setup terraform in VSCode?
step1:Open VSCode
step2:Click on extension icon and search for terraform
step3:Find official HashiCorp terraform as shown in picture and install it
step4:Now you can start using terraform
How to make a your first terraform file?
Terraform file has own extension which is .tf and terraform has its own code language which is HASHICORP language.
step1:Open a new folder using ctrl+k+o
step2:Open or create folder with any name for E.g: terraform
step3:Open it in VSCode
step4:Now create a file by clicking on new file
step5: Name the file with extension .tf
for example: main.tf
step6:Click enter and now you can start writing code
How to write a terraform code?
to write code you need we to go through the official documentation
Pattern of terraform code:
<block> <parameters> {
arguments
}
Example code:
How to execute a terraform file?
To execute terraform file there are total three commands:
terraform init
: Initializes the working directory, downloads necessary provider plugins, and prepares your environment for Terraform operations.terraform plan
: Previews the changes Terraform will make to your infrastructure, showing resources to be created, updated, or deleted. It does not apply any changes.terraform apply
: Applies the changes defined in the plan to your infrastructure, making the actual modifications (create, update, or delete resources).
for further you can refer to documentation