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 粘貼到正文中時它可以作業,但當我提供檔案時它不能作業。這是為什么?
uj5u.com熱心網友回復:
由于 Terraform 不知道檔案的位置,因此您應該明確指定它:
- 如果檔案在同一目錄中,則使用
./apigateway/json-resolved/swagger.json - 如果該檔案是運行 Terraform 的目錄的上一個目錄,則可以使用
../apigateway/json-resolved/swagger.json - 或者,使用 Terraform 內置函式進行路徑操作是個好主意:
path.cwd、path.module或path.root. 關于這三個函式代表什么的更詳細的解釋可以在 [1] 中找到。 pwd通過在檔案所在的目錄中運行來提供檔案的完整路徑(這適用于 Linux 和 MacOS)并將命令的結果粘貼到file函式輸入中。
此外,第 2 點和第 3 點的任何組合也可以使用,但您應該小心。
對于類似的問題[2],還有另一個很好的答案。
注意:在某些情況下,這些path.*函式在 Windows 上可能不會給出預期的結果。根據 Github 的此評論 [3],如果路徑使用一致(即 all/或 all \),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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418932.html
標籤:
