7

As mentioned at Terraform Resource: Connection Error while executing apply

I changed my code to the the below

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",
    ]
  }

But I still get the same error

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.

ls -la ooutput

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```
Koen.
  • 25,449
  • 7
  • 83
  • 78
Mayuresh Anand
  • 193
  • 1
  • 3
  • 10
  • Don't paste error messages as images - they are not indexable for search (at least as of 2021) - i.e. others might not be able to find your question and benefit from it. – Grzegorz Oledzki Jan 12 '21 at 08:11
  • 1
    Thanks! I changed it . – Mayuresh Anand Jan 12 '21 at 08:15
  • Can you run `ls -la /id*` and post the output? It seems your private key is in a different location – Moshe Jan 12 '21 at 08:35
  • ```[CORP\mayuresh@a-2xr1uki591z7i byoc-backend]$ ls -la /id* ls: cannot access /id*: No such file or directory [CORP\mayuresh@a-2xr1uki591z7i byoc-backend]$ ls byoc-terraform fetchUserData.js index.js node_modules package-lock.json utils controllers id_rsa.pem models package.json routes VMCreationFiles [CORP\mayuresh@a-2xr1uki591z7i byoc-backend]$ ``` – Mayuresh Anand Jan 12 '21 at 09:00
  • I have also added the full ls -la in the question. Thanks for the help! – Mayuresh Anand Jan 12 '21 at 09:04
  • ```[CORP\mayuresh@a-2xr1uki591z7i byoc-backend]$ ls -la id*``` ```-rw-r--r-- 1 CORP\mayuresh CORP\domain users 1765 Jan 12 13:21 id_rsa.pem``` – Mayuresh Anand Jan 12 '21 at 09:09
  • Where is your `explorer.tf`? Your ls printout does not show such a file. – Marcin Jan 12 '21 at 10:32
  • explorer.tf is in the folder ```byoc-terraform``` and there as well I placed a copy of ```id_rsa.pem``` in every directory I placed a copy of this pem file to be extra sure but it doesnt read it. :( – Mayuresh Anand Jan 12 '21 at 17:48

2 Answers2

16

Have you tried using the full path? Especially beneficial if you are using modules. I.E:

private_key = file("${path.module}/id_rsa.pem")

Or I think even this will work

private_key = file("./id_rsa.pem")

I believe your existing code is looking for the file at the root of your filesystem.

Terry Sposato
  • 572
  • 2
  • 7
1

Your path to the .pem is wrong. It looks like the file exists in your $HOME directory.

You can provide the absolute path of the id_rsa.pem file if that file is outside of path.module, path.root, path.cwd

To provide the absolute path

  1. Fetch the full path of the file How to get full path of a file?
  2. Paste the path in:
    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>")
     }
    
Junaid
  • 3,477
  • 1
  • 24
  • 24