brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.87k stars 1.92k forks source link

chapter 2 user data #42

Closed davidATsains closed 4 years ago

davidATsains commented 4 years ago

I am learning Terraform using this book. I am chapter 2 section: Deploy a single web server. i followed the code and everything. but when I curl the public ip:8080,
I have already tried curl http://elastic_instance_public_ip:8080 and curl https://elastic_instance_public_ip:8080

the result is Operation timed out How do I solve this issue.

Below is my code:

provider "aws" {
  version = "~>2.0"
  region  = "eu-west-1"

}

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

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

  tags = {
    Name = "test-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"
  default     = "terraform-example-instance"
}
output "public_ip" {
  value = aws_instance.example.public_ip
  description = "The public IP of the instance"
}
brikis98 commented 4 years ago

You're running ami-01f14919ba412de34, which I believe is Amazon Linux. This AMI does not have busybox installed by default, so the User Data script is likely failing. You can most likely verify this by looking at the system log of your EC2 instance; it's a bit hard to dig it out, but it'll probably have an error about busybox not being found.

In the book, I use Ubuntu by default, so you may want to switch to that if you're going to use busybox.

davidATsains commented 4 years ago

You're running ami-01f14919ba412de34, which I believe is Amazon Linux. This AMI does not have busybox installed by default, so the User Data script is likely failing. You can most likely verify this by looking at the system log of your EC2 instance; it's a bit hard to dig it out, but it'll probably have an error about busybox not being found.

In the book, I use Ubuntu by default, so you may want to switch to that if you're going to use busybox.

thank you

vim-diesel commented 4 years ago

still fails on ubuntu

yantaq commented 3 years ago

a quick workaround that works:

nohup busybox httpd -f -p 8080 &

with

nohup python -m SimpleHTTPServer 8080 &
amelvin commented 1 year ago

As per the author comment, switching to an Ubuntu instance worked for me with no other changes:

Terraform v1.3.2 on darwin_arm64