为什么Terraform SSH无法进入我的EC2实例?

时间:2019-07-10 19:22:29

标签: amazon-web-services amazon-ec2 ssh terraform terraform-provider-aws

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

我的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

它最终超时:

Blurry.with(context).radius(20).sampling(2).onto((ViewGroup)linear);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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

创建弹性IP资源以使用Terraform知识访问计算机的IP地址,而不是尝试从计算机中获取它,会更安全,更安全。当然,本地执行程序将比远程执行程序快,但是您创建了一个隐式依赖关系,这可能会产生问题。