diff --git a/chapterone/main.tf b/chapterone/main.tf deleted file mode 100644 index 5c1b2d9..0000000 --- a/chapterone/main.tf +++ /dev/null @@ -1,12 +0,0 @@ -provider "aws" { - region = "us-east-2" -} - -resource "aws_instance" "cloudbuntu" { - ami = "ami-0862be96e41dcbf74" - instance_type = "t2.micro" - - tags = { - Name = "terraform-example" - } -} diff --git a/chapterone/.terraform.lock.hcl b/chaptertwo/.terraform.lock.hcl similarity index 100% rename from chapterone/.terraform.lock.hcl rename to chaptertwo/.terraform.lock.hcl diff --git a/chaptertwo/main.tf.autoscaling b/chaptertwo/main.tf.autoscaling new file mode 100644 index 0000000..41efd06 --- /dev/null +++ b/chaptertwo/main.tf.autoscaling @@ -0,0 +1,66 @@ +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"] + } +} diff --git a/chapterone/main.tf.basics b/chaptertwo/main.tf.basics similarity index 100% rename from chapterone/main.tf.basics rename to chaptertwo/main.tf.basics diff --git a/chapterone/main.tf.variables b/chaptertwo/main.tf.variables similarity index 100% rename from chapterone/main.tf.variables rename to chaptertwo/main.tf.variables