【问题标题】:`terraform providers` command confusing resources for providers`terraform providers` 命令混淆了提供者的资源
【发布时间】:2020-11-20 15:40:59
【问题描述】:

我正在学习开发一个 terraform 提供程序。我正在关注 hashcorp 整理的这个惊人的教程:https://learn.hashicorp.com/tutorials/terraform/provider-use?in=terraform/providers#create-order

Terraform init 在我添加 required_providers 块时起作用。但是,一旦我添加了资源并执行terraform apply,应用失败并出现“无法加载插件”错误。在此错误之后,当我运行 terraform providers 进行调试时,它会将提供程序和资源列为必需的提供程序。

我正在使用的 Terraform 版本:0.13.5

下面是我使用的代码:

    required_providers {
      hashicups = {
          versions = ["0.3"]
          source = "hashicorp.com/edu/hashicups"
      }
    }
}

resource "haschicups_order" "edu" {
  items {
    coffee {
      id = 3
    }
    quantity = 2
  }
  items {
    coffee {
      id = 2
    }
    quantity = 2
  }
}

output "edu_order" {
  value = haschicups_order.edu
}```

```% terraform providers

Providers required by configuration:
.
├── provider[hashicorp.com/edu/hashicups]
└── provider[registry.terraform.io/hashicorp/haschicups]```

【问题讨论】:

  • 从历史上看,terraform providers 子命令确实被本地构建和加载的提供程序弄糊涂了。这种行为似乎仍然存在。
  • “无法加载插件”的 Terraform 错误,给出的原因是“配置提供了无法定位的所需插件,不满足版本约束”。我确信这个错误引用了 terraform 作为提供者令人困惑的资源

标签: plugins terraform


【解决方案1】:

您似乎为资源使用了不正确的资源类型名称:您写的是 haschicups_order 而不是 hashicups_order

由于您尚未声明本地名称为 haschicups 的提供者,Terraform 假设此资源类型必须属于名为 hashicorp/haschicups 的提供者,采用与 Terraform 允许声明 aws_instance 资源相同的机制明确声明 AWS 提供商。

如果您将资源类型名称更正为与提供者的本地名称具有相同的前缀,则 Terraform 应该按照您的预期将资源与提供者关联:

resource "hashicups_order" "edu" {
  items {
    coffee {
      id = 3
    }
    quantity = 2
  }
  items {
    coffee {
      id = 2
    }
    quantity = 2
  }
}

【讨论】:

  • 谢谢马丁!后来我发现我需要使用提供者前缀才能将资源识别为提供者的一部分。也感谢您指出提供者名称的更正!
猜你喜欢
  • 2021-07-17
  • 2018-07-30
  • 2018-11-11
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2019-04-06
  • 1970-01-01
相关资源
最近更新 更多