Created
July 24, 2024 14:47
-
-
Save vvsevolodovich/7bed408ed6b404c768e75c4219b65e5a to your computer and use it in GitHub Desktop.
Terraform Load Balancing Example
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
provider "aws" { | |
alias = "us-east-1" | |
region = "us-east-1" | |
} | |
provider "aws" { | |
alias = "us-west-2" | |
region = "us-west-2" | |
} | |
# Fetch the latest Amazon Linux 2 AMI | |
data "aws_ami" "latest_amazon_linux" { | |
provider = aws.us-east-1 | |
most_recent = true | |
filter { | |
name = "name" | |
values = ["amzn2-ami-hvm-*-x86_64-gp2"] | |
} | |
filter { | |
name = "virtualization-type" | |
values = ["hvm"] | |
} | |
owners = ["amazon"] | |
} | |
# Get the default VPC in us-east-1 | |
data "aws_vpc" "default_us_east_1" { | |
provider = aws.us-east-1 | |
default = true | |
} | |
# Get subnets in the default VPC in us-east-1 | |
data "aws_subnet" "default_subnet_1a_us_east_1" { | |
provider = aws.us-east-1 | |
vpc_id = data.aws_vpc.default_us_east_1.id | |
availability_zone = "us-east-1a" | |
} | |
data "aws_subnet" "default_subnet_1b_us_east_1" { | |
provider = aws.us-east-1 | |
vpc_id = data.aws_vpc.default_us_east_1.id | |
availability_zone = "us-east-1b" | |
} | |
# Get the default security group in us-east-1 | |
data "aws_security_group" "default_us_east_1" { | |
provider = aws.us-east-1 | |
filter { | |
name = "group-name" | |
values = ["default"] | |
} | |
vpc_id = data.aws_vpc.default_us_east_1.id | |
} | |
# Create EC2 instances in the default VPC in us-east-1 | |
resource "aws_instance" "web1_us_east_1a" { | |
provider = aws.us-east-1 | |
ami = data.aws_ami.latest_amazon_linux.id | |
instance_type = "t2.micro" | |
subnet_id = data.aws_subnet.default_subnet_1a_us_east_1.id | |
vpc_security_group_ids = [data.aws_security_group.default_us_east_1.id] | |
tags = { | |
Name = "web1_us_east_1a" | |
} | |
} | |
resource "aws_instance" "web2_us_east_1b" { | |
provider = aws.us-east-1 | |
ami = data.aws_ami.latest_amazon_linux.id | |
instance_type = "t2.micro" | |
subnet_id = data.aws_subnet.default_subnet_1b_us_east_1.id | |
vpc_security_group_ids = [data.aws_security_group.default_us_east_1.id] | |
tags = { | |
Name = "web2_us_east_1b" | |
} | |
} | |
# Create Load Balancer in us-east-1 | |
resource "aws_lb" "my_lb_us_east_1" { | |
provider = aws.us-east-1 | |
name = "my-lb-us-east-1" | |
internal = false | |
load_balancer_type = "application" | |
security_groups = [data.aws_security_group.default_us_east_1.id] | |
subnets = [data.aws_subnet.default_subnet_1a_us_east_1.id, data.aws_subnet.default_subnet_1b_us_east_1.id] | |
enable_deletion_protection = true | |
enable_cross_zone_load_balancing = true | |
} | |
resource "aws_lb_listener" "front_end_us_east_1" { | |
provider = aws.us-east-1 | |
load_balancer_arn = aws_lb.my_lb_us_east_1.arn | |
port = "80" | |
protocol = "HTTP" | |
default_action { | |
type = "forward" | |
target_group_arn = aws_lb_target_group.my_target_group_us_east_1.arn | |
} | |
} | |
resource "aws_lb_target_group" "my_target_group_us_east_1" { | |
provider = aws.us-east-1 | |
name = "my-target-group-us-east-1" | |
port = 80 | |
protocol = "HTTP" | |
vpc_id = data.aws_vpc.default_us_east_1.id | |
health_check { | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
timeout = 3 | |
interval = 30 | |
path = "/" | |
matcher = "200" | |
} | |
} | |
resource "aws_lb_target_group_attachment" "web1_us_east_1" { | |
provider = aws.us-east-1 | |
target_group_arn = aws_lb_target_group.my_target_group_us_east_1.arn | |
target_id = aws_instance.web1_us_east_1a.id | |
port = 80 | |
} | |
resource "aws_lb_target_group_attachment" "web2_us_east_1" { | |
provider = aws.us-east-1 | |
target_group_arn = aws_lb_target_group.my_target_group_us_east_1.arn | |
target_id = aws_instance.web2_us_east_1b.id | |
port = 80 | |
} | |
# Repeat for us-west-2 region | |
data "aws_ami" "latest_amazon_linux_us_west_2" { | |
provider = aws.us-west-2 | |
most_recent = true | |
filter { | |
name = "name" | |
values = ["amzn2-ami-hvm-*-x86_64-gp2"] | |
} | |
filter { | |
name = "virtualization-type" | |
values = ["hvm"] | |
} | |
owners = ["amazon"] | |
} | |
# Get the default VPC in us-west-2 | |
data "aws_vpc" "default_us_west_2" { | |
provider = aws.us-west-2 | |
default = true | |
} | |
# Get subnets in the default VPC in us-west-2 | |
data "aws_subnet" "default_subnet_1a_us_west_2" { | |
provider = aws.us-west-2 | |
vpc_id = data.aws_vpc.default_us_west_2.id | |
availability_zone = "us-west-2a" | |
} | |
data "aws_subnet" "default_subnet_1b_us_west_2" { | |
provider = aws.us-west-2 | |
vpc_id = data.aws_vpc.default_us_west_2.id | |
availability_zone = "us-west-2b" | |
} | |
# Get the default security group in us-west-2 | |
data "aws_security_group" "default_us_west_2" { | |
provider = aws.us-west-2 | |
filter { | |
name = "group-name" | |
values = ["default"] | |
} | |
vpc_id = data.aws_vpc.default_us_west_2.id | |
} | |
# Create EC2 instances in the default VPC in us-west-2 | |
resource "aws_instance" "web1_us_west_2a" { | |
provider = aws.us-west-2 | |
ami = data.aws_ami.latest_amazon_linux_us_west_2.id | |
instance_type = "t2.micro" | |
subnet_id = data.aws_subnet.default_subnet_1a_us_west_2.id | |
vpc_security_group_ids = [data.aws_security_group.default_us_west_2.id] | |
tags = { | |
Name = "web1_us_west_2a" | |
} | |
} | |
resource "aws_instance" "web2_us_west_2b" { | |
provider = aws.us-west-2 | |
ami = data.aws_ami.latest_amazon_linux_us_west_2.id | |
instance_type = "t2.micro" | |
subnet_id = data.aws_subnet.default_subnet_1b_us_west_2.id | |
vpc_security_group_ids = [data.aws_security_group.default_us_west_2.id] | |
tags = { | |
Name = "web2_us_west_2b" | |
} | |
} | |
# Create Load Balancer in us-west-2 | |
resource "aws_lb" "my_lb_us_west_2" { | |
provider = aws.us-west-2 | |
name = "my-lb-us-west-2" | |
internal = false | |
load_balancer_type = "application" | |
security_groups = [data.aws_security_group.default_us_west_2.id] | |
subnets = [data.aws_subnet.default_subnet_1a_us_west_2.id, data.aws_subnet.default_subnet_1b_us_west_2.id] | |
enable_deletion_protection = true | |
enable_cross_zone_load_balancing = true | |
} | |
resource "aws_lb_listener" "front_end_us_west_2" { | |
provider = aws.us-west-2 | |
load_balancer_arn = aws_lb.my_lb_us_west_2.arn | |
port = "80" | |
protocol = "HTTP" | |
default_action { | |
type = "forward" | |
target_group_arn = aws_lb_target_group.my_target_group_us_west_2.arn | |
} | |
} | |
resource "aws_lb_target_group" "my_target_group_us_west_2" { | |
provider = aws.us-west-2 | |
name = "my-target-group-us-west-2" | |
port = 80 | |
protocol = "HTTP" | |
vpc_id = data.aws_vpc.default_us_west_2.id | |
health_check { | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
timeout = 3 | |
interval = 30 | |
path = "/" | |
matcher = "200" | |
} | |
} | |
resource "aws_lb_target_group_attachment" "web1_us_west_2" { | |
provider = aws.us-west-2 | |
target_group_arn = aws_lb_target_group.my_target_group_us_west_2.arn | |
target_id = aws_instance.web1_us_west_2a.id | |
port = 80 | |
} | |
resource "aws_lb_target_group_attachment" "web2_us_west_2" { | |
provider = aws.us-west-2 | |
target_group_arn = aws_lb_target_group.my_target_group_us_west_2.arn | |
target_id = aws_instance.web2_us_west_2b.id | |
port = 80 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment