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 an optional health check to the container definition (#15) #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '3'

env:
TERM: screen-256color
Expand Down Expand Up @@ -36,7 +36,7 @@ tasks:

CWD=$PWD

for d in $DIRECTORIES; do
for d in $DIRECTORIES; do
cd $d
echo "${BOLD}$PWD:${NORM}"

Expand Down Expand Up @@ -66,4 +66,4 @@ tasks:
silent: true
cmds:
- go test -v ./... -timeout=1h

8 changes: 8 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ module "fargate" {
TEST_VARIABLE = "TEST_VALUE"
}

container_health_check = {
retries = 3,
command = ["CMD-SHELL", "curl -f http://localhost:9000/ || exit 1"],
timeout = 5,
interval = 30,
startPeriod = 15
}

health_check = {
port = "traffic-port"
path = "/"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ require (
github.com/gruntwork-io/terratest v0.36.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
15 changes: 13 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ resource "aws_security_group_rule" "egress_service" {
# LB Target group
# ------------------------------------------------------------------------------
resource "aws_lb_target_group" "task" {
name = "${var.name_prefix}-${var.task_container_port}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing name forces new resource.

count = var.lb_arn == "" ? 0 : 1
vpc_id = var.vpc_id
protocol = var.task_container_protocol
Expand Down Expand Up @@ -157,12 +156,14 @@ locals {
"environment" = local.task_container_environment
"environmentFiles" = var.task_container_environment_file
"MountPoints" = local.task_container_mount_points
"ulimits" = var.task_container_ulimits
"logConfiguration" = {
"logDriver" = "awslogs"
"options" = local.log_configuration_options
}
"privileged" : var.privileged
"readonlyRootFilesystem" : var.readonlyRootFilesystem
"healthCheck" : var.container_health_check
}, local.task_container_secrets, local.repository_credentials)
Comment on lines +166 to 167
Copy link
Contributor

@k1rd3rf k1rd3rf Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the pattern of some of the other optional variables (line 136-141), you could do something like

Suggested change
"healthCheck" : var.container_health_check
}, local.task_container_secrets, local.repository_credentials)
}, local.task_container_secrets, local.repository_credentials, local.task_container_health_check)
task_container_health_check = var.container_health_check != null ? { "healthCheck" = var.container_health_check } : null

}

Expand Down Expand Up @@ -200,6 +201,7 @@ resource "aws_ecs_task_definition" "task" {
operating_system_family = var.task_definition_os_family
cpu_architecture = var.task_definition_cpu_arch
}
tags = var.tags
}

resource "aws_ecs_service" "service" {
Expand All @@ -214,7 +216,7 @@ resource "aws_ecs_service" "service" {
cluster = var.cluster_id
task_definition = var.task_definition != "" ? var.task_definition : aws_ecs_task_definition.task.arn
desired_count = var.desired_count
launch_type = "FARGATE"
launch_type = length(var.capacity_provider_strategy) == 0 ? "FARGATE" : null
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
health_check_grace_period_seconds = var.lb_arn == "" ? null : var.health_check_grace_period_seconds
Expand Down Expand Up @@ -262,6 +264,15 @@ resource "aws_ecs_service" "service" {
container_name = var.container_name != "" ? var.container_name : var.name_prefix
}
}
tags = var.tags
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategy
content {
base = lookup(capacity_provider_strategy.value, "base", null)
capacity_provider = lookup(capacity_provider_strategy.value, "capacity_provider", null)
weight = lookup(capacity_provider_strategy.value, "weight", null)
}
}
}

# HACK: The workaround used in ecs/service does not work for some reason in this module, this fixes the following error:
Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ output "target_group_arn" {
value = var.lb_arn == "" ? null : aws_lb_target_group.task[0].arn
}

output "target_group_arn_suffix" {
description = "The ARN suffix for use with CloudWatch Metrics."
value = var.lb_arn == "" ? null : aws_lb_target_group.task[0].arn_suffix
}

output "target_group_name" {
description = "The Name of the Target Group."
value = var.lb_arn == "" ? null : aws_lb_target_group.task[0].name
Expand Down
30 changes: 30 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ variable "volumes" {
default = []
}

variable "capacity_provider_strategy" {
description = "List capacity provider strategy"
type = list(any)
default = []
}

variable "extra_target_groups" {
description = "List of extra target group configurations used to register a service to multiple target groups"
type = list(object({
Expand All @@ -314,3 +320,27 @@ variable "extra_target_groups" {
}))
default = []
}

variable "container_health_check" {
description = "An ECS TaskDefinition HealthCheck object to set in each container"
default = null
type = object(
{
command = list(string)
interval = number
retries = number
startPeriod = number
timeout = number
}
)
}

variable "task_container_ulimits" {
type = list(object({
name = string
hardLimit = number
softLimit = number
}))
description = "(Optional) Container ulimit settings. This is a list of maps, where each map should contain \"name\", \"hardLimit\" and \"softLimit\""
default = null
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ terraform {
}
null = {
source = "hashicorp/null"
version = "~> 3.1.0"
version = ">= 3.1.0"
}
}
}