简体   繁体   中英

Unable to connect to terraform created AWS instance via ssh

I am trying to use terraform to spin up a VPC and single instance and then connect via ssh but I'm unable to. I'm aware I don't have any keys here but I'm trying to simply connect via the web terminal and it still says

There was a problem setting up the instance connection The connection has been closed because the server is taking too long to respond. This is usually caused by network problems, such as a spotty wireless signal, or slow network speeds. Please check your network connection and try again or contact your system administrator.

Is anyone able to look at my code and see what I'm doing wrong?

provider "aws" {
    region = "us-east-2"
}

resource "aws_vpc" "vpc" {
    cidr_block = "10.0.0.0/16"
    enable_dns_hostnames = true
    enable_dns_support = true
    tags = {
        Name = "test"
    }
}

resource "aws_internet_gateway" "gateway" {
    vpc_id = "${aws_vpc.vpc.id}"

    tags = {
        Name = "test"
    }
}

resource "aws_subnet" "subnet" {
    vpc_id = "${aws_vpc.vpc.id}"
    cidr_block = "${aws_vpc.vpc.cidr_block}"
    availability_zone = "us-east-2a"
    map_public_ip_on_launch = true

    tags = {
        Name = "test"
    }
}

resource "aws_route_table" "table" {
    vpc_id = "${aws_vpc.vpc.id}"

    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = "${aws_internet_gateway.gateway.id}"
    }

    tags = {
        Name = "test"
    }

}

resource "aws_route_table_association" "public" {
    subnet_id = "${aws_subnet.subnet.id}"
    route_table_id = "${aws_route_table.table.id}"
}

resource "aws_instance" "node" {
    #ami = "ami-0d5d9d301c853a04a" # Ubuntu 18.04
    ami = "ami-0d03add87774b12c5" # Ubuntu 16.04
    instance_type = "t2.micro"
    subnet_id = "${aws_subnet.subnet.id}"
}

UPDATE1 : I've added key_name = "mykey" which I have previously created. I am unable to ping the public ip and upon trying to ssh with the key I get the following:

$ ssh -v -i ~/.ssh/mykey ubuntu@1.2.3.4
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to 1.2.3.4 [1.2.3.4] port 22.

where mykey and 1.2.3.4 have been changed for posting.

UPDATE2 : Looking at the security group I don't see anything which stands out. The ACL for this has the following:

Rule # Type          Protocol Port Range Source      Allow / Deny
100    ALL Traffic   ALL      ALL        0.0.0.0/0   ALLOW
*      ALL Traffic   ALL      ALL        0.0.0.0/0   DENY

Is this a problem? It seems that no one sees an issue with the terraform code so if anyone can confirm this is not a problem with the code then I think this can be closed out and moved to a different board since it would not be a code issue, correct?

The web console uses SSH to connect, so you still need to setup an SSH key. The only way to connect without an SSH key configured, and port 22 open in the Security Group, is to use AWS Systems Manager Session Manager, but that requires the SSM agent running on the EC2 instance and appropriate IAM roles assigned to the instance.

You have not supplied a key_name to indicate which SSH keypair to use.

If you don't have an existing aws_key_pair then you will also need to create one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM