user5820327
user5820327

Reputation: 241

Connection time out when using Terraform

I tried to create instance from a subnet and vpc id but am having issue with the provision remote exec.The purpose of this is to create 2 public subnets(eu-west-1a) and 2 private subnets(eu-west-1b) and use the subnet and vpc id in it to create an instance and then ssh and install nginx. I am not sure how to resolve this and unfortunately am not expert in Terraform so guidance is needed here. When I tried to also ssh it using the command prompt, it is saying connection timed out. The port is open in security group port 22

╷ │

 Error: remote-exec provisioner error
│ 
│   with aws_instance.EC2InstanceCreate,
│   on main_ec2.tf line 11, in resource "aws_instance" "EC2InstanceCreate":
│   11:   provisioner "remote-exec" {
│
│ timeout - last error: dial tcp 54.154.137.10:22: i/o timeout

[1enter image description here

My code below :

 `# Server Definition
resource "aws_instance" "EC2InstanceCreate" {
  ami                    = "${var.aws_ami}"
  instance_type          = "${var.server_type}"
  key_name               = "${var.target_keypairs}"
  subnet_id              = "${var.target_subnet}"

 
  provisioner "remote-exec" { 
  connection {
      type    = "ssh"
       host = "${self.public_ip}"
      user    = "centos"
      private_key = "${file("/home/michael/cs-104-michael/lesson6/EC2Tutorial.pem")}"
    timeout     = "5m"
    } 
  
  inline = [
  "sudo yum -y update",
  "sudo yum -y install nginx",
  "sudo service nginx start",
  "sudo yum -y install wget, unzip",
  
  ]

  
  }

  
  tags = {
    Name        = "cs-104-lesson6-michael"
    Environment = "TEST"
    App         = "React App"
  }
}

output "pub_ip" {
  value      = ["${aws_instance.EC2InstanceCreate.public_ip}"]
  depends_on = [aws_instance.EC2InstanceCreate]
}`

security group config :

# Create security group for webserver
resource "aws_security_group" "webserver_sg" {
  name        = "sg_ws_name"
  vpc_id      = "${var.target_vpc}"
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    description = "HTTP"
    cidr_blocks = ["0.0.0.0/0"]
   }
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    description = "HTTP"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }
  tags = {
    Name = "Security Group VPC devmind" 
    Project = "demo-assignment"
  }
}

subnet code :

resource "aws_subnet" "public-subnet" {
 vpc_id      = "${aws_vpc.default.id}"
  cidr_block  = "${var.public_subnet_2a_cidr}"
  availability_zone = "eu-west-1a"
map_public_ip_on_launch = true
tags = {

Name = "Web Public subnet 1"

}

}

resource "aws_subnet" "public-subnet2" {      
 vpc_id      = "${aws_vpc.default.id}"        
  cidr_block  = "${var.public_subnet_2b_cidr}"
  availability_zone = "eu-west-1a"
map_public_ip_on_launch = true
tags = {

Name = "Web Public subnet 2"

}

}

# Define private subnets

resource "aws_subnet" "private-subnet" {      
 vpc_id      = "${aws_vpc.default.id}"
  cidr_block  = "${var.private_db_subnet_2a_cidr}"
  availability_zone = "eu-west-1b"
map_public_ip_on_launch = false
tags = {

Name = "App Private subnet 1"

}

}

resource "aws_subnet" "private-subnet2" {
 vpc_id      = "${aws_vpc.default.id}"
  cidr_block  = "${var.private_db_subnet_2b_cidr}"
  availability_zone = "eu-west-1b"
map_public_ip_on_launch = false
tags = {

Name = "App Private subnet 2"

}

}

vpc code :

# Define our VPC        
resource "aws_vpc" "default" {
  cidr_block  = "${var.vpc_cidr}"

  enable_dns_hostnames = true

tags = {

Name = "Devops POC VPC"

}

}  

Internet gateway included code :

# Internet Gateway      
resource "aws_internet_gateway" "gw" {
  vpc_id      = "${aws_vpc.default.id}"

tags = {

name = "VPC IGW"

}

} 

Upvotes: 1

Views: 1776

Answers (1)

Marcin
Marcin

Reputation: 238081

You are not providing vpc_security_group_ids for your instance:

vpc_security_group_ids = [aws_security_group.webserver_sg.id]

There could be many other issues, such as incorrectly setup VPC which is not shown.

Upvotes: 1

Related Questions