0

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.

  • 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 May 23 '22 at 14:57
  • Has your terraform instance been assigned an IP address? Does its security group(s) allow SSH access? – Dale C. Anderson May 26 '22 at 02:17
  • The default route was missing in the routing table, which was why I was unable to ssh into the ec2 instance. – Vizeet Srivastava Jun 03 '22 at 07:02

1 Answers1

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.