Linux command
terraform 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Initialize a Terraform working directory
terraform init
Preview infrastructure changes
terraform plan
Apply infrastructure changes
terraform apply
Destroy managed infrastructure
terraform destroy
Format configuration files
terraform fmt
Validate configuration syntax
terraform validate
Show current state
terraform show
List resources in state
terraform state list
Import existing infrastructure
terraform import [resource_type.name] [id]
说明
terraform is an infrastructure as code tool for building, changing, and versioning cloud infrastructure safely and efficiently. It uses declarative configuration files to describe desired infrastructure state. Configuration is written in HCL (HashiCorp Configuration Language) defining resources like servers, networks, and databases. Terraform communicates with cloud providers (AWS, Azure, GCP, etc.) via plugins called providers. The workflow is: init (setup), plan (preview), apply (execute). State is tracked in terraform.tfstate, enabling Terraform to know what exists and what needs changing. Workspaces allow managing multiple environments (dev, staging, prod) from the same configuration with different state files.
参数
- init
- Initialize working directory, download providers
- plan
- Create execution plan showing proposed changes
- apply
- Apply changes to reach desired state
- destroy
- Destroy all managed infrastructure
- fmt
- Format configuration files to canonical style
- validate
- Check configuration syntax and consistency
- show
- Display current state or saved plan
- output
- Read output values from state
- state
- Advanced state management commands
- import
- Import existing infrastructure into state
- workspace
- Manage workspaces
- -auto-approve
- Skip interactive approval (apply/destroy)
- -var _'key=value'_
- Set input variable
- -var-file _file_
- Load variables from file
- -target _resource_
- Target specific resource
- -parallelism _n_
- Limit concurrent operations
FAQ
What is the terraform command used for?
terraform is an infrastructure as code tool for building, changing, and versioning cloud infrastructure safely and efficiently. It uses declarative configuration files to describe desired infrastructure state. Configuration is written in HCL (HashiCorp Configuration Language) defining resources like servers, networks, and databases. Terraform communicates with cloud providers (AWS, Azure, GCP, etc.) via plugins called providers. The workflow is: init (setup), plan (preview), apply (execute). State is tracked in terraform.tfstate, enabling Terraform to know what exists and what needs changing. Workspaces allow managing multiple environments (dev, staging, prod) from the same configuration with different state files.
How do I run a basic terraform example?
Run `terraform init` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does init do in terraform?
Initialize working directory, download providers