hi,
I am trying to connect to my EC2 instance using terraform but observing the below error -
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
aws_instance.nginx (remote-exec): Connecting to remote host via SSH...
aws_instance.nginx (remote-exec): Host: XX.XX.XXX.XXX
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): SSH Agent: false
aws_instance.nginx (remote-exec): Checking Host Key: false
aws_instance.nginx: Still creating... (35m18s elapsed)
-------------------------------------------------------
Network timed out exception is being observed when connecting to the same instance with this key file from putty through the instance IPaddress, but if i create an EC2 instance manually in AWS console I am able to connect to it through putty using the same file terraform.pem which has been converted to terraform.ppk.
Below is terraform code being used to connect to EC2 instance -
------------------------------------------------------------------
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "private_key_path" {}
variable "key_name" {
default = "terraformkey" #name of my key pair file- terraformkey.pem
}
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-1"
}
resource "aws_instance" "nginx" {
ami = "ami-c58c1dd3"
instance_type = "t2.micro"
key_name = "${var.key_name}"
connection {
user = "ec2-user"
private_key = "${file(var.private_key_path)}"
}
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}
output "aws_instance_public_dns" {
value = "${aws_instance.nginx.public_dns}"
}
---------------------------------------------------------------------------------.tfvars code--------------------------------------------------------------------------
aws_access_key = "XXXXX"
aws_secret_key = "XXXXXX"
private_key_path = "C:\\tf\\terraformkey.pem"
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Is there a problem with the way I am using my keypair while creating EC2 instance?
Any help would be appreciated
Thanks in advance.