Skip to content

Commit 06cc2e1

Browse files
authored
Feature/task healthcheck (#11)
* do not create target group if we dont need it (when we are not load balancing) * add optional task healthcheck * regen readme
1 parent c6bac9e commit 06cc2e1

File tree

4 files changed

+65
-41
lines changed

4 files changed

+65
-41
lines changed

README.md

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,57 +70,65 @@ Module is to be used with Terraform > 0.12.
7070
Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](https://www.linkedin.com/in/marcincuber/).
7171

7272
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
73+
## Providers
74+
75+
| Name | Version |
76+
|------|---------|
77+
| aws | n/a |
78+
| null | n/a |
79+
7380
## Inputs
7481

7582
| Name | Description | Type | Default | Required |
76-
|------|-------------|:----:|:-----:|:-----:|
77-
| capacity\_provider\_strategy | \(Optional\) The capacity\_provider\_strategy configuration block. This is a list of maps, where each map should contain "capacity\_provider ", "weight" and "base" | list | `[]` | no |
78-
| cluster\_id | The Amazon Resource Name \(ARN\) that identifies the cluster. | string | n/a | yes |
79-
| container\_name | Optional name for the container to be used instead of name\_prefix. | string | `""` | no |
80-
| deployment\_controller\_type | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS. | string | `"ECS"` | no |
81-
| deployment\_maximum\_percent | The upper limit of the number of running tasks that can be running in a service during a deployment | number | `"200"` | no |
82-
| deployment\_minimum\_healthy\_percent | The lower limit of the number of running tasks that must remain running and healthy in a service during a deployment | number | `"50"` | no |
83-
| desired\_count | The number of instances of the task definitions to place and keep running. | number | `"1"` | no |
84-
| docker\_volume\_configuration | \(Optional\) Used to configure a docker volume option "docker\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs\_task\_definition.html | list | `[]` | no |
85-
| health\_check | A health block containing health check settings for the target group. Overrides the defaults. | map(string) | n/a | yes |
86-
| health\_check\_grace\_period\_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers. | number | `"300"` | no |
87-
| lb\_arn | Arn for the LB for which the service should be attach to. | string | n/a | yes |
88-
| load\_balanced | Whether the task should be loadbalanced. | bool | `"true"` | no |
89-
| log\_retention\_in\_days | Number of days the logs will be retained in CloudWatch. | number | `"30"` | no |
90-
| logs\_kms\_key | The KMS key ARN to use to encrypt container logs. | string | `""` | no |
91-
| name\_prefix | A prefix used for naming resources. | string | n/a | yes |
92-
| placement\_constraints | \(Optional\) A set of placement constraints rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. This is a list of maps, where each map should contain "type" and "expression" | list | `[]` | no |
93-
| private\_subnet\_ids | A list of private subnets inside the VPC | list(string) | n/a | yes |
94-
| propogate\_tags | Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK\_DEFINITION. | string | `"TASK_DEFINITION"` | no |
95-
| proxy\_configuration | \(Optional\) The proxy configuration details for the App Mesh proxy. This is a list of maps, where each map should contain "container\_name", "properties" and "type" | list | `[]` | no |
96-
| repository\_credentials | name or ARN of a secrets manager secret \(arn:aws:secretsmanager:region:aws\_account\_id:secret:secret\_name\) | string | `""` | no |
97-
| repository\_credentials\_kms\_key | key id, key ARN, alias name or alias ARN of the key that encrypted the repository credentials | string | `"alias/aws/secretsmanager"` | no |
98-
| service\_registry\_arn | ARN of aws\_service\_discovery\_service resource | string | `""` | no |
99-
| tags | A map of tags \(key-value pairs\) passed to resources. | map(string) | `{}` | no |
100-
| target\_group\_name | The name for the tasks target group | string | `""` | no |
101-
| task\_container\_assign\_public\_ip | Assigned public IP to the container. | bool | `"false"` | no |
102-
| task\_container\_command | The command that is passed to the container. | list(string) | `[]` | no |
103-
| task\_container\_environment | The environment variables to pass to a container. | map(string) | `{}` | no |
104-
| task\_container\_image | The image used to start a container. | string | n/a | yes |
105-
| task\_container\_port | The port number on the container that is bound to the user-specified or automatically assigned host port | number | n/a | yes |
106-
| task\_container\_protocol | Protocol that the container exposes. | string | `"HTTP"` | no |
107-
| task\_definition\_cpu | Amount of CPU to reserve for the task. | number | `"256"` | no |
108-
| task\_definition\_memory | The soft limit \(in MiB\) of memory to reserve for the container. | number | `"512"` | no |
109-
| task\_host\_port | The port number on the container instance to reserve for your container. | number | `"0"` | no |
110-
| volume | \(Optional\) A set of volume blocks that containers in your task may use. This is a list of maps, where each map should contain "name", "host\_path" and "docker\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs\_task\_definition.html | list | `[]` | no |
111-
| vpc\_id | The VPC ID. | string | n/a | yes |
83+
|------|-------------|------|---------|:-----:|
84+
| capacity\_provider\_strategy | (Optional) The capacity\_provider\_strategy configuration block. This is a list of maps, where each map should contain "capacity\_provider ", "weight" and "base" | `list` | `[]` | no |
85+
| cluster\_id | The Amazon Resource Name (ARN) that identifies the cluster. | `string` | n/a | yes |
86+
| container\_name | Optional name for the container to be used instead of name\_prefix. | `string` | `""` | no |
87+
| deployment\_controller\_type | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS. | `string` | `"ECS"` | no |
88+
| deployment\_maximum\_percent | The upper limit of the number of running tasks that can be running in a service during a deployment | `number` | `200` | no |
89+
| deployment\_minimum\_healthy\_percent | The lower limit of the number of running tasks that must remain running and healthy in a service during a deployment | `number` | `50` | no |
90+
| desired\_count | The number of instances of the task definitions to place and keep running. | `number` | `1` | no |
91+
| docker\_volume\_configuration | (Optional) Used to configure a docker volume option "docker\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html | `list` | `[]` | no |
92+
| health\_check | A health block containing health check settings for the target group. Overrides the defaults. | `map(string)` | n/a | yes |
93+
| health\_check\_grace\_period\_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers. | `number` | `300` | no |
94+
| lb\_arn | Arn for the LB for which the service should be attach to. | `string` | n/a | yes |
95+
| load\_balanced | Whether the task should be loadbalanced. | `bool` | `true` | no |
96+
| log\_retention\_in\_days | Number of days the logs will be retained in CloudWatch. | `number` | `30` | no |
97+
| logs\_kms\_key | The KMS key ARN to use to encrypt container logs. | `string` | `""` | no |
98+
| name\_prefix | A prefix used for naming resources. | `string` | n/a | yes |
99+
| placement\_constraints | (Optional) A set of placement constraints rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. This is a list of maps, where each map should contain "type" and "expression" | `list` | `[]` | no |
100+
| private\_subnet\_ids | A list of private subnets inside the VPC | `list(string)` | n/a | yes |
101+
| propogate\_tags | Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK\_DEFINITION. | `string` | `"TASK_DEFINITION"` | no |
102+
| proxy\_configuration | (Optional) The proxy configuration details for the App Mesh proxy. This is a list of maps, where each map should contain "container\_name", "properties" and "type" | `list` | `[]` | no |
103+
| repository\_credentials | name or ARN of a secrets manager secret (arn:aws:secretsmanager:region:aws\_account\_id:secret:secret\_name) | `string` | `""` | no |
104+
| repository\_credentials\_kms\_key | key id, key ARN, alias name or alias ARN of the key that encrypted the repository credentials | `string` | `"alias/aws/secretsmanager"` | no |
105+
| service\_registry\_arn | ARN of aws\_service\_discovery\_service resource | `string` | `""` | no |
106+
| tags | A map of tags (key-value pairs) passed to resources. | `map(string)` | `{}` | no |
107+
| target\_group\_name | The name for the tasks target group | `string` | `""` | no |
108+
| task\_container\_assign\_public\_ip | Assigned public IP to the container. | `bool` | `false` | no |
109+
| task\_container\_command | The command that is passed to the container. | `list(string)` | `[]` | no |
110+
| task\_container\_environment | The environment variables to pass to a container. | `map(string)` | `{}` | no |
111+
| task\_container\_image | The image used to start a container. | `string` | n/a | yes |
112+
| task\_container\_port | The port number on the container that is bound to the user-specified or automatically assigned host port | `number` | n/a | yes |
113+
| task\_container\_protocol | Protocol that the container exposes. | `string` | `"HTTP"` | no |
114+
| task\_definition\_cpu | Amount of CPU to reserve for the task. | `number` | `256` | no |
115+
| task\_definition\_memory | The soft limit (in MiB) of memory to reserve for the container. | `number` | `512` | no |
116+
| task\_health\_check | An optional healthcheck definition for the task | `object({ command = list(string), interval = number, timeout = number, retries = number, startPeriod = number })` | n/a | yes |
117+
| task\_host\_port | The port number on the container instance to reserve for your container. | `number` | `0` | no |
118+
| volume | (Optional) A set of volume blocks that containers in your task may use. This is a list of maps, where each map should contain "name", "host\_path" and "docker\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html | `list` | `[]` | no |
119+
| vpc\_id | The VPC ID. | `string` | n/a | yes |
112120

113121
## Outputs
114122

115123
| Name | Description |
116124
|------|-------------|
117125
| log\_group\_name | The name of the Cloudwatch log group for the task. |
118-
| service\_arn | The Amazon Resource Name \(ARN\) that identifies the ECS service. |
126+
| service\_arn | The Amazon Resource Name (ARN) that identifies the ECS service. |
119127
| service\_name | The name of the service. |
120-
| service\_sg\_id | The Amazon Resource Name \(ARN\) that identifies the service security group. |
128+
| service\_sg\_id | The Amazon Resource Name (ARN) that identifies the service security group. |
121129
| target\_group\_arn | The ARN of the Target Group used by Load Balancer. |
122130
| target\_group\_name | The Name of the Target Group used by Load Balancer. |
123-
| task\_role\_arn | The Amazon Resource Name \(ARN\) specifying the ECS service role. |
131+
| task\_role\_arn | The Amazon Resource Name (ARN) specifying the ECS service role. |
124132
| task\_role\_name | The name of the Fargate task service role. |
125133

126134
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ resource "aws_security_group_rule" "egress_service" {
7474
# Load Balancer Target group
7575
#####
7676
resource "aws_lb_target_group" "task" {
77+
count = var.load_balanced ? 1 : 0
7778
name = var.target_group_name != "" ? var.target_group_name : "${var.name_prefix}-target-${var.task_container_port}"
7879
vpc_id = var.vpc_id
7980
protocol = var.task_container_protocol
@@ -158,6 +159,15 @@ resource "aws_ecs_task_definition" "task" {
158159
"awslogs-stream-prefix": "container"
159160
}
160161
},
162+
%{if var.task_health_check != null~}
163+
"healthcheck": {
164+
"command": ${jsonencode(var.task_health_check.command)},
165+
"interval": ${var.task_health_check.interval},
166+
"timeout": ${var.task_health_check.timeout},
167+
"retries": ${var.task_health_check.retries},
168+
"startPeriod": ${var.task_health_check.startPeriod}
169+
},
170+
%{~endif}
161171
"command": ${jsonencode(var.task_container_command)},
162172
"environment": ${jsonencode(local.task_environment)}
163173
}]

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ output "service_arn" {
55

66
output "target_group_arn" {
77
description = "The ARN of the Target Group used by Load Balancer."
8-
value = aws_lb_target_group.task.arn
8+
value = concat(aws_lb_target_group.task[*].arn, [""])[0]
99
}
1010

1111
output "target_group_name" {
1212
description = "The Name of the Target Group used by Load Balancer."
13-
value = aws_lb_target_group.task.name
13+
value = concat(aws_lb_target_group.task[*].name, [""])[0]
1414
}
1515

1616
output "task_role_arn" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,9 @@ variable "docker_volume_configuration" {
200200
description = "(Optional) Used to configure a docker volume option \"docker_volume_configuration\". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html"
201201
default = []
202202
}
203+
204+
variable "task_health_check" {
205+
type = object({ command = list(string), interval = number, timeout = number, retries = number, startPeriod = number })
206+
description = "An optional healthcheck definition for the task"
207+
default = null
208+
}

0 commit comments

Comments
 (0)