Skip to content

Instantly share code, notes, and snippets.

@shreeegupta
Created May 26, 2023 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shreeegupta/2784c8b7c9aeb99a36b500531e1bd777 to your computer and use it in GitHub Desktop.
Save shreeegupta/2784c8b7c9aeb99a36b500531e1bd777 to your computer and use it in GitHub Desktop.
alb.tf
# 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