
Terraform Tactics: A Guide to Mastering Terraform Commands for DevOps
Dive into this guide to mastering essential Terraform commands.
- Scalability: Terraform facilitates the management of complex infrastructure setups through code, enabling scalability and efficient resource provisioning.
- Consistency: Infrastructure configurations defined in Terraform ensure consistency across environments, reducing human error and enhancing reliability.
- Collaboration: Teams can collaborate effectively by version-controlling Terraform configurations, enabling seamless infrastructure updates and tracking changes.
- Flexibility: Terraform supports various cloud providers and services, allowing DevOps teams to work with diverse infrastructures using a unified tool.
- Cost-Efficiency: By adopting Terraform, organizations can optimize resource usage, monitor costs, and automate resource lifecycle management.
terraform version
Terraform v1.9.5
terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 3.47.0...
- Downloading plugin for provider "null" (hashicorp/null) 3.1.0...
- Downloading plugin for provider "template" (hashicorp/template) 2.2.0...
Terraform has been successfully initialized!
terraform init -migrate-state
Migrating state...
Migration successful! State files have been moved to the new backend.
terraform init -upgrade
Upgrading Terraform modules and plugins...
Upgrade successful! Modules and plugins are now up to date.
terraform init -backend-config=backend.tf
Initializing Terraform with backend configuration from backend.tf...
Initializing the backend...
- Using backend configuration from backend.tf
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 3.47.0...
- Downloading plugin for provider "null" (hashicorp/null) 3.1.0...
Terraform has been successfully initialized with the specified backend configuration.
terraform init -reconfigure
Reconfiguring backend...
Backend reconfiguration successful! Ready for deployment.
terraform workspace new staging
Created and switched to workspace "staging".
terraform workspace list
default
staging
production
terraform workspace select production
Switched to workspace "production".
terraform workspace show
Current workspace: production
terraform workspace delete staging
Deleted workspace "staging" and switched to "default" workspace.
terraform plan
Refreshing Terraform state...
...
Plan: 3 to add, 0 to change, 0 to destroy.
terraform plan -var-file="prod.tfvars"
Refreshing Terraform state...
...
Plan: 15 to add, 3 to change, 5 to destroy.
terraform plan -target="aws_instance.my_ec2"
Refreshing Terraform state...
...
Plan: 4 to add, 0 to change, 0 to destroy.
terraform plan -target="aws_instance.my_ec2" -var-file="prod.tfvars"
Refreshing Terraform state...
...
Plan: 4 to add, 0 to change, 0 to destroy.
terraform plan -out=tfplan
Saving a plan to tfplan
terraform apply
Refreshing Terraform state...
...
Plan: 10 to add, 2 to change, 0 to destroy.
terraform apply tfplan
Refreshing Terraform state...
...
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
terraform apply -var-file="prod.tfvars"
Refreshing Terraform state...
...
Apply complete! Resources: 15 added, 3 changed, 5 destroyed.
terraform apply -target="aws_instance.my_ec2"
Refreshing Terraform state...
...
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Refreshing Terraform state...
...
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
terraform destroy
...
Destroy complete! Resources: 3 destroyed.
terraform destroy -target="aws_instance.my_ec2"
...
Destroy complete! Resources: 1 destroyed.
terraform destroy -target="aws_instance.my_ec2" -var-file="prod.tfvars"
...
Destroy complete! Resources: 1 destroyed.
terraform taint aws_instance.my_ec2
Resource instance aws_instance.my_ec2 has been marked as tainted.
terraform untaint aws_instance.my_ec2
Resource instance aws_instance.my_ec2 has been successfully untainted.
terraform state list
aws_instance.foo
aws_instance.bar[0]
aws_instance.bar[1]
module.elb.aws_elb.main
terraform state list aws_instance.bar
aws_instance.bar[0]
aws_instance.bar[1]
terraform state pull > example.tfstate
terraform state push
terraform state rm aws_instance.bar
terraform force-unlock <LOCK_ID>
Lock ID LOCK_ID released
terraform show -json
terraform show -json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"aws_instance.example": {
"type": "aws_instance",
"depends_on": [],
"primary": {
"id": "i-1234567890abcdef0",
"attributes": {
"ami": "ami-0c55b159cbfafe1f0",
"instance_type": "t2.micro",
"tags": {
"Name": "example-server"
}
}
}
}
}