Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add healthcheck variables to support deploying non-custom docker containers #460

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions infra/modules/service/load-balancer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ resource "aws_lb_target_group" "app_tg" {
target_type = "ip"
deregistration_delay = "30"

health_check {
path = "/health"
port = var.container_port
healthy_threshold = 2
unhealthy_threshold = 10
interval = 30
timeout = 29
matcher = "200-299"
dynamic "health_check" {
for_each = var.enable_healthcheck ? [0] : []
content {
path = "/${local.healthcheck_path}"
port = var.container_port
healthy_threshold = 2
unhealthy_threshold = 10
interval = 30
timeout = 29
matcher = "200-299"
}
}

lifecycle {
Expand Down
19 changes: 11 additions & 8 deletions infra/modules/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ locals {
log_stream_prefix = var.service_name
task_executor_role_name = "${var.service_name}-task-executor"
image_url = "${data.aws_ecr_repository.app.repository_url}:${var.image_tag}"
healthcheck_path = trimprefix(var.healthcheck_path, "/")

base_environment_variables = [
{ name : "PORT", value : tostring(var.container_port) },
Expand Down Expand Up @@ -76,14 +77,16 @@ resource "aws_ecs_task_definition" "app" {
# Need to define all parameters in the healthCheck block even if we want
# to use AWS's defaults, otherwise the terraform plan will show a diff
# that will force a replacement of the task definition
healthCheck = {
interval = 30,
retries = 3,
timeout = 5,
command = ["CMD-SHELL",
"wget --no-verbose --tries=1 --spider http://localhost:${var.container_port}/health || exit 1"
]
},
healthCheck = var.enable_healthcheck ? {
interval = 30,
retries = 3,
timeout = 5,
startPeriod = var.healthcheck_start_period,
command = [
"CMD-SHELL",
var.healthcheck_type == "curl" ? "curl --fail http://localhost:${var.container_port}/${local.healthcheck_path} || exit 1" : "wget --no-verbose --tries=1 --spider http://localhost:${var.container_port}/${local.healthcheck_path} || exit 1",
],
} : null,
environment = local.environment_variables,
portMappings = [
{
Expand Down
32 changes: 32 additions & 0 deletions infra/modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ variable "db_vars" {
})
default = null
}

#-------------------
# Healthcheck
#-------------------

variable "enable_healthcheck" {
type = bool
description = "Enable container healthcheck"
default = true
}

variable "healthcheck_path" {
type = string
description = "The path to the application healthcheck"
default = "/health"
}

variable "healthcheck_type" {
type = string
description = "Whether to configure a curl or wget healthcheck. use wget for alpine-based images"
default = "wget"
validation {
condition = contains(["curl", "wget"], var.healthcheck_type)
error_message = "choose either: curl or wget"
}
}

variable "healthcheck_start_period" {
type = number
description = "The optional grace period to provide containers time to bootstrap in before failed health checks count towards the maximum number of retries"
default = 0
}