Issue
When I apply the below configuration I get an AWS Linux EC2 instance running connected to the default VPC with the default public subnet connected to the default public IGW.
When I ping the public URL of the instance. the hello world webpage doesn't open, and the page errors out
I also SSH into the instance and pinged google.com, which also didn't work.
Can anyone kindly point me in the direction which would resolve this issue?
resource "aws_instance" "instance_1" {
ami = "ami-05548f9cecf47b442"
instance_type = "t2.micro"
security_groups = [aws_security_group.instances.name]
key_name = var.ssh_key
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World $(hostname -f)</h1>" > /var/www/html/index.html
EOF
}
resource "aws_security_group" "instances" {
name = var.instance_security_group
}
resource "aws_security_group_rule" "allow_http_inbound" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.instances.id
}
resource "aws_security_group_rule" "allow_ssh_inbound" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.instances.id
}
Trails:
- I tried putting the script into another file and referencing it. (didn't work)
- I tried Using other AMIs and other data scripts. (didn't work)
Solution
The solution as mentioned in the comments is that the configuration was missing an Egress security group:
resource "aws_security_group_rule" "allow_all_outbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.instances.id
}
Answered By - GM_ Answer Checked By - Gilberto Lyons (WPSolving Admin)