OpenTofu is an OSS tool for building, changing, and versioning infrastructure safely and efficiently. OpenTofu can manage existing and popular service providers as well as custom in-house solutions.
Commands
Format and Validate
Command
Description
terraform init
Prepare your working directory for other commands
terraform fmt
Reformat your configuration in the standard style
terraform validate
Check whether the configuration is valid
Plan and apply
Command
Description
terraform apply --auto-approve
Create or update infrastructure without confirmation prompt
terraform destroy --auto-approve
Destroy previously-created infrastructure without confirmation prompt
terraform plan -out plan.out
Output the deployment plan to plan.out
terraform apply plan.out
Use the plan.out to deploy infrastructure
terraform plan -destroy
Outputs a destroy plan
terraform apply -target=aws_instance.myinstance
Only apply/deploy changes to targeted resource
terraform apply -var myregion=us-east-1
Pass a variable via CLI while applying a configuration
terraform apply -lock=true
Lock the state file so it can’t be modified
terraform apply refresh=false
Do not reconcile state file with real-world resources
terraform apply --parallelism=5
Number of simultaneous resource operations
terraform refresh
Reconcile the state in Terraform state file with real-world resources
terraform providers
Get informatino about providers used in the current configuration
Workspaces
Command
Description
terraform workspace new <workspace>
Create a new workspace
terraform workspace select default
Change to a workspace
terraform workspace list
List all workspaces
State Manipulation
Command
Description
terraform state show aws_instance.myinstance
Show details stored in the Terraform state file
terraform state pull > terraform.tfstate
Output Terraform state to a file
terraform state mv aws_iam_role.my_ssm_role module.mymodule
Move a resource tracked via state to different module
terraform state replace-provider hashicorp/aws registry.custom.com/aws
Replace an existing provider with another
terraform state list
List all resources tracked in the Terraform state file
terraform state rm aws_instance.myinstance
Unmanage a resource, delete it from the Terraform state file