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

Chapter-3 - webserver not showing proper html format. #88

Closed nsalfos closed 1 year ago

nsalfos commented 1 year ago

I've been trying to compare my code to what's here on the repo but i don't see any meaningful differences. And i'm not seeing the text with Header and Paragraphs as supposed to, instead i see this:

image

user_data.sh:

#!/bin/bash

cat > index.html <<EOF
<h1>Hello, World</h1>
<p>DB address: ${db_address}</p>
<p>DB port: ${db_port}</p>
EOF

nohup busybox httpd -f -p ${server_port} & 

cluster config:

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

terraform {
  backend "s3" {
    bucket          = "nsalfos-terraform-up-and-running-state"
    key             = "stage/services/webserver-cluster/terraform.tfstate"
    region          = "eu-west-1"
    dynamodb_table  = "terraform-up-and-running-locks"
    encrypt         = true
  }
}

resource "aws_launch_configuration" "example" {
  image_id          = "ami-05e786af422f8082a" #Ubuntu 22.04
  instance_type          = "t2.micro"
  security_groups = [aws_security_group.instance.id]

  user_data = templatefile("user_data.sh", {
    server_port = var.server_port
    db_address  = data.terraform_remote_state.db.outputs.address
    db_port     = data.terraform_remote_state.db.outputs.port
  })

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "example" {
    launch_configuration = aws_launch_configuration.example.name
    vpc_zone_identifier  = data.aws_subnets.default.ids

    target_group_arns = [aws_lb_target_group.asg.arn]
    health_check_type = "ELB"

    min_size = 2
    max_size = 10

    tag {
      key = "Name"
      value = "terraform-asg-example"
      propagate_at_launch = true
    }

    lifecycle {
      create_before_destroy = true
    }
}

resource "aws_security_group" "instance" {
   name = "terraform-example-instance"

  ingress {
    from_port   = var.server_port
    to_port     = var.server_port
    protocol    = "tcp"
    security_groups = [aws_security_group.alb.id]
  }
}

resource "aws_lb" "example" {
  name                = "terraform-asg-example"
  load_balancer_type  = "application"
  subnets             = data.aws_subnets.default.ids
  security_groups     = [aws_security_group.alb.id]
}

resource "aws_lb_listener" "http" {
  load_balancer_arn = aws_lb.example.arn
  port = 80
  protocol = "HTTP"

  default_action {
    type = "fixed-response"

    fixed_response {
      content_type = "text/plain"
      message_body = "404: page not found"
      status_code  = 404
    } 
  }
}

resource "aws_lb_listener_rule" "asg" {
  listener_arn = aws_lb_listener.http.arn
  priority = 100

  condition {
    path_pattern {
      values = ["*"]
    }
  }

  action {
    type              = "forward"
    target_group_arn  = aws_lb_target_group.asg.arn
  }

}

resource "aws_security_group" "alb" {
  name = "terraform-alb-example"

  ingress {
    cidr_blocks = [ "37.228.228.114/32" ]
    description = "Ingress rules for Load Balancer"
    from_port   = var.alb_port
    to_port     = var.alb_port
    protocol    = "tcp"
  } 

  egress {
    cidr_blocks = [ "0.0.0.0/0" ]
    description = "Egress rules for Load Balancer"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
  } 
}

resource "aws_lb_target_group" "asg" {
  name      = "terraform-asg-example"
  port      = var.server_port
  protocol  = "HTTP"
  vpc_id    = data.aws_vpc.default.id

  health_check {
    path                = "/"
    protocol            = "HTTP"
    matcher             = "200"
    interval            = 15
    timeout             = 3
    healthy_threshold   = 2
    unhealthy_threshold = 2
  }
}

data "aws_vpc" "default" {
  default = true
}

data "aws_subnets" "default" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.default.id]
  }
}

data "terraform_remote_state" "db" {
  backend = "s3"

  config = {
    bucket          = "nsalfos-terraform-up-and-running-state"
    key             = "stage/data-stores/mysql/terraform.tfstate"
    region          = "eu-west-1"
   }
}
brikis98 commented 1 year ago

Hard to know without a bit more context!

  1. Are you sure you're testing the right URL? Not some URL of a different ASG/ELB from earlier? One way to test is to run destroy and then apply again from scratch.
  2. Did you deploy an ASG originally with the old user data script (without markup), then change the user data script after? If so, did you run apply again to redeploy? If so, did you get to the part about how changing the launch configuration does not automatically roll out changes? If not, read on for how to do "zero downtime deployment" with ASGs.
nsalfos commented 1 year ago

Thanks, not sure what happened but, i kept on with the book, and naturally i had to destroy old ASG to be modified, and when re deployed the markup was there.