【问题标题】:Gateway rest API resource can't find the file I provide网关休息 API 资源找不到我提供的文件
【发布时间】:2022-01-21 14:39:34
【问题描述】:
resource "aws_api_gateway_rest_api" "api" {
  body = "${file("apigateway/json-resolved/swagger.json")}"
  name = "api"
}

---------------------------------------------------------------------------------
Invalid value for "path" parameter: no file exists at apigateway/json-resolved/swagger.json;
this function works only with files that are distributed as 
part of the configuration source code, 
so if this file will be created by a resource in this configuration you must 
instead obtain this result from an attribute of that resource.

当我尝试通过提供 API JSON 的实际路径来部署我的 API 时,这就是它所抛出的。即使文件在那里,即使我尝试了不同的路径,从相对路径到绝对路径等。当我将整个 JSON 粘贴到正文中时它可以工作,但当我提供文件时它不能工作。这是为什么呢?

【问题讨论】:

  • 您需要一个前导./,或者如果文件位于与您运行代码的位置相比的子目录中,您可以使用path.cwd,例如,${file("${ path.cwd}/apigateway/json-resolved/swagger.json")}"
  • @MarkoE ".." 为我工作,谢谢!
  • @MarkoE 建议转换为答案。

标签: json terraform aws-api-gateway


【解决方案1】:

由于 Terraform 不知道文件的位置,您应该明确指定它:

  1. 如果文件在同一目录下,则使用./apigateway/json-resolved/swagger.json
  2. 如果文件是运行 Terraform 的目录的上一级目录,则可以使用 ../apigateway/json-resolved/swagger.json
  3. 或者,最好使用 Terraform 内置函数进行路径操作:path.cwdpath.modulepath.root。关于这三个函数代表什么的更详细的解释可以在 [1] 中找到。
  4. 通过在文件所在的目录中运行pwd 来提供文件的完整路径(这适用于Linux 和MacOS)并将命令的结果粘贴到file 函数输入中。

此外,第 2. 点和第 3. 点的任意组合也可以使用,但您应该小心。

对于类似的问题 [2],还有另一个很好的答案。

注意:在某些情况下,path.* 函数在 Windows 上可能不会给出预期的结果。根据 Github 的此评论 [3],如果一致使用路径(即,所有 / 或所有 \),Windows 也应该能够使用 path.*,但仅适用于 Terraform 的版本 >=0.12 .根据代码 sn-p 形式的问题,在这种情况下似乎使用的是旧版本。


[1]https://www.terraform.io/language/expressions/references#filesystem-and-workspace-info

[2]Invalid value for "path" parameter: no file exists at

[3]https://github.com/hashicorp/terraform/issues/14986#issuecomment-448756885

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2016-05-01
    • 2018-01-03
    • 2014-02-02
    • 2014-06-30
    相关资源
    最近更新 更多