Reputation: 75
Im trying to run this in terraform, everythig goes fine but after creating the instance(ubuntu) it is failing to connect, it just times out. I have generated the private key several times but I'm still getting the error:
.tf file
#####################################
#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"
Error:
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 ([email protected]:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
What could be wrong here? I have tried adding the path to the .pem instead of using the variable but got the same error. Also when I use host = "self.public_ip" instead of host = "${self.public_ip}" it is not even retreiving the public IP so thats why Im using ${self.public_ip}.
Terraform v0.12.28
Upvotes: 0
Views: 1517
Reputation: 574
There are two things that need changing:
First, the user name.
The user for ubuntu amis is normally "ubuntu"
Change
user = "ec2-user"
to
user = "ubuntu"
and it will connect and start to install nginx.
But, you need to also change
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"]
or it will hang at the prompt for both the apt update and the nginx install
Upvotes: 1