Terraform资源:执行apply时发生连接错误



我正在尝试登录terraform将使用以下代码创建的ec2实例:

resource "aws_instance" "sess1" {
ami           = "ami-c58c1dd3"
instance_type = "t2.micro"
key_name        = "logon"
connection {
host= self.public_ip
user        = "ec2-user"
private_key = file("/logon.pem")
}

provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}

但这给了我一个错误:

PS C:UsersAmritvir SinghDocumentsGitHubAWS-ScribbleTerraform> terraform apply
provider.aws.region
The region where AWS operations will take place. Examples
are us-east-1, us-west-2, etc.
Enter a value: us-east-1

Error: Invalid function argument
on Session1.tf line 13, in resource "aws_instance" "sess1":
13:     private_key = file("/logon.pem")
Invalid value for "path" parameter: no file exists at logon.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.

在不登录控制台的情况下,如何在运行时保存将密钥从资源传递给provisioner?

您尝试过使用完整路径吗?如果您正在使用模块,则特别有益。I.E:

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

或者我认为即使这样也能起作用

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

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

连接应在provisioner块中:

resource "aws_instance" "sess1" {

ami           = "ami-c58c1dd3"
instance_type = "t2.micro"
key_name      = "logon"

provisioner "remote-exec" {
connection {
host= self.public_ip
user        = "ec2-user"
private_key = file("/logon.pem")
}
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}

以上假设其他一切都是正确的,例如存在密钥文件或安全组允许ssh连接。

相关内容

  • 没有找到相关文章

最新更新