Timed out error connecting to EC2 instance through terraform using private key

1,496 views
Skip to first unread message

first.sub...@gmail.com

unread,
Mar 12, 2019, 7:40:33 PM3/12/19
to Terraform
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  -
------------------------------------------------------------------
module.tf  file code -

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.



Lowe Schmidt

unread,
Mar 13, 2019, 5:03:47 AM3/13/19
to terrafo...@googlegroups.com
Does your security group allow ssh in ?
--
Lowe Schmidt | +46 723 867 157


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/d2e5a54e-3d04-4114-9525-72fe8d9e1658%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pavel Voinov

unread,
Mar 13, 2019, 6:36:47 PM3/13/19
to Terraform
Hi,

If SG is ok, consider increasing https://www.terraform.io/docs/provisioners/connection.html#timeout value. In some cases, instances can boot slower than "default" 5 minutes (e.g. some services or cloud-init scripts hit timeouts if they connect to remote URLs).
Try to test this manually launching EC2 from the AMI in AWS Console and check the time when SSH become available from the host where you run terraform commands.

Good luck,
Pavel

first.sub...@gmail.com

unread,
Mar 14, 2019, 5:19:07 PM3/14/19
to Terraform
Yes, I am able to login and do ssh on the instances created with this key in AWS console. 
Its only the instances created using terraform that are throwing time out creating connection to instances using commands in the script and also when tried to manually ssh into these instances

first.sub...@gmail.com

unread,
Mar 14, 2019, 5:25:20 PM3/14/19
to Terraform
tried the same manually using console and ssh becomes available as soon as the instance gets to running state and status checks are finished.
Also, once the terraform script failed, I have waited for a while and tried to ssh into that instance manually and observed the same timed out issue connecting to this instance through putty

Bill Anderson

unread,
Mar 15, 2019, 12:55:12 AM3/15/19
to Terraform
The error has nothing to do with your ssh keys. A timeout error happens before the login actually begins.

Bill Anderson

unread,
Mar 15, 2019, 1:01:18 AM3/15/19
to Terraform
Ugh wrong button.

Anyway what you’ll want to do is look at the differences between a working and non-working instance in the console. It will be something to do with the networking, be it security group, VPC access, or even subnet routing or settings. The key pair is irrelevant until you can actually make a connection and you’ll need to find what is preventing that from happening.

Unfortunately there isn’t enough detail here to tell you just what it is. I’d recommend trying the EC2 troubleshooting docs as that will be more focused on the problem.

laxman Singh Rathore

unread,
Mar 15, 2019, 8:07:46 AM3/15/19
to terrafo...@googlegroups.com
Hi, 

Check output below code actually you have not mentioned type in your code.

connection {
    type = "ssh"
    user = "root"
    private_key = "${file("~/.ssh/id_rsa")}"
  }

  provisioner "remote-exec" {
    inline = [
    ]
  }

Include line type ="ssh"

Than again executed the terraform plan and apply 

Regards
Laxman  singh 
Sr. DevOps engineer 



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages