Basic Terraform Commands
-
Initialize a Terraform Configuration
terraform init
- Downloads provider plugins and initializes the working directory.
-
Validate Terraform Configuration
terraform validate
- Validates the syntax and configuration of your Terraform files.
-
Format Terraform Configuration
terraform fmt
- Formats Terraform configuration files to a canonical format.
-
Show the Current State
terraform show
- Displays the current state or a state file.
-
Generate an Execution Plan
terraform plan
- Shows what actions Terraform will take based on the current configuration.
-
Apply the Configuration
terraform apply
- Applies the changes required to reach the desired state of the configuration.
-
Destroy Infrastructure
terraform destroy terraform destroy -target aws_instance.myec2 # destroy a specific resource
- Destroys all resources managed by the Terraform configuration.
-
Refresh the State
terraform refresh
- Updates the state file with the latest information from the provider.
-
Output Information from State
terraform output
- Displays the values of output variables defined in the configuration.
-
Get Provider Plugins
terraform get
- Downloads provider plugins specified in the configuration.
Intermediate Terraform Commands
-
Upgrade Providers
terraform providers lock -upgrade
- Upgrades provider versions to match constraints in the configuration.
-
List All Resources
terraform state list
- Lists all resources in the state file.
-
Show Detailed Information About a Resource
terraform state show <resource_address>
- Shows detailed state information for a specific resource.
-
Remove Resource from State
terraform state rm <resource_address>
- Removes a resource from the Terraform state without destroying it.
-
Import Existing Infrastructure
terraform import <resource_address> <resource_id>
- Imports existing infrastructure into Terraform management.
-
Create a Plan File
terraform plan -out=<plan_file>
- Saves the execution plan to a file for review or application later.
-
Apply a Specific Plan File
terraform apply <plan_file>
- Applies the changes described in a saved plan file.
-
View the Resource Graph
terraform graph
- Outputs a visual representation of dependencies among resources in DOT format.
-
List Available Providers
terraform providers
- Lists all providers used in the configuration.
-
Workspace Management
- Create a New Workspace:
terraform workspace new <workspace_name>
- Select a Workspace:
terraform workspace select <workspace_name>
- List Workspaces:
terraform workspace list
- Delete a Workspace:
terraform workspace delete <workspace_name>
- Create a New Workspace:
-
Configure Backend
terraform init -backend-config="path/to/backend-config"
- Configures the backend settings for state storage.
Tips
- Use Aliases for Providers: Manage multiple provider versions by defining aliases in your configuration.
- Remote State Management: Consider using remote state storage (e.g., S3 with DynamoDB locking) for collaborative environments.
- Version Control: Always use version control for your Terraform configuration files and state files.
These commands should cover the majority of your needs for managing infrastructure with Terraform, from initial setup to advanced state management. 🚀