Skip to content

Commit a780ec9

Browse files
authored
Add support for container timeouts and volumes (#6)
* Add support for startTimeout and stopTimeout * Add support for efs volumes and mountPoints * update README
1 parent 713ca97 commit a780ec9

File tree

5 files changed

+116
-19
lines changed

5 files changed

+116
-19
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Module is to be used with Terraform > 0.12.
3636
## Examples
3737

3838
* [ECS Fargate Task Definition](https://github.com/umotif-public/terraform-aws-ecs-fargate-task-definition/tree/master/examples/core)
39+
* [ECS Fargate Task Definition with EFS](https://github.com/umotif-public/terraform-aws-ecs-fargate-task-definition/tree/master/examples/task-efs)
3940

4041
## Authors
4142

@@ -59,7 +60,6 @@ No requirements.
5960
| cloudwatch\_log\_group\_name | CloudWatch log group name required to enabled logDriver in container definitions for ecs task. | `string` | `""` | no |
6061
| container\_name | Optional name for the container to be used instead of name\_prefix. | `string` | `""` | no |
6162
| create\_repository\_credentials\_iam\_policy | Set to true if you are specifying `repository_credentials` variable, it will attach IAM policy with necessary permissions to task role. | `bool` | `false` | no |
62-
| 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 |
6363
| enabled | Whether to create the resources. Set to `false` to prevent the module from creating any resources | `bool` | `true` | no |
6464
| name\_prefix | A prefix used for naming resources. | `string` | n/a | yes |
6565
| 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 |
@@ -79,7 +79,10 @@ No requirements.
7979
| task\_definition\_memory | The soft limit (in MiB) of memory to reserve for the task. | `number` | `512` | no |
8080
| task\_health\_check | An optional healthcheck definition for the task | `object({ command = list(string), interval = number, timeout = number, retries = number, startPeriod = number })` | `null` | no |
8181
| task\_host\_port | The port number on the container instance to reserve for your container. | `number` | `0` | no |
82-
| 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 |
82+
| task\_mount\_points | The mount points for data volumes in your container. Each object inside the list requires "sourceVolume", "containerPath" and "readOnly". For more information see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html | `list(object({ sourceVolume = string, containerPath = string, readOnly = bool }))` | `null` | no |
83+
| task\_start\_timeout | Time duration (in seconds) to wait before giving up on resolving dependencies for a container. If this parameter is not specified, the default value of 3 minutes is used (fargate). | `number` | `null` | no |
84+
| task\_stop\_timeout | Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own. The max stop timeout value is 120 seconds and if the parameter is not specified, the default value of 30 seconds is used. | `number` | `null` | no |
85+
| 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", "docker\_volume\_configuration" and "efs\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html | `list` | `[]` | no |
8386

8487
## Outputs
8588

examples/core/main.tf

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ provider "aws" {
22
region = "eu-west-1"
33
}
44

5-
data "aws_kms_key" "secretsmanager_key" {
6-
key_id = "alias/aws/secretsmanager"
7-
}
5+
#####
6+
# Optional Secret creation for task credentials
7+
#####
88

9+
# data "aws_kms_key" "secretsmanager_key" {
10+
# key_id = "alias/aws/secretsmanager"
11+
# }
912

10-
resource "aws_secretsmanager_secret" "task_credentials" {
11-
name = "task_repository_credentials"
1213

13-
kms_key_id = data.aws_kms_key.secretsmanager_key.arn
14-
}
14+
# resource "aws_secretsmanager_secret" "task_credentials" {
15+
# name = "task_repository_credentials"
16+
17+
# kms_key_id = data.aws_kms_key.secretsmanager_key.arn
18+
# }
1519

1620
#####
1721
# task definition
@@ -37,7 +41,10 @@ module "ecs-task-definition" {
3741
cloudwatch_log_group_name = "/test-cloudwatch/log-group"
3842
task_container_command = ["/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""]
3943

40-
create_repository_credentials_iam_policy = true
41-
repository_credentials = aws_secretsmanager_secret.task_credentials.arn # also set create_repository_credentials_iam_policy = true
44+
task_stop_timeout = 90
45+
46+
### uncomment the following lines to use private repository credentials
47+
# create_repository_credentials_iam_policy = true
48+
# repository_credentials = aws_secretsmanager_secret.task_credentials.arn # also set create_repository_credentials_iam_policy = true
4249
}
4350

examples/task-efs/main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
resource "aws_efs_file_system" "efs" {
6+
creation_token = "efs-html"
7+
8+
tags = {
9+
Name = "efs-html"
10+
}
11+
}
12+
13+
#####
14+
# task definition
15+
#####
16+
module "ecs-task-definition" {
17+
source = "../.."
18+
19+
enabled = true
20+
name_prefix = "test-container"
21+
task_container_image = "httpd:2.4"
22+
23+
container_name = "test-container-name"
24+
task_container_port = "80"
25+
task_host_port = "80"
26+
27+
task_definition_cpu = "512"
28+
task_definition_memory = "1024"
29+
30+
task_container_environment = {
31+
"ENVIRONEMNT" = "Test"
32+
}
33+
34+
cloudwatch_log_group_name = "/test-cloudwatch/log-group"
35+
task_container_command = ["/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""]
36+
37+
task_stop_timeout = 90
38+
39+
task_mount_points = [
40+
{
41+
"sourceVolume" = aws_efs_file_system.efs.creation_token,
42+
"containerPath" = "/usr/share/nginx/html",
43+
"readOnly" = true
44+
}
45+
]
46+
47+
volume = [
48+
{
49+
name = "efs-html",
50+
efs_volume_configuration = [
51+
{
52+
"file_system_id" : aws_efs_file_system.efs.id,
53+
"root_directory" : "/usr/share/nginx"
54+
}
55+
]
56+
}
57+
]
58+
}

main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ resource "aws_ecs_task_definition" "task" {
124124
%{if var.task_container_cpu != null~}
125125
"cpu": ${var.task_container_cpu},
126126
%{~endif}
127+
%{if var.task_start_timeout != null~}
128+
"startTimeout": ${var.task_start_timeout},
129+
%{~endif}
130+
%{if var.task_stop_timeout != null~}
131+
"stopTimeout": ${var.task_stop_timeout},
132+
%{~endif}
133+
%{if var.task_mount_points != null~}
134+
"mountPoints": ${jsonencode(var.task_mount_points)},
135+
%{~endif}
127136
"environment": ${jsonencode(local.task_environment)}
128137
}]
129138
EOF
@@ -152,7 +161,7 @@ EOF
152161
host_path = lookup(volume.value, "host_path", null)
153162

154163
dynamic "docker_volume_configuration" {
155-
for_each = var.docker_volume_configuration
164+
for_each = lookup(volume.value, "docker_volume_configuration", [])
156165
content {
157166
scope = lookup(docker_volume_configuration.value, "scope", null)
158167
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
@@ -161,6 +170,14 @@ EOF
161170
labels = lookup(docker_volume_configuration.value, "labels", null)
162171
}
163172
}
173+
174+
dynamic "efs_volume_configuration" {
175+
for_each = lookup(volume.value, "efs_volume_configuration", [])
176+
content {
177+
file_system_id = lookup(efs_volume_configuration.value, "file_system_id", null)
178+
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
179+
}
180+
}
164181
}
165182
}
166183

variables.tf

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,7 @@ variable "proxy_configuration" {
124124

125125
variable "volume" {
126126
type = list
127-
description = "(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"
128-
default = []
129-
}
130-
131-
variable "docker_volume_configuration" {
132-
type = list
133-
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"
127+
description = "(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\", \"docker_volume_configuration\" and \"efs_volume_configuration\". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html"
134128
default = []
135129
}
136130

@@ -139,3 +133,21 @@ variable "task_health_check" {
139133
description = "An optional healthcheck definition for the task"
140134
default = null
141135
}
136+
137+
variable "task_start_timeout" {
138+
type = number
139+
description = "Time duration (in seconds) to wait before giving up on resolving dependencies for a container. If this parameter is not specified, the default value of 3 minutes is used (fargate)."
140+
default = null
141+
}
142+
143+
variable "task_stop_timeout" {
144+
type = number
145+
description = "Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own. The max stop timeout value is 120 seconds and if the parameter is not specified, the default value of 30 seconds is used."
146+
default = null
147+
}
148+
149+
variable "task_mount_points" {
150+
description = "The mount points for data volumes in your container. Each object inside the list requires \"sourceVolume\", \"containerPath\" and \"readOnly\". For more information see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html "
151+
type = list(object({ sourceVolume = string, containerPath = string, readOnly = bool }))
152+
default = null
153+
}

0 commit comments

Comments
 (0)