我在运行地形计划时收到如下错误消息
Error: Error in function call
on instance.tf line 3, in resource "aws_key_pair" "mykey":
3: public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
|----------------
| var.PATH_TO_PUBLIC_KEY is "mykey.pub"
Call to function "file" failed: no file exists at mykey.pub.
在文件系统上,我有以下两个文件
/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pem
/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pub
我的地形代码如下main.tf
resource "aws_key_pair" "mykey" {
key_name = "mykey"
public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
}
resource "aws_instance" "example" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.mykey.key_name}"
provisioner "file" {
source = "script.sh"
destination = "/tmp/script.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/script.sh",
"sudo /tmp/script.sh"
]
}
connection {
host = "${self.public_ip}"
user = "${var.INSTANCE_USERNAME}"
private_key = "${file("${var.PATH_TO_PRIVATE_KEY}")}"
}
}
vars.tf
variable "AWS_ACCESS_KEY" {}
variable "AWS_SECRET_KEY" {}
variable "AWS_REGION" {
default = "ap-southeast-2"
}
variable "AMIS" {
type = "map"
default = {
ap-southeast-2 = "ami-039bb4c3a7946ce19"
ap-southeast-2 = "ami-039bb4c3a7946ce19"
ap-southeast-1 = "ami-05c6d22d98f97471c"
}
}
/*
variable "PATH_TO_PRIVATE_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pem"
}
variable "PATH_TO_PUBLIC_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pub"
}
*/
variable "PATH_TO_PRIVATE_KEY" {
default = "mykey"
}
variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub"
}
variable "INSTANCE_USERNAME" {
default = "ubuntu"
}
在我的 AWS 账户中,我有私有密钥,并且已下载名为 Ravi-TF-Work.pem 的公共密钥我通过以下命令将 *.pem 转换为 *.pub:
ssh-keygen -y -f private_key1.pem > public_something_else.pub
下面的地形版本
Terraform v0.12.5
+ provider.aws v2.20.0
在这个阶段,我对我的AWS账户中的私有密钥和我下载的公有密钥(pem文件,转换为pub文件(以及如何使其工作感到困惑。
任何领导将不胜感激。
您似乎已经注释掉了实际密钥的位置:
/*
variable "PATH_TO_PRIVATE_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pem"
}
variable "PATH_TO_PUBLIC_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pub"
}
*/
variable "PATH_TO_PRIVATE_KEY" {
default = "mykey"
}
variable "PATH_TO_PUBLIC_KEY" {
default = "mykey.pub"
}
取消注释并删除虚拟定义:
variable "PATH_TO_PRIVATE_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pem"
}
variable "PATH_TO_PUBLIC_KEY" {
default = "/home/ec2-user/ravi-tf-work-3/Ravi-TF-Work.pub"
}