Detailed explanation of terraform installation and commands
Install Terraform
Mac system installation
Linux system installation
- Ubuntu installation
- CentOS system
Verify installation
|
|
Resource management of terraform commands
Resource initialization
For a terraform resource project, I created three basic files: main.tf (entry file), variables.tf (variable information), versions.tf (version information)
|
|
Format terraform files
fmt will format the .tf files in the current directory by default, and the format is standard tf format.
|
|
Create a resource plan
terraform plan checks that a set of changes to the execution plan match your expectations without changing the actual resources or state.
|
|
Creating cloud resources
Terraform apply will automatically generate a resource creation plan and approve the execution of the plan. At the same time, a tfstate file will be generated in the current directory.
|
|
View the created resource information
terraform show will view which resource data has been created for the current project,
terraform show -json View data in json format
|
|
Taint terrraform
The taint command is used to mark a resource as “tainted”. When the apply command is executed again, the tainted resource will be released first, and then a new one will be created. This is equivalent to deleting and then creating a new resource for this specific resource.
The terraform untaint command is the opposite, which is used to cancel the “tainted” mark and restore it to a normal state.
Destroy cloud resource data
terraform destroy will destroy cloud resource data according to the current resource configuration
|
|
Import cloud data into local projects
Terraform import generates local resource data through the cloud instance ID. The local directory will generate a terraform.tfstate file. Before importing existing data in the local project, please back up the tfstate file and .terraform directory. For the data that has been imported locally, you can use terraform show to display the terrafrom file format, copy it out and further process it to get the tf resource file content.
|
|
Terraform resource relationship drawing
There are different degrees of relationship between the resources defined in each template. Terraform graph can draw a large resource relationship diagram as follows:
|
|
This command The result can also be exported directly as a picture through the command terraform graph | dot -Tsvg > graph.svg (graphviz needs to be installed in advance: brew install graphviz )
|
|
By viewing graph.svg, you can see the relationship graph between various resources:
Terraform command state management
View the current state Store all resources
View the specific data of a resource
Remove specific resources
terraform state rm
|
|
Refresh resources
terraform refresh Refresh the current state content, call the cloud API to pull the latest data and write it to the state file
|
|