Unable to connect from my local system to ec2 instance created by terraform script



  • 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.



  • 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.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2