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.| Step | Purpose | CLI Examples |
|---|---|---|
| Write | Create infrastructure definitions in HCL (.tf files). | # Edit .tf files |
| Format & Validate | Ensure readable, valid configuration. | terraform fmtterraform validate |
| Init | Initialize the working directory: download providers, configure backend. | terraform init |
| Plan | Preview proposed changes before applying. | terraform planterraform plan -out=planfile |
| Apply | Provision or modify resources described in configuration. | terraform applyterraform apply planfile |
| Destroy | Tear down managed resources. | terraform destroy |
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.| Command | Purpose | Example |
|---|---|---|
terraform init | Initialize a working directory and download providers. | terraform init |
terraform fmt | Format configuration files. | terraform fmt |
terraform validate | Validate configuration for syntax and internal consistency. | terraform validate |
terraform plan | Generate and display an execution plan. | terraform plan -out=planfile |
terraform apply | Execute changes to reach the desired state. | terraform apply |
terraform destroy | Destroy remote resources managed by a configuration. | terraform destroy |
terraform state | Inspect and modify the state file. | terraform state list |
terraform show | Show state or saved plan details. | terraform show planfile |
terraform import | Import existing resources into state. | terraform import aws_instance.example i-12345678 |

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