<p>The issue was that the default route was missing in the routing table.</p>
<pre><code>resource &quot;aws_route&quot; &quot;update&quot; {
    provider               = aws.docdb_peer
    route_table_id         = &quot;${aws_vpc.docdb_peer.default_route_table_id}&quot;
    destination_cidr_block = &quot;0.0.0.0/0&quot;
    gateway_id             = &quot;${aws_internet_gateway.gw_connect.id}&quot;
}
</code></pre>
<p>Adding this solved the issue.</p>

CC BY-SA 4.0
2022-06-03T07:01:22.617Z
2022-06-03T07:01:22.617Z
Vizeet Srivastava
<p>Following is the source code:</p>
<pre><code>variable &quot;ec2_instance_type_name&quot; {
    type    = string
    default = &quot;t2.nano&quot;
}

terraform {
    required_providers {
        aws = {
            source = &quot;hashicorp/aws&quot;
            version = &quot;~&gt; 3.27&quot;
        }
    }
}

provider &quot;aws&quot; {
    alias   = &quot;us&quot;
    region  = &quot;us-east-1&quot;
}

provider &quot;aws&quot; {
    alias   = &quot;eu&quot;
    region  = &quot;eu-west-1&quot;
}

data &quot;aws_ami&quot; &quot;amazon_2&quot; {
    provider = aws.eu
    most_recent = true

    filter { 
        name = &quot;name&quot;
        values = [&quot;amzn2-ami-kernel-*-hvm-*-x86_64-gp2&quot;]
    } 
    owners = [&quot;amazon&quot;]
}

data &quot;http&quot; &quot;myip&quot; {
    url = &quot;http://ipv4.icanhazip.com&quot;
}

resource &quot;aws_vpc&quot; &quot;docdb_peer&quot; {
    provider                = aws.eu
    cidr_block              = &quot;172.32.0.0/16&quot;
    enable_dns_support      = true
    enable_dns_hostnames    = true
}

resource &quot;aws_internet_gateway&quot; &quot;gw_connect&quot; {
    provider = aws.eu
    vpc_id = aws_vpc.docdb_peer.id
}

resource &quot;aws_security_group&quot; &quot;vpc_sg&quot; {
    provider = aws.eu
    vpc_id       = aws_vpc.docdb_peer.id
    name         = &quot;vpc-connect&quot;
    description  = &quot;VPC Connect&quot;
  
    ingress {
        cidr_blocks = [&quot;${chomp(data.http.myip.body)}/32&quot;]
        from_port   = 22
        to_port     = 22
        protocol    = &quot;tcp&quot;
    } 

    egress {
        from_port   = 0
        to_port     = 0
        protocol    = &quot;-1&quot;
        cidr_blocks = [&quot;0.0.0.0/0&quot;]
    }
}

resource &quot;aws_subnet&quot; &quot;main&quot; {
    provider = aws.eu
    vpc_id                    = aws_vpc.docdb_peer.id
    availability_zone         = &quot;eu-west-1a&quot;
    cidr_block                = &quot;172.32.0.0/20&quot;
    map_public_ip_on_launch   = true
}

resource &quot;aws_instance&quot; &quot;tunnel-ec2&quot; { 
    provider = aws.eu
    vpc_security_group_ids = [&quot;${aws_security_group.vpc_sg.id}&quot;]
    subnet_id     = aws_subnet.main.id
    ami           = data.aws_ami.amazon_2.id
    instance_type = var.ec2_instance_type_name
    key_name      = &quot;ireland_ofc_new&quot;
    depends_on = [aws_internet_gateway.gw_connect]
}
</code></pre>
<p>I try to ssh into the system using the key pair pem file and it just timeout.
My other ec2 instance which I manually created works just fine.
Please help resolve the issue.</p>

CC BY-SA 4.0
2022-05-23T14:57:52.707Z
Without seeing how your other instances are configured we can't tell how this differs.  I'll point out that 172.32/20 is a public address space and probably why you can't connect.
kenlukas
CC BY-SA 4.0
2022-05-26T02:17:03.383Z
Has your terraform instance been assigned an IP address? Does its security group(s) allow SSH access?
Dale C. Anderson
CC BY-SA 4.0
2022-06-03T07:02:55.433Z
The default route was missing in the routing table, which was why I was unable to ssh into the ec2 instance.
Vizeet Srivastava
<terraform><amazon-ec2><aws-vpc><aws-security-group><aws-subnets>
Unable to connect from my local system to ec2 instance created by terraform script
Vizeet Srivastava
# Unable to connect from my local system to ec2 instance created by terraform script

(Asked by: username_0 on 2022-05-21)

Following is the source code:
variable "ec2_instance_type_name" {
    type    = string
    default = "t2.nano"
}

terraform {
    required_providers {
        aws = {
            source = "hashicorp/aws"
            version = "~> 3.27"
        }
    }
}

provider "aws" {
    alias   = "us"
    region  = "us-east-1"
}

provider "aws" {
    alias   = "eu"
    region  = "eu-west-1"
}

data "aws_ami" "amazon_2" {
    provider = aws.eu
    most_recent = true

    filter { 
        name = "name"
        values = ["amzn2-ami-kernel-*-hvm-*-x86_64-gp2"]
    } 
    owners = ["amazon"]
}

data "http" "myip" {
    url = "http://ipv4.icanhazip.com"
}

resource "aws_vpc" "docdb_peer" {
    provider                = aws.eu
    cidr_block              = "172.32.0.0/16"
    enable_dns_support      = true
    enable_dns_hostnames    = true
}

resource "aws_internet_gateway" "gw_connect" {
    provider = aws.eu
    vpc_id = aws_vpc.docdb_peer.id
}

resource "aws_security_group" "vpc_sg" {
    provider = aws.eu
    vpc_id       = aws_vpc.docdb_peer.id
    name         = "vpc-connect"
    description  = "VPC Connect"
  
    ingress {
        cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
    } 

    egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_subnet" "main" {
    provider = aws.eu
    vpc_id                    = aws_vpc.docdb_peer.id
    availability_zone         = "eu-west-1a"
    cidr_block                = "172.32.0.0/20"
    map_public_ip_on_launch   = true
}

resource "aws_instance" "tunnel-ec2" { 
    provider = aws.eu
    vpc_security_group_ids = ["${aws_security_group.vpc_sg.id}"]
    subnet_id     = aws_subnet.main.id
    ami           = data.aws_ami.amazon_2.id
    instance_type = var.ec2_instance_type_name
    key_name      = "ireland_ofc_new"
    depends_on = [aws_internet_gateway.gw_connect]
}

I try to ssh into the system using the key pair pem file and it just timeout.
My other ec2 instance which I manually created works just fine.
Please help resolve the issue.

**Tags:** terraform, amazon-ec2, aws-vpc, aws-security-group, aws-subnets

> **Comments:**
> **username_1**: Without seeing how your other instances are configured we can't tell how this differs.  I'll point out that 172.32/20 is a public address space and probably why you can't connect.  
> **username_2**: Has your terraform instance been assigned an IP address? Does its security group(s) allow SSH access?  
> **username_0**: The default route was missing in the routing table, which was why I was unable to ssh into the ec2 instance.  

---

## 1 answers

### Answer by username_0 on 2022-06-03. Score: 0

The issue was that the default route was missing in the routing table.
resource "aws_route" "update" {
    provider               = aws.docdb_peer
    route_table_id         = "${aws_vpc.docdb_peer.default_route_table_id}"
    destination_cidr_block = "0.0.0.0/0"
    gateway_id             = "${aws_internet_gateway.gw_connect.id}"
}

Adding this solved the issue.


---

