diff --git a/ecs/task/README.md b/ecs/task/README.md
index a60e88be..0bb945ed 100644
--- a/ecs/task/README.md
+++ b/ecs/task/README.md
@@ -73,6 +73,12 @@ We recommend creating the task definition using `image` or `image_name` + `image
| [environment\_variables](#input\_environment\_variables) | Environment variables to pass to a container. | `map(string)` | `{}` | no |
| [essential](#input\_essential) | If the essential parameter of a container is marked as true, and that container fails or stops for any reason, all other containers that are part of the task are stopped. | `bool` | `true` | no |
| [execution\_role\_arn](#input\_execution\_role\_arn) | The ARN of IAM role that allows ECS to execute your task.
Required when:
- using `environment_parameters` to give ECS access to the SSM parameters
- using `launch_type = "FARGATE"` when running the task | `string` | `null` | no |
+| [healthcheck\_command](#input\_healthcheck\_command) | Command that the container runs to determine if it is healthy | `string` | `null` | no |
+| [healthcheck\_interval](#input\_healthcheck\_interval) | The time period in seconds between each health check execution. You may specify between 5 and 300 seconds. | `number` | `30` | no |
+| [healthcheck\_retries](#input\_healthcheck\_retries) | The number of times to retry a failed health check before the container is considered unhealthy. You may specify between 1 and 10 retries. | `number` | `2` | no |
+| [healthcheck\_shell](#input\_healthcheck\_shell) | Whether the healthcheck\_command should be run using a shell | `bool` | `true` | no |
+| [healthcheck\_start\_period](#input\_healthcheck\_start\_period) | The optional grace period to provide containers time to bootstrap before failed health checks count towards the maximum number of retries. You can specify between 0 and 300 seconds. | `number` | `0` | no |
+| [healthcheck\_timeout](#input\_healthcheck\_timeout) | The time period in seconds to wait for a health check to succeed before it is considered a failure. You may specify between 2 and 60 seconds. | `number` | `5` | no |
| [image](#input\_image) | Full container image name, including the version tag. Either image or image\_name has to be provided. | `string` | `null` | no |
| [image\_name](#input\_image\_name) | Container image name, without the version tag. Either image or image\_name has to be provided. | `string` | `null` | no |
| [image\_tag](#input\_image\_tag) | Container image version tag, if omitted will use one from the latest revision. Used only when image\_name is provided. | `string` | `null` | no |
diff --git a/ecs/task/container_definition/README.md b/ecs/task/container_definition/README.md
index ba2c35b7..6fbf1046 100644
--- a/ecs/task/container_definition/README.md
+++ b/ecs/task/container_definition/README.md
@@ -34,6 +34,12 @@ No resources.
| [environment\_parameters](#input\_environment\_parameters) | Environment variables that should be set to Systems Manager parameter values.
Maps environment variable names to parameters. |
map(object({| `{}` | no | | [environment\_variables](#input\_environment\_variables) | Environment variables to pass to a container. | `map(string)` | `{}` | no | | [essential](#input\_essential) | If the essential parameter of a container is marked as true, and that container fails or stops for any reason, all other containers that are part of the task are stopped. | `bool` | `true` | no | +| [healthcheck\_command](#input\_healthcheck\_command) | Command that the container runs to determine if it is healthy | `string` | `null` | no | +| [healthcheck\_interval](#input\_healthcheck\_interval) | The time period in seconds between each health check execution. You may specify between 5 and 300 seconds. | `number` | `30` | no | +| [healthcheck\_retries](#input\_healthcheck\_retries) | The number of times to retry a failed health check before the container is considered unhealthy. You may specify between 1 and 10 retries. | `number` | `2` | no | +| [healthcheck\_shell](#input\_healthcheck\_shell) | Whether the healthcheck\_command should be run using a shell | `bool` | `true` | no | +| [healthcheck\_start\_period](#input\_healthcheck\_start\_period) | The optional grace period to provide containers time to bootstrap before failed health checks count towards the maximum number of retries. You can specify between 0 and 300 seconds. | `number` | `0` | no | +| [healthcheck\_timeout](#input\_healthcheck\_timeout) | The time period in seconds to wait for a health check to succeed before it is considered a failure. You may specify between 2 and 60 seconds. | `number` | `5` | no | | [image](#input\_image) | Container image | `string` | n/a | yes | | [log\_config](#input\_log\_config) | jsonencodable logging configuration | `any` | `null` | no | | [memory\_hard\_limit](#input\_memory\_hard\_limit) | The amount (in MiB) of memory to present to the container. If your container attempts to exceed the memory specified here, the container is killed. | `number` | `1024` | no | diff --git a/ecs/task/container_definition/main.tf b/ecs/task/container_definition/main.tf index cdbf0fb1..ec87c446 100644 --- a/ecs/task/container_definition/main.tf +++ b/ecs/task/container_definition/main.tf @@ -48,5 +48,14 @@ locals { }] logConfiguration = var.log_config + + # https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_HealthCheck.html + healthCheck = var.healthcheck_command ? { + command = [var.healthcheck_shell ? "CMD-SHELL" : "CMD", var.healthcheck_command] + interval = var.healthcheck_interval + retries = var.healthcheck_retries + timeout = var.healthcheck_timeout + startPeriod = var.healthcheck_start_period + } : null } } diff --git a/ecs/task/container_definition/variables.tf b/ecs/task/container_definition/variables.tf index 94e6be03..25e1ad04 100644 --- a/ecs/task/container_definition/variables.tf +++ b/ecs/task/container_definition/variables.tf @@ -96,3 +96,38 @@ variable "log_config" { default = null } +variable "healthcheck_command" { + description = "Command that the container runs to determine if it is healthy" + type = string + default = null +} + +variable "healthcheck_shell" { + description = "Whether the healthcheck_command should be run using a shell" + type = bool + default = true +} + +variable "healthcheck_interval" { + description = "The time period in seconds between each health check execution. You may specify between 5 and 300 seconds." + type = number + default = 30 +} + +variable "healthcheck_retries" { + description = "The number of times to retry a failed health check before the container is considered unhealthy. You may specify between 1 and 10 retries." + type = number + default = 2 +} + +variable "healthcheck_start_period" { + description = "The optional grace period to provide containers time to bootstrap before failed health checks count towards the maximum number of retries. You can specify between 0 and 300 seconds." + type = number + default = 0 +} + +variable "healthcheck_timeout" { + description = "The time period in seconds to wait for a health check to succeed before it is considered a failure. You may specify between 2 and 60 seconds." + type = number + default = 5 +} diff --git a/ecs/task/examples/basic/main.tf b/ecs/task/examples/basic/main.tf index 1cdcbeb9..34afeba8 100644 --- a/ecs/task/examples/basic/main.tf +++ b/ecs/task/examples/basic/main.tf @@ -35,6 +35,8 @@ module "httpbin" { } execution_role_arn = module.httpbin_execution_role.arn + + healthcheck_command = "python3 -c \"import http.client, sys; conn = http.client.HTTPConnection('localhost'); conn.request('GET', '/'); sys.exit(0) if conn.getresponse().status == 200 else sys.exit(1)\"" } output "httpbin_arn" { diff --git a/ecs/task/main.tf b/ecs/task/main.tf index 5392168c..9fb11131 100644 --- a/ecs/task/main.tf +++ b/ecs/task/main.tf @@ -43,6 +43,13 @@ module "container" { environment_parameters = var.environment_parameters enable_environment_parameters_hash = var.enable_environment_parameters_hash log_config = module.container_log.container_config + + healthcheck_command = var.healthcheck_command + healthcheck_shell = var.healthcheck_shell + healthcheck_interval = var.healthcheck_interval + healthcheck_retries = var.healthcheck_retries + healthcheck_start_period = var.healthcheck_start_period + healthcheck_timeout = var.healthcheck_timeout } resource "aws_ecs_task_definition" "task" { diff --git a/ecs/task/variables.tf b/ecs/task/variables.tf index 06a11af5..4d7892ca 100644 --- a/ecs/task/variables.tf +++ b/ecs/task/variables.tf @@ -185,3 +185,39 @@ variable "placement_constraint_expressions" { type = list(string) default = [] } + +variable "healthcheck_command" { + description = "Command that the container runs to determine if it is healthy" + type = string + default = null +} + +variable "healthcheck_shell" { + description = "Whether the healthcheck_command should be run using a shell" + type = bool + default = true +} + +variable "healthcheck_interval" { + description = "The time period in seconds between each health check execution. You may specify between 5 and 300 seconds." + type = number + default = 30 +} + +variable "healthcheck_retries" { + description = "The number of times to retry a failed health check before the container is considered unhealthy. You may specify between 1 and 10 retries." + type = number + default = 2 +} + +variable "healthcheck_start_period" { + description = "The optional grace period to provide containers time to bootstrap before failed health checks count towards the maximum number of retries. You can specify between 0 and 300 seconds." + type = number + default = 0 +} + +variable "healthcheck_timeout" { + description = "The time period in seconds to wait for a health check to succeed before it is considered a failure. You may specify between 2 and 60 seconds." + type = number + default = 5 +}
arn = string
version = number
}))