Gateway rest API 资源找不到我提供的文件
Gateway rest API resource can't find the file I provide
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 粘贴到正文中时它起作用,但当我提供文件时它不起作用。这是为什么?
由于 Terraform 不知道文件的位置,您应该明确指定它:
- 如果文件在同一目录下,则使用
./apigateway/json-resolved/swagger.json
- 如果文件是您 运行 Terraform 所在目录的上一级目录,您可以使用
../apigateway/json-resolved/swagger.json
- 或者,使用 Terraform built-in 函数进行路径操作是个好主意:
path.cwd
、path.module
或 path.root
。关于这三个函数代表什么的更详细的解释可以在[1]中找到。
- 在文件所在的目录中通过运行
pwd
提供文件的完整路径(这适用于Linux和MacOS)并粘贴命令的结果在file
函数输入中。
此外,第 2 点和第 3 点的任意组合也可以,但您应该小心。
类似问题还有另一个很好的答案 [2]。
注意:在某些情况下,path.*
函数可能不会在 Windows 上给出预期的结果。根据来自 Github 的评论 [3],如果路径使用一致(即所有 /
或所有 \
),Windows 也应该能够使用path.*
但仅适用于 Terraform >=0.12
版本。根据代码片段形式,在这种情况下似乎使用了旧版本的问题。
[1] https://www.terraform.io/language/expressions/references#filesystem-and-workspace-info
[2]
[3] https://github.com/hashicorp/terraform/issues/14986#issuecomment-448756885
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 粘贴到正文中时它起作用,但当我提供文件时它不起作用。这是为什么?
由于 Terraform 不知道文件的位置,您应该明确指定它:
- 如果文件在同一目录下,则使用
./apigateway/json-resolved/swagger.json
- 如果文件是您 运行 Terraform 所在目录的上一级目录,您可以使用
../apigateway/json-resolved/swagger.json
- 或者,使用 Terraform built-in 函数进行路径操作是个好主意:
path.cwd
、path.module
或path.root
。关于这三个函数代表什么的更详细的解释可以在[1]中找到。 - 在文件所在的目录中通过运行
pwd
提供文件的完整路径(这适用于Linux和MacOS)并粘贴命令的结果在file
函数输入中。
此外,第 2 点和第 3 点的任意组合也可以,但您应该小心。
类似问题还有另一个很好的答案 [2]。
注意:在某些情况下,path.*
函数可能不会在 Windows 上给出预期的结果。根据来自 Github 的评论 [3],如果路径使用一致(即所有 /
或所有 \
),Windows 也应该能够使用path.*
但仅适用于 Terraform >=0.12
版本。根据代码片段形式,在这种情况下似乎使用了旧版本的问题。
[1] https://www.terraform.io/language/expressions/references#filesystem-and-workspace-info
[2]
[3] https://github.com/hashicorp/terraform/issues/14986#issuecomment-448756885