Skip to content

Commit

Permalink
alb
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzardr committed Feb 6, 2024
1 parent d7d6857 commit db35572
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ data "aws_subnets" "default"{
values = [data.aws_vpc.default.id]
}
}
output "public_ip" {
value = aws_instance.tf_example.public_ip
description = "The public IP Address of the webserver"
output "alb_dns_name" {
value = aws_lb.tf_example.dns_name
description = "The domain name of the ALB"

}

Expand Down Expand Up @@ -54,6 +54,9 @@ resource "aws_autoscaling_group" "tf_example" {
launch_configuration = aws_launch_configuration.tf_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

Expand All @@ -72,13 +75,30 @@ resource "aws_lb" "tf_example" {
security_groups = [aws_security_group.alb.id]
}

resource "aws_lb_target_group" "asg" {
name = "tf-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
}
}

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

default_action {
type = "fixed_response"
type = "fixed-response"

fixed_response {
content_type = "text/plain"
Expand All @@ -88,6 +108,23 @@ resource "aws_lb_listener" "http" {
}
}


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 = "tf-example-alb"

Expand All @@ -98,10 +135,15 @@ resource "aws_security_group" "alb" {
cidr_blocks = ["0.0.0.0/0"]
}

egress = {
egress = [{
description = "for all outgoing traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
cidr_blocks = [ "0.0.0.0/0" ]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = []
security_groups = []
self = false
}]
}

0 comments on commit db35572

Please sign in to comment.