我将写下常用的Terraform命令
首先
我想要针对经常在Terraform中使用的基本命令进行输出。
请看以下的文章,在那里我详细说明了在Mac环境下进行Terraform设置的步骤,希望您也能参考一下。
Terraform是什么?

这是由HashiCorp公司提供的配置管理工具。
它是一种能够自动化云上资源的创建和修改的工具(即基础设施即代码)。
经常使用的命令
-
- 初期化
-
- フォーマット
-
- 実行計画の確認
-
- 変更の適用
-
- リソースの削除
- リソースの状態確認
初始化
使用该命令可以初始化Terraform工作目录。在执行Terraform之前,首先要执行此命令。
terraform init
satsuton:EC2_Create ma-sato$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "> 3.0.0"...
- Installing hashicorp/aws v4.12.1...
- Installed hashicorp/aws v4.12.1 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
satsuton:EC2_Create ma-sato$
格式
这个命令是用于整理Terraform代码格式的。
terraform fmt
satsuton:EC2_Create ma-sato$ terraform fmt
main.tf
satsuton:EC2_Create ma-sato$
确认执行计划
这个命令基于当前源代码来确认执行计划。
在执行terraform apply之前,使用它进行预先确认。
terraform plan
satsuton:EC2_Create ma-sato$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.server will be created
+ resource "aws_instance" "server" {
+ ami = "ami-02c3627b04781eada"
+ arn = (known after apply)
+ associate_public_ip_address = true
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ disable_api_termination = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ id = (known after apply)
+ instance_initiated_shutdown_behavior = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
〜〜〜〜長いため省略〜〜〜〜
+ main_route_table_id = (known after apply)
+ owner_id = (known after apply)
+ tags = {
+ "Env" = "dev"
+ "Name" = "Test-dev-vpc"
+ "Project" = "Test"
}
+ tags_all = {
+ "Env" = "dev"
+ "Name" = "Test-dev-vpc"
+ "Project" = "Test"
}
}
Plan: 6 to add, 0 to change, 0 to destroy.
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
satsuton:EC2_Create ma-sato$
应用更改
根据现行源代码进行变更的应用命令。
terraform apply
satsuton:EC2_Create ma-sato$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.server will be created
+ resource "aws_instance" "server" {
+ ami = "ami-02c3627b04781eada"
+ arn = (known after apply)
+ associate_public_ip_address = true
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
〜〜〜長いので省略〜〜〜
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
〜〜〜長いので省略〜〜〜
aws_route.public_rt_igw_r: Creation complete after 0s [id=r-rtb-0b8e5cc75b96605211080289494]
aws_security_group.opmng_sg: Creation complete after 2s [id=sg-05977eae8905e2de6]
aws_security_group_rule.opmng_in_ssh: Creating...
aws_security_group_rule.opmng_in_ssh: Creation complete after 1s [id=sgrule-3046359132]
aws_subnet.public_subnet: Still creating... [10s elapsed]
aws_subnet.public_subnet: Creation complete after 11s [id=subnet-0fe22cf846150399d]
aws_route_table_association.public_rt_1a: Creating...
aws_instance.server: Creating...
aws_route_table_association.public_rt_1a: Creation complete after 1s [id=rtbassoc-06d4deb245e69daf9]
aws_instance.server: Still creating... [10s elapsed]
aws_instance.server: Still creating... [20s elapsed]
aws_instance.server: Still creating... [30s elapsed]
aws_instance.server: Creation complete after 32s [id=i-07aafe6f29ee808ca]
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
satsuton:EC2_Create ma-sato$
如果在选项中加上”-auto-approve”参数,可以强制自动批准并进一步操作。
terraform apply -auto-approve
删除资源
这是用于删除由Terraform管理的资源的命令。
terraform destroy
satsuton:EC2_Create ma-sato$ terraform destroy
aws_vpc.vpc: Refreshing state... [id=vpc-046903fc5cd5883ff]
aws_key_pair.keypair: Refreshing state... [id=Test-dev-keypeir]
aws_internet_gateway.igw: Refreshing state... [id=igw-02d0932dea1636f58]
aws_subnet.public_subnet: Refreshing state... [id=subnet-0f68dc0d33de46ee2]
aws_security_group.opmng_sg: Refreshing state... [id=sg-0f6d6d027d317871d]
aws_route_table.public_rt: Refreshing state... [id=rtb-06f54a5eee901f156]
aws_route_table_association.public_rt_1a: Refreshing state... [id=rtbassoc-012b28476c8a667f6]
aws_route.public_rt_igw_r: Refreshing state... [id=r-rtb-06f54a5eee901f1561080289494]
aws_security_group_rule.opmng_in_ssh: Refreshing state... [id=sgrule-4054515482]
aws_instance.server: Refreshing state... [id=i-018d7d758c615b5f7]
〜〜〜長いので省略〜〜〜
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
〜〜〜長いので省略〜〜〜
aws_key_pair.keypair: Destroying... [id=Test-dev-keypeir]
aws_subnet.public_subnet: Destroying... [id=subnet-0f68dc0d33de46ee2]
aws_security_group.opmng_sg: Destroying... [id=sg-0f6d6d027d317871d]
aws_key_pair.keypair: Destruction complete after 0s
aws_security_group.opmng_sg: Destruction complete after 0s
aws_subnet.public_subnet: Destruction complete after 0s
aws_vpc.vpc: Destroying... [id=vpc-046903fc5cd5883ff]
aws_vpc.vpc: Destruction complete after 1s
Destroy complete! Resources: 10 destroyed.
satsuton:EC2_Create ma-sato$
如果在选项中添加“-auto-approve”,则可以强制选择“是”来继续进行。
terraform destroy -auto-approve
确认资源的状态
以下是一个基于 terraform.state 文件查看当前资源状态的命令。
terraform show
terraform.state 是一個記錄管理對象資源配置狀態的檔案。
satsuton:EC2_Create ma-sato$ terraform show
# aws_instance.server:
resource "aws_instance" "server" {
ami = "ami-02c3627b04781eada"
arn = "arn:aws:ec2:ap-northeast-1:555246305565:instance/i-006b8fa6653595f1f"
associate_public_ip_address = true
availability_zone = "ap-northeast-1a"
cpu_core_count = 1
cpu_threads_per_core = 1
disable_api_termination = false
ebs_optimized = false
get_password_data = false
hibernation = false
id = "i-006b8fa6653595f1f"
〜〜〜長いので省略〜〜〜
tags = {
"Env" = "dev"
"Name" = "Test-dev-vpc"
"Project" = "Test"
}
tags_all = {
"Env" = "dev"
"Name" = "Test-dev-vpc"
"Project" = "Test"
}
}
satsuton:EC2_Create ma-sato$
总结
我将整理本次介绍的命令列表。
コマンド説明備考terraform initTerraformの作業ディレクトリの初期化Terraformを実行する際に一番最初に実行terraform fmtterraformのコードのフォーマットを整える-terraform plan現状のソースコードをもとに実行計画を確認する
terraform apply
の実行前に実行terraform validate構成ファイルの記述が全て正常であることを確認terraform apply
の実行前に実行terraform apply現状のソースコードに従って変更を適用以下のコマンドにて強制的にYesで実行可能terraform apply -auto-approve
terraform destroyterraformで管理されているリソースを削除以下のコマンドにて強制的にYesで実行可能terraform destroy -auto-approve
terraform showterraform.state
ファイルを元に現在のリソースの状態を参照するリソースに変更を加えた際等に確認のために実行最后
以下是正在使用的代码。
*正在编写README。代码也可能会更改(目标是使其能够直接执行)。
请参考下文。
5分钟了解Terraform(基础设施即代码)
针对Terraform新手的超级简单总结