Command structure and examples
Commands are invoked with the base keywordterraform followed by a subcommand and optional flags or parameters:
terraform— invokes the Terraform CLI.plan— subcommand that generates an execution plan.-out=planfile— optional flag that saves the plan for later use withapply.
Terraform workflow mapped to CLI commands
Terraform’s recommended workflow maps directly to CLI subcommands. Follow these steps to develop, test, and manage infrastructure safely.
Canonical sequence of commands:
terraform plan before terraform apply is a best practice—it lets you review changes and avoids surprises.
Be careful with
terraform destroy. It will remove infrastructure managed by Terraform. Confirm that you really intend to destroy resources before typing yes.Core CLI commands — quick reference
Below are the most commonly used Terraform subcommands with a short description and example usage.
This list is not exhaustive but covers the commands most teams use daily.

Additional commonly used subcommands
Beyond the core workflow, Terraform includes powerful subcommands for state management, debugging, and advanced operations:terraform state— inspect and manually modify state objects (use carefully).terraform show— display state or plan details in a human-readable format.terraform import— bring existing external resources under Terraform management.terraform fmtandterraform validate— help enforce configuration quality and consistency.
Environment variables
Environment variables are a secure, convenient way to provide credentials, configure Terraform behavior, and set input variables without embedding secrets in.tf files.
Why use environment variables?
- Avoid committing credentials into version control.
- Provide defaults or overrides for variables in CI/CD pipelines.
- Control logging and runtime behavior for debugging.
TF_LOG— set Terraform logging level (TRACE,DEBUG,INFO,WARN,ERROR). Useful for troubleshooting.TF_VAR_<name>— set Terraform input variables from the environment. For example:A configuration referencingvar.server_namewill pick up this value.- Provider-specific variables — many providers support environment-based credentials (for example AWS or Azure environment variables). Terraform checks multiple sources for credentials, including environment variables and credential helpers.
Use environment variables for credentials and sensitive values instead of hardcoding them into
.tf files to reduce the risk of accidentally committing secrets.Best practices and tips
- Always run
terraform fmtandterraform validatebefore committing changes. - Use
terraform plan -out=planfileandterraform apply planfileto ensure the exact planned changes are applied. - Store state securely (remote backends like S3 with locking via DynamoDB, or HashiCorp Consul, are recommended for team workflows).
- Use workspaces, modules, and variable files to organize environments and reusable components.
- Limit the use of
terraform statecommands; prefer importing and re-creating resources through configuration when possible.
Links and references
- Terraform documentation: https://www.terraform.io/docs
- Terraform CLI reference: https://www.terraform.io/cli
- State and backends: https://www.terraform.io/language/state