我想使用terraform脚本来创建 ec2 实例。我还希望能够使用SSH和在 AWS 控制台中创建的密钥对连接到计算机。
这是脚本的相关部分:
resource "aws_instance" "web" {
instance_type = "t2.micro"
ami = "${lookup(var.aws_amis, var.aws_region)}"
key_name = "${var.key_name}" # this is my keypair
connection {
type = "ssh"
user = "${var.ssh_user}"
}
vpc_security_group_ids = ["${aws_security_group.default.id}"]
subnet_id = "${aws_subnet.default.id}"
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update"
]
}
}
然后,在5分钟后获得超时,表明terraform无法连接到 ec2 实例
aws_instance.web: Still creating... (5m20s elapsed)
aws_instance.web (remote-exec): Connecting to remote host via SSH...
aws_instance.web (remote-exec): Host: 34.250.66.198
aws_instance.web (remote-exec): User: ubuntu
aws_instance.web (remote-exec): Password: false
aws_instance.web (remote-exec): Private key: false
aws_instance.web (remote-exec): SSH Agent: true
Error applying plan:
1 error(s) occurred:
* aws_instance.web: 1 error(s) occurred:
* timeout
但是,即使有超时的terraform文件
ssh -i "aws.pem" ubuntu@instance_ip
此信息可能相关: * terraform版本:v0.10.7 *地区:欧 - 西1 *操作系统:El Capitan 10.11.6
以防这是我如何映射amis
variable "aws_amis" {
default = {
eu-west-1 = "ami-674cbc1e"
us-east-1 = "ami-1d4e7a66"
us-west-1 = "ami-969ab1f6"
us-west-2 = "ami-8803e0f0"
}
}
非常感谢你的帮助
答案 0 :(得分:0)
您需要在connection中指定私钥,如下所示:
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update"
]
connection {
private_key = "${file("/location/of/the/pem/file")}"
}
}