Skip to main content

Terraform Commands Overview

490 words·
Terraform Commands
Table of Contents

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]



Taint / Recreate Terraform Resource
#

# List tracked resources
terraform state list

# Shell output:
hcloud_firewall.firewall1
hcloud_network.vpc1
hcloud_network.vpc2
hcloud_network_route.vpc1_route1
hcloud_network_route.vpc2_route1
hcloud_network_subnet.vpc1_subnet1
hcloud_network_subnet.vpc2_subnet1
hcloud_server.gateway
hcloud_server.vm1
hcloud_server.vm2
# Taint a Terraform resource
terraform taint hcloud_server.vm1

# Shell output:
Resource instance hcloud_server.vm1 has been marked as tainted.
# Recreate Terraform resource
terraform apply -auto-approve

# Shell output:
Plan: 1 to add, 0 to change, 1 to destroy.
hcloud_server.vm1: Destroying... [id=65035894]
hcloud_server.vm1: Destruction complete after 33s
hcloud_server.vm1: Creating...
hcloud_server.vm1: Still creating... [00m20s elapsed]
hcloud_server.vm1: Creation complete after 21s [id=65037264]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.