variable "http_port" {
 description = "Port to listen for HTTP requests on"
 default     = 8080
 type        = number
}

output "public_ip" {
 value       = aws_instance.cloudbuntu.public_ip
 description = "The public IP of our new cloudbuntu!"
}

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

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

resource "aws_launch_configration" "clusterbuntu" {
 image_id        = "ami-0862be96e41dcbf74"
 instance_type   = "t2.micro"
 security_groups = [aws_security_group.cloudwall.id]

 user_data       = <<-EOF
                   #!/bin/bash
                   echo "Hello, World! I'm $(hostname)!" > index.html
                   echo "I'm running $(uname -s) on $(uname -i)!" >> index.html
                   nohup busybox httpd -f -p ${var.http_port} &
                   EOF

 # Required when using a launch configuration with an auto-scaling group
 lifecycle {
  create_before_destroy = true
 }
}

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

 min_size = 2
 max_size = 10

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

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

 ingress {
  from_port   = var.http_port
  to_port     = var.http_port
  protocol    = "tcp"
  cidr_blocks = ["0.0.0.0/0"]
 }
}