0
votes

I tried Creating Instance in AWS using Terraform and try to copy a set of files into the newly created AWS Instance. I used "provisioner" for the same but for the connection, it always says connection timed out.

In the example below I showed like its AWS Pem file but I tried with both ppk and pem files. nothing works.

provider "aws" {
    region = "ap-southeast-1"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}
resource "aws_instance" "firsttest" {
    ami = "ami-061eb2b23f9f8839c"
    instance_type = "t2.micro"
    key_name = "deepak"
    provisioner "file" {
        source      = "index.html"
        destination = "/home/ubuntu/index.html"
    connection {
        type     = "ssh"
        user     = "ubuntu"
        private_key = file("D:/awskeyterraform/deepak.pem")
        host = "${aws_instance.firsttest.public_ip}"
        }
    }
    user_data = <<-EOF
        #!/bin/bash
        apt-get update -y
        apt-get install -y nginx
        systemctl enable nginx
        service nginx restart
        touch index.html
        EOF
    tags = {
        name = "terraform-firsttest"
    }
}

Expected should copy the index.html but actual the connection timed out to connect to the newly created instance

1
You don't have any security groups defined (AWS take on a stateful firewall), so nothing can go in or out of the instance (including SSH). Since this is a super important aspect of AWS, I would drop terraform for now and try to create instances using the console to understand the basics of EC2, and then jump on terraform. Otherwise you'll be fighting 2 big battles. - Augusto
I have modified the default security group to any: any and it is working fine when I am executing the same from Linux but making problems only in windows. I could understand that Linux by default has SSH agent but not in windows!!! just curious how to handle such a situation without installing any ssh-agent in windows. - DeenaDeepak
@DeenaDeepak you should be able to use the WinRM provisioner - Scott Heath
@ScottHeath, thanks for the suggestion. WinRM Provisioner is for Windows VM but I managed to make it work by renaming the "deepak.pem" to "id_rsa". It works as expected. - DeenaDeepak

1 Answers

0
votes

In Windows, SSH module Connection doesn't accept "*.pem". Instead, it accepts the PEM file after renaming it to "id_rsa".

provider "aws" {
    region = "ap-southeast-1"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}
resource "aws_instance" "firsttest" {
    ami = "ami-061eb2b23f9f8839c"
    instance_type = "t2.micro"
    key_name = "deepak"
    provisioner "file" {
        source      = "index.html"
        destination = "/home/ubuntu/index.html"
    connection {
        type     = "ssh"
        user     = "ubuntu"
        private_key = "${file("D:/awskeyterraform/id_rsa")}"
        host = "${aws_instance.firsttest.public_ip}"
        }
    }
    user_data = <<-EOF
        #!/bin/bash
        apt-get update -y
        apt-get install -y nginx
        systemctl enable nginx
        service nginx restart
        touch index.html
        EOF
    tags = {
        name = "terraform-firsttest"
    }
}

Hope this should solve the issue.