简体   繁体   English

terraform 计划以一种方式工作,但不是另一种方式?

[英]terraform plan works one way but not the other?

Terraform v0.12.17 Terraform v0.12.17

I just have 2 simple files我只有 2 个简单的文件

snapshot_id.tf = gets a list of my completed EBS volume snapshot ids
data "aws_ebs_snapshot_ids" "jenkins_master" {
  filter {
    name   = "tag:Name"
    values = ["jenkins-master"]
  }
  filter {
    name   = "status"
    values = ["completed"]
  }
}

ebs_volume_green.tf = use above data resource to create an EBS volume
resource "aws_ebs_volume" "jenkins_master_ebs_green" {
  availability_zone = var.availability_zones.green
  snapshot_id = data.aws_ebs_snapshot_ids.jenkins_master.id
         size = data.aws_ebs_snapshot_ids.jenkins_master.volume_size
         type = "gp2"
  tags = {
    Name = "jenkins-master-green"
    Environment = "sandbox"
    Product = "Jenkins"
    Role = "master"
  }
}

This passes, so obviously the resource has volume_size defined.这通过了,因此显然该资源定义了volume_size

$ terraform plan -target ebs_volume_green.tf -out out.output
$ terraform apply out.output

But this fails, ie, if I don't specify the -target option.但这失败了,即,如果我没有指定-target选项。 Why?为什么?

$ terraform plan -out out.output    
Error: Unsupported attribute

  on ebs_volume_green.tf line 4, in resource "aws_ebs_volume" "jenkins_master_ebs_green":
   4:          size = data.aws_ebs_snapshot_ids.jenkins_master.volume_size

This object has no argument, nested block, or exported attribute named
"volume_size".

You have confused the aws_ebs_snapshot_ids and the aws_ebs_snapshot data sources.您混淆了aws_ebs_snapshot_idsaws_ebs_snapshot数据源。

You should only be using the plural data sources if you need to return multiple of something.如果您需要返回多个数据源,您应该只使用复数数据源。 You can then pass these IDs into the individual data source that returns more useful information or just pass the IDs into something that takes a list of IDs such as the aws_autoscaling_group resource's vpc_zone_identifier parameter.然后,您可以将这些 ID 传递到返回更多有用信息的单个数据源中,或者仅将这些 ID 传递到采用 ID 列表的内容中,例如aws_autoscaling_group资源的vpc_zone_identifier参数。

In your case if you just want the most recent snapshot that matches your tags you would just use the following:在您的情况下,如果您只想要与您的标签匹配的最新快照,您只需使用以下内容:

data "aws_ebs_snapshot" "jenkins_master" {
  most_recent = true

  filter {
    name   = "tag:Name"
    values = ["jenkins-master"]
  }

  filter {
    name   = "status"
    values = ["completed"]
  }
}

This will then have the volume_size attribute from the data source that you are expecting.这将具有来自您期望的数据源的volume_size属性

In your question you can see that it plans and applies successfully when you target just the data source but it's only when your plan includes a usage of a resource or data source's output that doesn't exist that Terraform will complain because it's not evaluating everything in -target mode.在您的问题中,您可以看到当您仅针对数据源时,它计划并成功应用,但只有当您的计划包含不存在的资源或数据源输出的使用时,Terraform 才会抱怨,因为它没有评估中的所有内容-target模式。 In general if you have to use -target then something is wrong somewhere and you should see that as a red flag.一般来说,如果您必须使用-target则某处有问题,您应该将其视为危险信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM