Getting CLI help
Open a terminal in your working directory and request top-level help to see global usage and available subcommands:| Command | Use Case |
|---|---|
init | Initialize a working directory and download provider plugins |
validate | Check HCL syntax and basic configuration consistency |
plan | Produce an execution plan without changing infrastructure |
apply | Create or update resources described by the configuration |
destroy | Remove resources managed by Terraform |
state | Advanced state inspection and manipulation |
workspace | Manage named workspaces for different state instances |
taint and untaint were deprecated in favor of flags such as -replace).
Global options (use these before the subcommand)
validate documents JSON output and color control flags).
Callout: subcommand help
You can get help for any subcommand (for example,
terraform plan --help) to see detailed options, examples, and usage notes.Deep dive: terraform plan
terraform plan generates a speculative execution plan showing what Terraform would do to bring infrastructure in line with the configuration. It does not perform any changes unless you later pass a saved plan file to apply.
Example help excerpt:
| Flag | Purpose | Example |
|---|---|---|
-replace=resource_address | Force resource replacement (replacement of taint) | terraform plan -replace=aws_instance.web |
-target=resource_address | Plan only specific resource(s) | terraform plan -target=module.db |
-var | Set a variable from the CLI | terraform plan -var 'num_of_pets=3' |
-var-file | Load variables from a file | terraform plan -var-file=prod.tfvars |
-out | Save a plan to a path for later apply | terraform plan -out=tfplan |
-parallelism | Limit concurrent operations | terraform plan -parallelism=4 |
- Use
-outif you want to guarantee thatapplyperforms the same actions as planned. - Prefer
-var-fileor environment variables in CI to avoid leaking secrets in shell history.
Passing Terraform variables via environment
Terraform supports setting input variables from the environment using theTF_VAR_<variable_name> pattern. For the example above, set TF_VAR_num_of_pets to provide num_of_pets from the environment.
If you run terraform plan without supplying num_of_pets, Terraform will prompt:
terraform plan will now use that value without prompting:
TF_VAR_num_of_pets to another number (for example 6), resources depending on var.num_of_pets will reflect the new value on the next plan or apply.
Callout: using TF_VAR_ environment variables
Use
TF_VAR_<variable_name> to inject Terraform variable values from the environment. Example: export TF_VAR_num_of_pets=3. This is useful in CI/CD pipelines and local scripts.Environment variables for provider authentication
Provider SDKs typically read standard environment variables for authentication. These are provider-specific and are not Terraform-level variables. Common provider authentication environment variables:| Provider | Common env vars | Notes / docs |
|---|---|---|
| AWS | AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION | See AWS provider docs: https://registry.terraform.io/providers/hashicorp/aws |
| GCP | GOOGLE_CREDENTIALS, GOOGLE_PROJECT, GOOGLE_REGION | Accepts service account JSON via GOOGLE_CREDENTIALS |
| Azure | ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_SUBSCRIPTION_ID, ARM_TENANT_ID | AzureRM provider reads these |
| Vault | VAULT_ADDR, VAULT_TOKEN | Use VAULT_TOKEN or approles for auth |
plan or apply.
Callout: protect credentials
Never commit credentials, tokens, or secret keys to version control. Use secure secrets management, environment injection in CI/CD, or a secrets backend (e.g., HashiCorp Vault) instead of hardcoding secrets in files.
Summary and quick checklist
- Use
terraform --helpandterraform <subcommand> --helpto discover commands, flags, and usage examples. terraform plansupports many customization flags (-replace,-target,-var,-var-file,-out); use-outwhen you need a reproducible apply step.- Supply Terraform input variables via the environment using
TF_VAR_<variable_name>for automation-friendly workflows. - Provider authentication commonly relies on provider-specific environment variables (for example, AWS and Vault); consult provider docs for exact variable names.
- Never store secrets in source control—use secure vaults or platform-managed secret injection.
- Terraform CLI documentation: https://www.terraform.io/docs/cli
- Terraform Providers: https://registry.terraform.io/browse/providers
- AWS provider docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs
- HashiCorp Vault: https://www.vaultproject.io/docs