问题描述
无法从Terraform连接到ec2实例。如果我手动创建ec2实例(而不是通过terraform),则相同的密钥对有效。确认我的密钥对是正确的。这是我正在尝试执行的代码。我得到的错误是:`aws_instance.ec2_test_instance:使用'remote-exec'进行配置...
错误:无法读取ssh私钥:未找到密钥
错误:错误导入KeyPair:MissingParameter:请求必须包含参数PublicKeyMaterial 状态码:400,请求ID:`
resource "aws_instance" "ec2_test_instance" {
ami = var.instance_test_ami
instance_type = var.instance_type
subnet_id = var.aws_subnet_id
key_name = aws_key_pair.deployer.key_name
tags = {
Name = var.environment_tag
}
connection {
type = "ssh"
host = self.public_ip
user = "centos"
private_key = "file(path.root/my-key)"
}
provisioner "remote-exec" {
inline = [
"sudo yum -y install wget,unzip","sudo yum -y install java-1.8.0-openjdk",]
}
解决方法
由于您使用的是terraform的较新版本,您很可能不需要""
引号,因为内置文件功能已经返回了字符串,例如:
private_key = file("${path.module}/my-key")
在文档中,该示例在参数字段中的实际文件路径周围显示双引号: https://www.terraform.io/docs/configuration/functions/file.html