为什么 Terraform 无法通过 SSH 连接到我的 EC2 实例?



我正在尝试使用 terraform ssh 到新创建的 EC2 实例中。我的主机是Windows 10,我从主机使用Bitvise SSH客户端SSH进入实例没有问题,但是Terraform似乎无法通过SSH在实例上创建目录:

我的main.tf

provider "aws" {
region = "us-west-2"
}
resource "aws_security_group" "instance" { 
name = "inlets-server-instance" 
description = "Security group for the inlets server" 
ingress { 
from_port = 22 
to_port = 22 
protocol = "tcp" 
cidr_blocks = ["0.0.0.0/0"] 
} 
egress { 
from_port = 0 
to_port = 0 
protocol = "-1" 
cidr_blocks = ["0.0.0.0/0"] 
} 
}
resource "aws_instance" "tunnel" {
ami = "ami-07b4f3c02c7f83d59"
instance_type = "t2.nano"
key_name = "${var.key_name}"
vpc_security_group_ids = [aws_security_group.instance.id]
tags = {
Name = "inlets-server"
}
provisioner "local-exec" {
command = "echo ${aws_instance.tunnel.public_ip} > ${var.public_ip_path}"
}
provisioner "remote-exec" {
inline = [
"mkdir /home/${var.ssh_user}/ansible",
]
connection {
type        = "ssh"
host        = "${file("${var.public_ip_path}")}"
user        = "${var.ssh_user}"
private_key = "${file("${var.private_key_path}")}"
timeout     = "1m"
agent       = false
}
}
}

我的variables.tf

variable "key_name" {
description = "Name of the SSH key pair generated in Amazon EC2."
default     = "aws_ssh_key"
}
variable "public_ip_path" {
description = "Path to the file that contains the instance's public IP address"
default     = "ip_address.txt"
}
variable "private_key_path" {
description = "Path to the private SSH key, used to access the instance."
default     = "aws_ssh_key.pem"
}
variable "ssh_user" {
description = "SSH user name to connect to your instance."
default     = "ubuntu"
}

我得到的只是尝试的连接:

aws_instance.tunnel (remote-exec): Connecting to remote host via SSH...
aws_instance.tunnel (remote-exec):   Host: XX.XXX.XXX.XXX
aws_instance.tunnel (remote-exec):   User: ubuntu
aws_instance.tunnel (remote-exec):   Password: false
aws_instance.tunnel (remote-exec):   Private key: true
aws_instance.tunnel (remote-exec):   Certificate: false
aws_instance.tunnel (remote-exec):   SSH Agent: false
aws_instance.tunnel (remote-exec):   Checking Host Key: false

它最终超时,并显示:

Error: timeout - last error: dial tcp: lookup XX.XXX.XXX.XXX
: no such host

有什么想法吗?

你没有谈论你的网络结构。

您的 win10 机器是否在 VPC 内?如果没有,您是否正确设置了互联网网关、路由表、NAT 网关?

创建弹性 IP 资源以访问具有地形知识的计算机的 IP 地址,而不是尝试从计算机获取 IP 地址,会更干净、更安全。当然,本地可执行文件会比远程可执行文件更快,但您创建了一个可能产生问题的隐式依赖项。

相关内容

  • 没有找到相关文章

最新更新