Last active
March 16, 2023 20:08
-
-
Save clarkdave/b4cfb66bb9bb1cfe492b33ede96f5343 to your computer and use it in GitHub Desktop.
[Terraform] Load balanced PgBouncer service on Amazon ECS
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
# | |
# Reference implementation of a load balanced PgBouncer service on Amazon ECS | |
# | |
# see: https://engineering.loyaltylion.com/load-balanced-pgbouncer-service-on-amazon-ecs-f02120d1733e | |
# | |
resource "aws_lb" "pgbouncer" { | |
name = "${var.environment}-pgbouncer" | |
internal = true | |
load_balancer_type = "network" | |
subnets = ["${var.subnets}"] | |
enable_cross_zone_load_balancing = true | |
enable_deletion_protection = true | |
tags { | |
Service = "pgbouncer" | |
Environment = "${var.environment}" | |
Terraform = "1" | |
} | |
} | |
resource "aws_lb_target_group" "pgbouncer" { | |
name = "${var.environment}-pgbouncer" | |
port = 6543 | |
protocol = "TCP" | |
vpc_id = "${var.vpc_id}" | |
target_type = "ip" | |
deregistration_delay = 180 | |
health_check { | |
port = "traffic-port" | |
protocol = "TCP" | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
interval = 10 | |
} | |
tags { | |
Service = "pgbouncer" | |
Environment = "${var.environment}" | |
Terraform = "1" | |
} | |
} | |
resource "aws_lb_listener" "pgbouncer" { | |
load_balancer_arn = "${aws_lb.pgbouncer.id}" | |
port = 6543 | |
protocol = "TCP" | |
default_action { | |
target_group_arn = "${aws_lb_target_group.pgbouncer.id}" | |
type = "forward" | |
} | |
} | |
resource "aws_ecs_task_definition" "pgbouncer" { | |
family = "${var.environment}-pgbouncer" | |
network_mode = "awsvpc" | |
lifecycle { | |
create_before_destroy = true | |
} | |
container_definitions = <<EOF | |
[ | |
{ | |
"name": "pgbouncer", | |
"image": "your-pgbouncer-image:tag", | |
"portMappings": [{ | |
"hostPort": 6543, | |
"containerPort": 6543, | |
"protocol": "tcp" | |
}], | |
"cpu": 0, | |
"memoryReservation": 128, | |
"ulimits": [ | |
{ | |
"name": "nofile", | |
"softLimit": 16384, | |
"hardLimit": 16384 | |
} | |
], | |
"essential": true | |
} | |
] | |
EOF | |
} | |
resource "aws_security_group" "pgbouncer" { | |
name = "${var.environment}-ecs-pgbouncer" | |
vpc_id = "${var.vpc_id}" | |
ingress { | |
protocol = "tcp" | |
from_port = 6543 | |
to_port = 6543 | |
security_groups = ["${var.cluster_security_group_id}"] | |
} | |
egress { | |
protocol = -1 | |
from_port = 0 | |
to_port = 0 | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
} | |
resource "aws_ecs_service" "pgbouncer" { | |
name = "${var.environment}-pgbouncer" | |
cluster = "${var.cluster_id}" | |
task_definition = "${aws_ecs_task_definition.pgbouncer.arn}" | |
desired_count = 3 | |
health_check_grace_period_seconds = 10 | |
network_configuration { | |
subnets = ["${var.vpc_subnets}"] | |
security_groups = ["${aws_security_group.pgbouncer.id}"] | |
} | |
load_balancer { | |
target_group_arn = "${aws_lb_target_group.pgbouncer.id}" | |
container_name = "pgbouncer" | |
container_port = 6543 | |
} | |
ordered_placement_strategy { | |
type = "spread" | |
field = "instanceId" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment