Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmorganoverbey committed Dec 5, 2023
1 parent 7aab7d8 commit 9427f27
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 228 deletions.
62 changes: 0 additions & 62 deletions global/s3/main.tf

This file was deleted.

8 changes: 0 additions & 8 deletions global/s3/outputs.tf

This file was deleted.

28 changes: 0 additions & 28 deletions prod/data-stores/mysql/main.tf

This file was deleted.

9 changes: 0 additions & 9 deletions prod/data-stores/mysql/outputs.tf

This file was deleted.

11 changes: 0 additions & 11 deletions prod/data-stores/mysql/variables.tf

This file was deleted.

42 changes: 0 additions & 42 deletions prod/services/webserver-cluster/main.tf

This file was deleted.

4 changes: 0 additions & 4 deletions prod/services/webserver-cluster/outputs.tf

This file was deleted.

152 changes: 152 additions & 0 deletions services/webserver-cluster/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
locals {
http_port = 80
any_port = 0
any_protocol = "-1"
tcp_protocol = "tcp"
all_ips = ["0.0.0.0/0"]
}

resource "aws_launch_configuration" "example" {
image_id = "ami-0cbd40f694b804622"
instance_type = var.instance_type
# The vpc_security_group_ids parameter is set to the ID of the security group created by the module.
security_groups = [aws_security_group.instance.id]
# The <<EOF and EOF are Terraform’s heredoc syntax, which allows you to create
# multiline strings without having to insert \n characters all over the plac


# Render the User Data script as a template
user_data = templatefile("${path.module}/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
})

# Required when using a launch configuration with an auto scaling group.
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 = var.min_size
max_size = var.max_size
tag {
key = "Name"
value = "${var.cluster_name}-asg"
propagate_at_launch = true
}
}

resource "aws_security_group" "instance" {
name = "${var.cluster_name}-instance"
ingress {
from_port = var.server_port
to_port = var.server_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}




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

data "aws_subnets" "default" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}
# the load balancer needs to have a security group defined to allow ingress and egress traffic
resource "aws_lb" "example" {
name = "${var.cluster_name}-lb"
load_balancer_type = "application"
subnets = data.aws_subnets.default.ids
security_groups = [aws_security_group.alb.id]
}

# This listener configures the ALB to listen on the default HTTP port, port 80, use HTTP as the protocol,
# and send a simple 404 page as the default response for requests that don’t match any listener rules.
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.example.arn
port = local.http_port
protocol = "HTTP"
# By default, return a simple 404 page
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "404: page not found"
status_code = 404
}
}
}

resource "aws_security_group" "alb" {
name = "${var.cluster_name}-alb"
# Allow inbound HTTP requests
ingress {
from_port = local.http_port
to_port = local.http_port
protocol = local.tcp_protocol
cidr_blocks = local.all_ips
}
# Allow all outbound requests
egress {
from_port = local.any_port
to_port = local.any_port
protocol = local.any_protocol
cidr_blocks = local.all_ips
}
}


# the lb target group tells the load balancer to route traffic to the instances in the auto scaling group
resource "aws_lb_target_group" "asg" {
name = "${var.cluster_name}-asg"
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
}
}

# the listener rule tells the load balancer to forward all requests to the target group
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
}
}
data "terraform_remote_state" "db" {
backend = "s3"
config = {
bucket = var.db_remote_state_bucket
key = var.db_remote_state_key
region = "us-west-1"
}
}
10 changes: 10 additions & 0 deletions services/webserver-cluster/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
output "alb_dns_name" {
value = aws_lb.example.dns_name
description = "The domain name of the load balancer"
}

output "asg_name" {
value = aws_autoscaling_group.example.name
description = "the autoscaling group name"
}

7 changes: 7 additions & 0 deletions services/webserver-cluster/user-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/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} &
29 changes: 29 additions & 0 deletions services/webserver-cluster/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
variable "server_port" {
description = "The port the server will use for HTTP requests"
type = number
default = 8080
}
variable "cluster_name" {
description = "The name to use for all the cluster resources"
type = string
}
variable "db_remote_state_bucket" {
description = "The name of the S3 bucket for the database's remote state"
type = string
}
variable "db_remote_state_key" {
description = "The path for the database's remote state in S3"
type = string
}
variable "instance_type" {
description = "The type of EC2 Instances to run (e.g. t2.micro)"
type = string
}
variable "min_size" {
description = "The minimum number of EC2 Instances in the ASG"
type = number
}
variable "max_size" {
description = "The maximum number of EC2 Instances in the ASG"
type = number
}
Loading

0 comments on commit 9427f27

Please sign in to comment.