Cannot access AWS EC2 public IP created through Terraform

Viewed 447

I am trying to run one of the first basic examples from the book Terraform Up and Running. My main.tf is almost identical to the one in the link apart from the version:

provider "aws" {
  region = "us-east-2"
}

resource "aws_instance" "example" {
  ami                    = "ami-0c55b159cbfafe1f0"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.instance.id]

  user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF

  tags = {
    Name = "terraform-example"
  }
}

resource "aws_security_group" "instance" {

  name = var.security_group_name

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

variable "security_group_name" {
  description = "The name of the security group"
  type        = string
  default     = "terraform-example-instance"
}

output "public_ip" {
  value       = aws_instance.example.public_ip
  description = "The public IP of the Instance"
}

I ran terraform apply and everything seems to be created successfully. However, when I try to run curl http://<EC2_INSTANCE_PUBLIC_IP>:8080, the command hangs.

I created my AWS account right before I ran the example, so it uses the default network configuration.

The routing table has an entry pointing to the Internet Gateway of the VPC:

Destination    |      Target      |     Status      |   Propagated
0.0.0.0/0      |     igw-<igwId>  |      active     |      No

The Network ACLs has the default settings:

Rule number   |    Type    | Protocol  | Port range | Source    | Allow/Deny
100           | All traffic|    All    |    All     | 0.0.0.0/0 |    Allow
*             | All traffic|    All    |    All     | 0.0.0.0/0 |    Deny

My Terraform version is v0.14.10.

Any ideas on how to access the instance's server through the EC2's public IP?

1 Answers

There is nothing wrong the TF program. I verified it using my sandbox account and it works as expected. It takes 1-2 minutes for script to start working, so maybe you are testing it too soon.

So whatever difficulties you have are not due to the script itself. So either what you posted on SO is not your actual code, or you have somehow modified your VPC configurations which make the instance not-accessible.

Related