“路径”参数值无效:不存在文件

May*_*and 7 amazon-web-services terraform

正如Terraform Resource: 执行 apply 时发生连接错误中所述

我将代码更改为以下内容

provisioner "remote-exec" {
    connection {
      type        = "ssh"
      host        = aws_eip.nat-eip.public_ip
      user        = "ubuntu"
      private_key = file("/id_rsa.pem")
    }
    inline = [
      "chmod +x /tmp/start_node.sh",
      "sudo sed -i -e 's/\r$//' /tmp/start_node.sh", # Remove the spurious CR characters.
      "sudo /tmp/start_node.sh",
    ]
  }
Run Code Online (Sandbox Code Playgroud)

但我仍然遇到同样的错误

Error: Invalid function argument

  on explorer.tf line 60, in resource "aws_instance" "explorer":
  60:       private_key = file("/id_rsa.pem")

Invalid value for "path" parameter: no file exists at /id_rsa.pem;

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.
Run Code Online (Sandbox Code Playgroud)

ls -la输出

total 156
drwxr-xr-x  10 CORP\mayuresh CORP\domain users  4096 Jan 12 14:29 .
drwxr-xr-x  16 CORP\mayuresh CORP\domain users  4096 Jan 10 13:10 ..
drwxr-xr-x  12 CORP\mayuresh CORP\domain users  4096 Jan 12 09:49 byoc-terraform
drwxr-xr-x   2 CORP\mayuresh CORP\domain users  4096 Jan 11 11:57 controllers
-rw-r--r--   1 CORP\mayuresh CORP\domain users   188 Jan 10 13:27 .env
-rw-r--r--   1 CORP\mayuresh CORP\domain users  1582 Jan 10 17:12 fetchUserData.js
drwxr-xr-x   9 CORP\mayuresh CORP\domain users  4096 Jan 12 13:14 .git
-rw-r--r--   1 CORP\mayuresh CORP\domain users   629 Jan 10 13:27 .gitignore
-rw-r--r--   1 CORP\mayuresh CORP\domain users   107 Dec 30 06:49 .gitmodules
-rw-r--r--   1 CORP\mayuresh CORP\domain users  1765 Jan 12 13:21 id_rsa.pem
-rw-r--r--   1 CORP\mayuresh CORP\domain users  1488 Jan 10 13:27 index.js
drwxr-xr-x   3 CORP\mayuresh CORP\domain users  4096 Jan 10 13:27 models
drwxr-xr-x 221 CORP\mayuresh CORP\domain users 12288 Jan 10 13:30 node_modules
-rw-r--r--   1 CORP\mayuresh CORP\domain users  1058 Jan 10 13:27 package.json
-rw-r--r--   1 CORP\mayuresh CORP\domain users 78791 Jan 10 13:27 package-lock.json
drwxr-xr-x   2 CORP\mayuresh CORP\domain users  4096 Jan 10 13:27 routes
drwxr-xr-x   2 CORP\mayuresh CORP\domain users  4096 Jan 10 17:01 utils
drwxr-xr-x   2 CORP\mayuresh CORP\domain users  4096 Jan 10 13:27 VMCreationFiles```
Run Code Online (Sandbox Code Playgroud)

Ter*_*ato 17

您是否尝试过使用完整路径?如果您使用模块,则特别有用。IE:

private_key = file("${path.module}/id_rsa.pem")
Run Code Online (Sandbox Code Playgroud)

或者我认为这也行

private_key = file("./id_rsa.pem")
Run Code Online (Sandbox Code Playgroud)

我相信您现有的代码正在文件系统的根目录中查找该文件。

  • 将 ${path.module} 添加到文件名对我有用。我在 WSL2 上使用 Ubuntu。 (3认同)

Jun*_*aid 1

你的道路.pem是错误的。看起来该文件存在于您的 $HOME 目录中。

id_rsa.pem如果该文件位于外部,您可以提供该文件的绝对路径path.module, path.root, path.cwd

提供绝对路径

  1. 获取文件的完整路径如何获取文件的完整路径?
  2. 将路径粘贴到:
    provisioner "remote-exec" {
     connection {
       type        = "ssh"
       host        = aws_eip.nat-eip.public_ip
       user        = "ubuntu"
       private_key = file("<Absolute path to .pem file e.g /home/ubuntu/id_rsa.pem>")
     }
    
    Run Code Online (Sandbox Code Playgroud)