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

provider "random" {}

resource "random_pet" "name" {}

resource "aws_instance" "web" {
  ami           = "ami-0022f774911c1d690"
  instance_type = "t2.micro"
  user_data     = file("init-script.sh")
  vpc_security_group_ids = [aws_security_group.web-sg.id]

  tags = {
    Name = random_pet.name.id
  }
}

resource "aws_security_group" "web-sg" {
  name = "${random_pet.name.id}-sg"
  ingress {
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  } 
  
  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

I am using Terraform to deploy a PHP web server. As shown, ingress and egress rules are defined and I should be able to connect. What am I doing wrong?

Also, I am following this tutorial:

https://learn.hashicorp.com/tutorials/terraform/resource?in=terraform/configuration-language

14
  • Looks like this example is assuming that you are deploying into an existing VPC the route table of which has a route to an internet gateway. Is that the case for you?
    – Paolo
    May 19, 2022 at 13:46
  • How are you connecting? Have you done terraform output application-url?
    – baduker
    May 19, 2022 at 13:49
  • @Paolo I am deploying to the default AWS VPC
    – PySquirrel
    May 19, 2022 at 13:50
  • @PySquirrel and does the route table associated to the VPC have a route to an internet gateway?
    – Paolo
    May 19, 2022 at 13:51
  • @baduker "application-url = "ec2-3-82-38-246.compute-1.amazonaws.com/index.php" yes, I am connecting to this URL, still "ec2-3-82-38-246.compute-1.amazonaws.com refused to connect."
    – PySquirrel
    May 19, 2022 at 13:51

1 Answer 1

2

I cloned and ran this tutorial myself & it does not work for me either. Trying to connect gives me a timeout error.

1st observation - it's very old & not a great tutorial. The AMI it samples does not even exist anymore & I used the latest AMZN Default Linux 2 AMI instead.

2nd observation - no key pair is created or used with this EC2 Instance in the tutorial which makes troubleshooting difficult since you cannot connect & view logs.

3rd observation - ties in with my 1st one, chkconfig is used in the init-script & if you're using a newer AMI (centos, redhat or amzn linux image) they all likely use systemctl instead.

Conclusion: This tutorial really needs updating & I don't recommend using it since it's virtually unusable and very outdated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.