brikis98 / terraform-up-and-running-code

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

Issue with code update for EC2 #102

Closed G0d3r closed 1 year ago

G0d3r commented 1 year ago

Hello everyone! I have a question regarding this code. I fixed my last issue about running the EC2 and hello world string but when I want to change a small piece of code e.g form "Hello_World" to "Hello_World_1" then I don't have access for some reason to the instance for the printed public address. It looks like I make a change with the string as I wrote above I use terraform apply command and then when I see a newly created address I don't have access to that even if I do the same steps. I use the command: curl :8080

Even when I open my running updated EC2 instance and when I try to use a new IP address with the :8080 it also does not work.

It looks like I have to create a new instance from the very beginning e.g copy the same code and try to run a new instance. Then it works but any updates have a negative impact on that and cause it to stop running.

Do you have any ideas about what's going on?

provider "aws" {
  region = "eu-central-1"
}

resource "aws_security_group" "instance" {
  name = "aws-terraform-example-5"
 ingress {
    from_port        = 8080
    to_port          = 8080
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "example" {
  ami           = "ami-08f13e5792295e1b2"
  instance_type = "t2.micro"

  user_data = <<-EOF
                #!/bin/bash
                echo "Hello_World" > index.html
                nohup python3 -m http.server 8080 &
                EOF
  tags = {
    Name = "aws-terraform-example-5"
  }

  vpc_security_group_ids = [aws_security_group.instance.id]
}

output "public_ip" {
  value = "${aws_instance.example.public_ip}"
  description = "Public IP address"
}
brikis98 commented 1 year ago

To make sure I understand:

  1. You ran apply to deploy an EC2 instance that says the text "Hello_World" via User Data and it worked.
  2. You changed the User Data to "Hello_world_1" and re-ran apply, but this time, it didn't work?

If so, did you set user_data_replace_on_change = true in the aws_instance resource as instructed in the book?

G0d3r commented 1 year ago

Sure, now it works! I couldn't find that in the book. Thank you for your help!

brikis98 commented 1 year ago

No problem!