Created
May 26, 2023 16:24
-
-
Save shreeegupta/2784c8b7c9aeb99a36b500531e1bd777 to your computer and use it in GitHub Desktop.
alb.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Security Group | |
resource "aws_security_group" "alb_security_group" { | |
name = "${var.env}-alb-security-group" | |
description = "ALB Security Group" | |
vpc_id = aws_vpc.vpc.id | |
ingress { | |
description = "HTTP from Internet" | |
from_port = 80 | |
to_port = 80 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
egress { | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
tags = { | |
Name = "${var.env}-alb-security-group" | |
Environment = var.env | |
} | |
} | |
# Application Load Balancer | |
resource "aws_lb" "alb" { | |
name = "${var.env}-alb" | |
internal = false | |
load_balancer_type = "application" | |
security_groups = [aws_security_group.alb_security_group.id] | |
subnets = [for i in aws_subnet.public_subnet : i.id] | |
tags = { | |
Name = "${var.env}-alb" | |
Environment = var.env | |
} | |
} | |
resource "aws_lb_target_group" "target_group" { | |
name = "${var.env}-target-group" | |
port = 80 | |
protocol = "HTTP" | |
vpc_id = aws_vpc.vpc.id | |
health_check { | |
path = "/" | |
matcher = 200 | |
} | |
} | |
resource "aws_lb_target_group_attachment" "target_group_attachment" { | |
count = 2 | |
target_group_arn = aws_lb_target_group.target_group.arn | |
target_id = aws_instance.web_server[count.index].id | |
port = 80 | |
} | |
resource "aws_lb_listener" "alb_listener" { | |
load_balancer_arn = aws_lb.alb.arn | |
port = "80" | |
protocol = "HTTP" | |
default_action { | |
type = "forward" | |
target_group_arn = aws_lb_target_group.target_group.arn | |
} | |
tags = { | |
Name = "${var.env}-alb-listenter" | |
Environment = var.env | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment