I'm creating an aws_instance resource and run a provisioner but the SSH connection is never successful.
Here is my resource code:
resource "aws_instance" "pos" {
ami = "ami-c58c1dd3"
instance_type = "m4.xlarge"
subnet_id = "${var.subnet_id_1}"
key_name = "${var.key_name}"
connection {
type = "ssh"
user = "ec2-user"
private_key = "${file(var.private_key_path)}"
host = "aws_instance.instance.private_ip"
}
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}
During creation I get the following output repeated over and over:
aws_instance.pos (remote-exec): Connecting to remote host via SSH...
aws_instance.pos (remote-exec): Host: aws_instance.insance.private_ip
aws_instance.pos (remote-exec): User: ec2-user
aws_instance.pos (remote-exec): Password: false
aws_instance.pos (remote-exec): Private key: true
aws_instance.pos (remote-exec): Certificate: false
aws_instance.pos (remote-exec): SSH Agent: false
aws_instance.pos (remote-exec): Checking Host Key: false
aws_instance.pos: Still creating... [40s elapsed]
The SSH connection is never successful and eventually I must kill the command. However the EC2 instance is successfully created and I can SSH into from my local machine using the private key (PEM file).
I've also tried using self.public_ip in the host field, but that produced the same result. How can I connect to the EC2 instance and provision it during creation?