只处理特定的资源
首先
到目前为止,我们按照对象将验证分成了不同的文件夹进行。
对每个文件夹,我们执行了init、plan和apply命令。
更麻烦的是,terraform.tfvars和variables.tf是相同的文件。
我们将它们复制到每个文件夹中。
└terraform
├ec2
│ ├terraform.tfvars
│ ├variables.tf
│ └ec2.tf
├ec2ssm
│ ├terraform.tfvars
│ ├variables.tf
│ ├ec2.tf
│ └role.tf
└s3
├terraform.tfvars
├variables.tf
└s3.tf
但是,这种结构更易于管理,并且工作文件夹也可以集中在一个地方处理。
└terraform
├terraform.tfvars
├variables.tf
├ec2.tf
├ec2.tf
├role.tf
└s3.tf
在查找Terraform选项时,我发现可能可以使用“-terget”选项。
假设已初始化上述文件夹结构,我们立即进行验证。
请参照以下内容的汉语表达方式,仅需要给出一种选择:
引用
请参考以下内容的中文表述,只需要给出一种选项:
首次使用Terraform
通过SSM访问EC2
使用Terraform注册S3存储桶
命令:计划
使用Terraform指定目标删除
操作步驟
# terraform plan -target=s3.tf
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes
are needed.
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the result of this plan may not represent all of
│ the changes requested by the current configuration.
│
│ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from
│ errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.
发生错误了…
经过查询,似乎需要指定资源而不是文件作为目标。
那么我会尝试使用这个命令进行验证。
首先是S3。
让我们尝试指定s3.tf中的”resource”。
resource "aws_s3_bucket" "bucket1" {
bucket = "xxxxx-tf-bucket"
acl = "private"
}
# terraform plan -target=aws_s3_bucket.bucket1
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
+ create
:
:
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.
计划成功地顺利执行。
为了保险起见,我也会尝试一下 EC2。
同样,我会尝试将 “Resource” 在 ec2.tf 文件中指定。
resource "aws_instance" "xxxxx_tf-ec2" {
count = 1
ami = "ami-03d5c68bab01f3496" # Ubuntu 20.04 LTS official ami
instance_type = "t2.micro"
iam_instance_profile = aws_iam_instance_profile.systems_manager.name
tags = {
Name = "${format("xxxxx_tf-ec2-%02d", count.index + 1)}"
}
}
这边的计划也顺利执行完成。
# terraform plan -target=aws_instance.xxxxx_tf-ec2
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
+ create
:
:
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.
在中文中,可以这样表述:计划和申请都可以执行,也可以销毁。
最后
这个验证在什么情况下有用呢?
比如说,当部署的目标资源很多的时候。
如果只运行特定的资源,可以缩短执行时间。
关于计划方面,已经确认过的资源不需要执行,只需要确认特定的资源,这种情况下会有帮助。