Skip to content

Commit a7becdf

Browse files
Merge pull request #3828 from nhsuk/validate_db_connection_in_internal_healthcheck
Validate DB connection in internal healthcheck
2 parents 59e1c1c + 922d5ba commit a7becdf

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ WORKDIR /rails
1010

1111
# Install base packages
1212
RUN apt-get update -qq && \
13-
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libicu-dev postgresql-client && \
13+
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libicu-dev postgresql-client jq && \
1414
rm -rf /var/lib/apt/lists /var/cache/apt/archives
1515

1616
# Set production environment

bin/internal_healthcheck

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
export PGPASSWORD="$(echo $DB_CREDENTIALS | jq -r .password)"
4+
psql -h "$DB_HOST" -d "$DB_NAME" -U "$(echo $DB_CREDENTIALS | jq -r .username)" -c "select 1" || {
5+
echo "DB connection could not be established: Internal healthcheck failed."; exit 1;
6+
}
7+
curl -f "$1" || {
8+
echo "DB connection could not be established, but $1 did not return a 200 response."; exit 2;
9+
}

terraform/app/ecs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module "web_service" {
3131
task_role_arn = aws_iam_role.ecs_task_role.arn
3232
log_group_name = aws_cloudwatch_log_group.ecs_log_group.name
3333
region = var.region
34-
health_check_command = ["CMD-SHELL", "curl -f http://localhost:4000/health/database || exit 1"]
34+
health_check_command = ["CMD-SHELL", "./bin/internal_healthcheck http://localhost:4000/health/database"]
3535
}
3636
network_params = {
3737
subnets = [aws_subnet.private_subnet_a.id, aws_subnet.private_subnet_b.id]
@@ -70,7 +70,7 @@ module "good_job_service" {
7070
task_role_arn = aws_iam_role.ecs_task_role.arn
7171
log_group_name = aws_cloudwatch_log_group.ecs_log_group.name
7272
region = var.region
73-
health_check_command = ["CMD-SHELL", "curl -f http://localhost:4000/status/connected || exit 1"]
73+
health_check_command = ["CMD-SHELL", "./bin/internal_healthcheck http://localhost:4000/status/connected"]
7474
}
7575
network_params = {
7676
subnets = [aws_subnet.private_subnet_a.id, aws_subnet.private_subnet_b.id]

terraform/app/loadbalancer.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ resource "aws_lb_target_group" "blue" {
8282
protocol = "HTTP"
8383
port = "traffic-port"
8484
matcher = "200"
85-
interval = 10
86-
timeout = 5
85+
interval = 5
86+
timeout = 4
8787
healthy_threshold = 2
8888
unhealthy_threshold = 2
8989
}
@@ -100,8 +100,8 @@ resource "aws_lb_target_group" "green" {
100100
protocol = "HTTP"
101101
port = "traffic-port"
102102
matcher = "200"
103-
interval = 10
104-
timeout = 5
103+
interval = 5
104+
timeout = 4
105105
healthy_threshold = 2
106106
unhealthy_threshold = 2
107107
}

0 commit comments

Comments
 (0)