Prerequisites #
Terraform Installation Linux (Deb) #
# Install the HashiCorp GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
# Verify the GPG key fingerprint
gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
# Add the official HashiCorp repository
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
# Install Terraform
sudo apt update && sudo apt-get install terraform
# Verify installation / check version
terraform version
Terraform Installation Windows #
Download Terraform: https://developer.hashicorp.com/terraform/downloads
- Extract the “terraform.exe” file and move it to a folder like
C:\Terraform
Create an Environment Path Variable that points to the directory of your terraform.exe file. Open Advanced system settings

Edit Path Variables
Create new Path Variable that points to the terraform.exe location
Terraform Commands #
Main Commands #
Initialize Terraform Project #
- This will download and install the AWS Terraform provider, defined in the configuration
# Initialize the Terraform project
terraform init
Validate Configuration Files #
# Validates the syntax and structure of Terraform configuration files
terraform validate
# Shell output:
Success! The configuration is valid.
Plan the Deployment #
# Dry run / preview changes before applying them
terraform plan
Apply the Configuration #
# Create stack: Without confirmation
terraform apply -auto-approve
Delete Resources #
# Delete stack: Without confirmation
terraform destroy -auto-approve
Outputs #
# Display the values of output variables defined in the configuration
terraform output
Console #
Open / Close Console #
# Open Terraform Console
terraform console
# Exit Console
exit
List Resource Details #
# List resource details: Example public IP
aws_instance.vm[*].public_ip
Terraform Deployment State Management #
List Tracked Resources #
# Lists all resources tracked in the Terraform state file
terraform state list
# Example output:
aws_instance.vm[0]
aws_instance.vm[1]
Detailed Resource Information #
# Display detailed information about a specific resource from the state file
terraform state show aws_instance.vm[0]
# Shell output:
resource "aws_instance" "vm" {
ami = "ami-0e2c8caa4b6378d8c"
arn = "arn:aws:ec2:us-east-1:012345678912:instance/i-0c82d4840548182f3"
associate_public_ip_address = true
availability_zone = "us-east-1a"
cpu_core_count = 1
cpu_threads_per_core = 2
...
Refresh State File #
- Useful if resources are modified outside Terraform (manually or by another tool) and you need the state file to stay in sync
# Update the Terraform state file
terraform refresh
Remove State #
Remove the state of a resource, in case the resource was manually deleted and removed from the configuration.
# Remove a specific resource
terraform state rm aws_instance.vm[0]