连接到EC2实例时Terraform超时



我试图在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,而不是使用变量,但出现了相同的错误。同样当我使用主机="主机"时;self.public_ip";而不是主机="${self.public_ip}";它甚至没有检索公共IP,所以这就是为什么我使用${self.public_IP}.

Terraform 0.12.28版

  • provider.aws v2.70.0

有两件事需要更改:

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

用户="ubuntu">
,它将连接并开始安装nginx。

但是,您还需要将
inline=[quot;sudoapt-get-update",quot;SUDOapt-get-installnginx"quot:sudoservicenginxstart"]
更改为
>inline=[quot;Sudoapt-geet-update-y",quot!sudoapt-get-installnginx-y&&quot&quot

相关内容

  • 没有找到相关文章

最新更新