This content originally appeared on DEV Community and was authored by Daberechi
Introduction to terraform
Terraform is an open source Iac tool, developed by Hashicorp. It allows you to define, manage and provision infrastructure in a safe, predictable, and automated way using a declarative configuration language.
Terraform managed both cloud infrastructure (AWS, Azure, GCP) and on-prem resources (VMware, Kubernetes, and more). With terraform, you can:
- Provision infrastructure: create servers, databases, networks and more
- Update infrastructure: modify existing resources without downtime
Destroy infrastructure: remove resources when there are no longer needed
Terraform Providers
Providers serves as a middleman between terraform and where you are deploying resources to. For example, provider “AWS”Hashicorp Configuration Language
HCl is the language terraform uses to create infrastructure resources and configurations.Resources
These are the infrastructure resources you create with terraform. Example, Resources “aws_ec2_instance”Terraform State
This is like a memory or storage that stores what have been created or modify. It also store the information of the infrastructure resources created or modify.Resources Block
It is a block that defines a specific resource you want to create or manage.Resource Type
It is the specific type of resource you want to create e.g EC2 instance, Google compute engine, VPC e.t.cResource Name
It is a unique identifier for the resources within your terraform configurationAttributes/Machine Type/Region
It is a property or configuration of the resources often required to create or manage it.Variables
It is like a container that holds information. It’s also inputs defined to make the configuration dynamic and reusable.Terraform Provisioners
These are used to execute scripts or commands on a resource after it is created or updated. This allows you to perform additional configuration or setup tasks on the resource, such as installing software, modifying files, or running shell commands.Types of Peovisioners
Remote Executioner or Exec: it execute commands on the remote machine using SSH or WinRM.
Local Exec: it execute commands on the machine where terraform is running
File provisioner: it upload files or directories from the machine running terraform to the remote resources
Installing Terraform
Here are the steps to install Terraform on Windows, macOS, and Linux:
Windows
- Download the Terraform binary: Go to the Terraform downloads page and download the Windows binary (zip file).
-
Extract the binary: Extract the contents of the zip file to a directory, such as
C:\Terraform
. -
Add Terraform to the system PATH:
- Right-click on “This PC” or “Computer” and select “Properties.”
- Click on “Advanced system settings” on the left side.
- Click on “Environment Variables.”
- Under “System Variables,” scroll down and find the “Path” variable, then click “Edit.”
- Click “New” and add the path to the Terraform binary (e.g.,
C:\Terraform
).
-
Verify the installation: Open a new Command Prompt or PowerShell window and run
terraform -v
to verify that Terraform is installed correctly.
macOS (via Homebrew)
- Install Homebrew: If you haven’t already, install Homebrew by following the instructions on the Homebrew website.
-
Install Terraform: Run
brew tap hashicorp/tap
and thenbrew install hashicorp/tap/terraform
. -
Verify the installation: Run
terraform -v
to verify that Terraform is installed correctly.
Linux
- Download the Terraform binary: Go to the Terraform downloads page and download the Linux binary (zip file).
-
Extract the binary: Extract the contents of the zip file to a directory, such as
/usr/local/bin
. -
Make the binary executable: Run
chmod +x /usr/local/bin/terraform
to make the binary executable. -
Verify the installation: Run
terraform -v
to verify that Terraform is installed correctly.
Alternatively, you can install Terraform on Linux using a package manager like apt (Ubuntu/Debian) or yum (RHEL/CentOS):
Ubuntu/Debian:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
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
sudo apt-get install terraform
RHEL/CentOS:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
After installation, verify that Terraform is working correctly by running terraform -v
.
You can install Terraform on Windows using Chocolatey. Here’s how:
- Install Chocolatey: If you haven’t already, install Chocolatey by following the instructions on the Chocolatey website.
Install Terraform: Run the following command in an elevated Command Prompt or PowerShell:
choco install terraformVerify the installation: Run
terraform -v
to verify that Terraform is installed correctly.
That’s it! Chocolatey will handle the installation and configuration for you.
If you want to install a specific version of Terraform, you can use the --version
option:
choco install terraform –version=1.5.2
Replace 1.5.2
with the desired version.
Using Chocolatey makes it easy to manage and update Terraform on your Windows system.
This content originally appeared on DEV Community and was authored by Daberechi