【问题标题】:Terraform timeout when connecting to EC2 instance连接到 EC2 实例时的 Terraform 超时
【发布时间】:2020-07-17 21:08:53
【问题描述】:

我试图在 terraform 中运行它,一切正常,但是在创建实例(ubuntu)之后它无法连接,它只是超时。我已经多次生成私钥,但仍然出现错误:

.tf 文件

#####################################
#VARIABLES
#####################################


variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "private_key_path" {}
variable "key_name" {}
variable "region" {
default = "us-west-2"

}


#####################################
#PROVIDERS
#####################################

provider "aws" {

access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = var.region

}



#####################################
#DATA
#####################################


#data "aws_ami" "aws-linux" {
#most_recent = true
#owners = ["amazon"]

#filter {
#name = "name"
#values = ["amzn-ami-hvn*"]
#}

#filter {
#name = "root-device-type"
#values = ["ebs"]

#}

#filter {

#name = "virtualization-type"
#values = ["hvn"]
#}


#}


#####################################
#RESOURCES
#####################################

# this uses the dfault VPC. It will nor delete it on destroy.

resource "aws_default_vpc" "default" {


}

resource "aws_security_group" "allow_ssh" {

name = "nginx_demo2"
description = "allow ports for nginx demo"
vpc_id = aws_default_vpc.default.id



ingress {

from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {

from_port = 80
to_port = 80
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"]
}

}


# EC2 instance

resource "aws_instance" "nginx" {
#ami = data.aws_ami.aws-linux.id
ami = "ami-039d8ba38d6aff04b"
instance_type = "t2.micro"
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.allow_ssh.id]


#connection {
#type = "ssh"
#host = "self.public_ip"
#user = "ec2-user"
#private_key = file(var.private_key_path)
#}


connection {
type = "ssh"
#host = "self.public_ip"
host = "${self.public_ip}"
user = "ec2-user"
private_key = "${file(var.private_key_path)}"



}

provisioner "remote-exec" {





inline = ["sudo apt-get update", "sudo apt-get install nginx", "sudo service nginx start"]



#inline = ["yum install nginx -y", "systemctl start nginx"]
#command = "yum install nginx -y && service nginx start"



}

}


#####################################
#OUTPUT
#####################################

output "aws_instance_public_dns" {
value = aws_instance.nginx.public_dns
}

.tfvars

aws_access_key = "xxxxxxxxxxxxxx"
aws_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
key_name = "terra_test"
private_key_path = "C:\\Users\\user.name\\Documents\\Terraform\\Base\\web\\terra_test.pem"

错误:

ws_instance.nginx: Still creating... [5m30s elapsed]
aws_instance.nginx: Still creating... [5m40s elapsed]
aws_instance.nginx (remote-exec): Connecting to remote host via SSH...
aws_instance.nginx (remote-exec):   Host: 54.202.52.132
aws_instance.nginx (remote-exec):   User: ec2-user
aws_instance.nginx (remote-exec):   Password: false
aws_instance.nginx (remote-exec):   Private key: true
aws_instance.nginx (remote-exec):   Certificate: false
aws_instance.nginx (remote-exec):   SSH Agent: false
aws_instance.nginx (remote-exec):   Checking Host Key: false
aws_instance.nginx: Still creating... [5m50s elapsed]

Error: timeout - last error: SSH authentication failed (ec2-user@54.202.52.132:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

这里可能有什么问题? 我尝试将路径添加到 .pem 而不是使用变量,但得到了相同的错误。 此外,当我使用 host = "self.public_ip" 而不是 host = "${self.public_ip}" 时,它甚至没有检索公共 IP,所以这就是我使用 ${self.public_ip} 的原因。

Terraform v0.12.28

  • provider.aws v2.70.0

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    有两点需要改变:

    首先是用户名称。
    ubuntu amis 的用户通常是“ubuntu
    更改
    user = "ec2-user"

    user = "ubuntu"
    它将连接并开始安装 nginx。

    但是,您还需要更改
    inline = ["sudo apt-get update", "sudo apt-get install nginx", "sudo service nginx start"]
    to
    inline = ["sudo apt-get update -y", "sudo apt-get install nginx -y", "sudo service nginx start"]
    或者它会在提示 apt update 和 nginx install 时挂起

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2016-04-15
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 2020-03-13
      • 2012-06-08
      相关资源
      最近更新 更多