连接到EC2实例时Terraform超时
我试图在terraform中运行这个,一切都很好,但是在创建了实例(ubuntu)之后,它无法连接,它只是超时了。我已多次生成私钥,但仍会出现错误: .tf文件连接到EC2实例时Terraform超时,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,我试图在terraform中运行这个,一切都很好,但是在创建了实例(ubuntu)之后,它无法连接,它只是超时了。我已多次生成私钥,但仍会出现错误: .tf文件 ##################################### #VARIABLES ##################################### variable "aws_access_key" {} variable "aws_secret_key" {} va
#####################################
#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\u ip”而不是host=“${self.public\u ip}”时,它甚至没有检索公共ip,因此我使用${self.public\u ip}
地形v0.12.28
- provider.aws v2.70.0
有两件事需要改变:
首先是用户名称。
ubuntu amis的用户通常是“ubuntu”
更改
user=“ec2用户”
到
user=“ubuntu”
它将连接并开始安装nginx 但是,您还需要更改
inline=[“sudo-apt-get-update”、“sudo-apt-get-install-nginx”、“sudo-service-nginx-start”]
到
inline=[“sudo-apt-get-update-y”、“sudo-apt-get-install-nginx-y”、“sudo-service-nginx-start”]
或者在apt更新和nginx安装的提示下,它将挂起