-3

I am trying to replicate an aws demo via terraform which I previously did manually within the aws console and it worked fine. I have tried to compare it with code that I have deployed previously where the url also loaded and it seems to match but I am very new to this and simply trying to learn TF.` Once deployed via TF, I am able to use the connect button within the console and it seems to connect just fine but when I ping or enter in a url http://followed by public ip address it says:


Host Not Found DNS error (the host name of the page you are looking for does not exist) or Server did not accept the connection.***

   provider "aws" {
   region = "us-east-1"
   access_key = 
   secret_key =

   }

     resource "aws_vpc" "vpclab2" {
     cidr_block = "10.0.0.0/16"
     enable_dns_hostnames = "true"

    tags = {
    name = "vpclab2"
      }
    }

   resource "aws_subnet" "pubsub1" {
   vpc_id = aws_vpc.vpclab2.id
   cidr_block = "10.0.0.0/24"
   availability_zone = "us-east-1b"
   map_public_ip_on_launch = true

   tags = {
   "name" = "pubsub1b"
     }


  }

    resource "aws_subnet" "pubsub2" {
    vpc_id = aws_vpc.vpclab2.id
    cidr_block = "10.0.1.0/24"
    availability_zone = "us-east-1c"
    map_public_ip_on_launch = true

    tags = {
    "name" = "pubsub1c"
      }
    }

    resource "aws_internet_gateway" "iglab2" {
    vpc_id = aws_vpc.vpclab2.id

    tags = {
    "name" = "ig"
     }

   }

   resource "aws_route_table" "rtpubsub1" {
   vpc_id = aws_vpc.vpclab2.id

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

  }

   resource "aws_route_table_association" "rtsub1" {
   subnet_id = aws_subnet.pubsub1.id
   route_table_id = aws_route_table.rtpubsub1.id

  }


   resource "aws_route_table" "rtpubsub2" {
   vpc_id = aws_vpc.vpclab2.id

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

  }

   resource "aws_route_table_association" "RT-sub2" {
   subnet_id = aws_subnet.pubsub2.id
   route_table_id = aws_route_table.rtpubsub2.id

  }

  resource "aws_security_group" "sg" {
  name = "allow http"
  vpc_id = aws_vpc.vpclab2.id

  ingress {
    description = "allow http"
    from_port = "80"
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

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

   egress  {
   cidr_blocks =  ["0.0.0.0/0"]
   description = "all outbound traf"
   from_port = 0
   protocol = "-1"
   to_port = 0

  }

 }

  resource "aws_instance" "ec2ins" {
  ami = "ami-02f3f602d23f1659d"
  instance_type = "t2.micro"

  subnet_id = aws_subnet.pubsub1.id
  availability_zone = "us-east-1b"
  associate_public_ip_address = "true"
  vpc_security_group_ids = [aws_security_group.sg.id]


  user_data  = <<-EOF
 #!/bin/bash
  yum update
  yum -y install httpd
  chkconfig httpd on 
  service httpd start
  EOF

}


  provider "aws" {
  region = "us-east-1"
  access_key = 
  secret_key = 
  }

  resource "aws_vpc" "vpclab2" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = "true"

  tags = {
  name = "vpclab2"
  }


  resource "aws_subnet" "pubsub1" {
  vpc_id = aws_vpc.vpclab2.id
  cidr_block = "10.0.0.0/24"
  availability_zone = "us-east-1b"
  map_public_ip_on_launch = true

  tags = {
 "name" = "pubsub1b"
  }


  

  resource "aws_subnet" "pubsub2" {
  vpc_id = aws_vpc.vpclab2.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1c"
  map_public_ip_on_launch = true

  }

  resource "aws_internet_gateway" "iglab2" {
  vpc_id = aws_vpc.vpclab2.id

  }



  resource "aws_route_table" "rtpubsub1" {
  vpc_id = aws_vpc.vpclab2.id

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



  resource "aws_route_table_association" "rtsub1" {
  subnet_id = aws_subnet.pubsub1.id
  route_table_id = aws_route_table.rtpubsub1.id

  }


   resource "aws_route_table" "rtpubsub2" {
   vpc_id = aws_vpc.vpclab2.id

   route {
   cidr_block = "0.0.0.0/0"
   gateway_id = aws_internet_gateway.iglab2.id


  }

   resource "aws_route_table_association" "RT-sub2" {
   subnet_id = aws_subnet.pubsub2.id
   route_table_id = aws_route_table.rtpubsub2.id

   }

   resource "aws_security_group" "sg" {
   name = "allow http"
   vpc_id = aws_vpc.vpclab2.id

   ingress {
   description = "allow http"
   from_port = "80"
   to_port = 80
   protocol = "tcp"
   cidr_blocks = ["0.0.0.0/0"]
   }

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

   egress  {
   cidr_blocks =  ["0.0.0.0/0"]
   description = "all outbound traf"
   from_port = 0
   protocol = "-1"
   to_port = 0

   }

 resource "aws_instance" "ec2ins" {
 ami = "ami-02f3f602d23f1659d"
 instance_type = "t2.micro"

 subnet_id = aws_subnet.pubsub1.id
 availability_zone = "us-east-1b"
 associate_public_ip_address = "true"
 vpc_security_group_ids = [aws_security_group.sg.id]


user_data  = <<-EOF
#!/bin/bash
 yum update
yum -y install httpd
chkconfig httpd on
service httpd start
EOF

}

15
  • 1
    This is not a valid TF code. Thus its not clear what you are actually doing.
    – Marcin
    Mar 18 at 11:05
  • Not sure what you mean as it did deploy? The code deployed an ec2 instance, a vpc, 2 public subnets, internet gateway, security group and route tables. The instance has been deployed into one of the subnets and I added user data to the instance. The infra deployed into AWS but I am not able to access the instance via a url when I enter the public IP address.
    – T_London
    Mar 18 at 11:13
  • 1
    Ok. So copy and paste your code from the question and try to run it. It has syntax errors.
    – Marcin
    Mar 18 at 11:15
  • 1
    Your code that you posted in the question is invalid. It does not even compile nor runs. It has syntax errors. So its not possible for you to "deploy" it. Whatever you deployed is not the code from the question.
    – Marcin
    Mar 18 at 11:23
  • 2
    As @Marcin said this isnt valid terraform code just look at your first resource for the VPC is doesnt have a closing } on it. there are a number of resources like this. if you paste the terraform code you have put in th question into any IDE with a TF parser or run a terraform init you will get a message like On main.tf line 75: There is no closing brace for this block before the end of the file. This may be caused by incorrect brace nesting elsewhere in this file. people are trying to help you but you claim they are wrong. clearly you must know more Mar 18 at 11:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.