Skip to content

Commit 2253c1f

Browse files
authored
Add support for additional variables and configuration options. (#12)
1 parent 6350f2d commit 2253c1f

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.25.0
21+
rev: v1.29.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
8888
| 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 |
8989
| 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 |
9090
| 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 |
9291
| health\_check | A health block containing health check settings for the target group. Overrides the defaults. | `map(string)` | n/a | yes |
9392
| 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 |
9493
| lb\_arn | Arn for the LB for which the service should be attach to. | `string` | n/a | yes |
@@ -97,6 +96,7 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
9796
| logs\_kms\_key | The KMS key ARN to use to encrypt container logs. | `string` | `""` | no |
9897
| name\_prefix | A prefix used for naming resources. | `string` | n/a | yes |
9998
| 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 |
99+
| platform\_version | The platform version on which to run your service. Only applicable for launch\_type set to FARGATE. | `string` | `"LATEST"` | no |
100100
| private\_subnet\_ids | A list of private subnets inside the VPC | `list(string)` | n/a | yes |
101101
| 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 |
102102
| 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 |
@@ -107,15 +107,19 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
107107
| target\_group\_name | The name for the tasks target group | `string` | `""` | no |
108108
| task\_container\_assign\_public\_ip | Assigned public IP to the container. | `bool` | `false` | no |
109109
| task\_container\_command | The command that is passed to the container. | `list(string)` | `[]` | no |
110+
| task\_container\_cpu | Amount of CPU to reserve for the container. | `number` | n/a | yes |
110111
| task\_container\_environment | The environment variables to pass to a container. | `map(string)` | `{}` | no |
111112
| task\_container\_image | The image used to start a container. | `string` | n/a | yes |
113+
| task\_container\_memory | The hard limit (in MiB) of memory for the container. | `number` | n/a | yes |
114+
| task\_container\_memory\_reservation | The soft limit (in MiB) of memory to reserve for the container. | `number` | n/a | yes |
112115
| 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 |
113116
| task\_container\_protocol | Protocol that the container exposes. | `string` | `"HTTP"` | no |
117+
| task\_container\_working\_directory | The working directory to run commands inside the container. | `string` | `""` | no |
114118
| 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 |
119+
| task\_definition\_memory | The soft limit (in MiB) of memory to reserve for the task. | `number` | `512` | no |
116120
| 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 |
117121
| 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 |
122+
| 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 |
119123
| vpc\_id | The VPC ID. | `string` | n/a | yes |
120124

121125
## Outputs

examples/core/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ module "fargate" {
8383
lb_arn = module.alb.arn
8484
cluster_id = aws_ecs_cluster.cluster.id
8585

86+
platform_version = "1.4.0" # defaults to LATEST
87+
8688
task_container_image = "marcincuber/2048-game:latest"
8789
task_definition_cpu = 256
8890
task_definition_memory = 512

main.tf

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,26 @@ resource "aws_ecs_task_definition" "task" {
161161
},
162162
%{if var.task_health_check != null~}
163163
"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}
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}
169169
},
170170
%{~endif}
171171
"command": ${jsonencode(var.task_container_command)},
172+
%{if var.task_container_working_directory != ""~}
173+
"workingDirectory": ${var.task_container_working_directory},
174+
%{~endif}
175+
%{if var.task_container_memory != null~}
176+
"memory": ${var.task_container_memory},
177+
%{~endif}
178+
%{if var.task_container_memory_reservation != null~}
179+
"memoryReservation": ${var.task_container_memory_reservation},
180+
%{~endif}
181+
%{if var.task_container_cpu != null~}
182+
"cpu": ${var.task_container_cpu},
183+
%{~endif}
172184
"environment": ${jsonencode(local.task_environment)}
173185
}]
174186
EOF
@@ -197,7 +209,7 @@ EOF
197209
host_path = lookup(volume.value, "host_path", null)
198210

199211
dynamic "docker_volume_configuration" {
200-
for_each = var.docker_volume_configuration
212+
for_each = lookup(volume.value, "docker_volume_configuration", [])
201213
content {
202214
scope = lookup(docker_volume_configuration.value, "scope", null)
203215
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
@@ -206,6 +218,14 @@ EOF
206218
labels = lookup(docker_volume_configuration.value, "labels", null)
207219
}
208220
}
221+
222+
dynamic "efs_volume_configuration" {
223+
for_each = lookup(volume.value, "efs_volume_configuration", [])
224+
content {
225+
file_system_id = lookup(efs_volume_configuration.value, "file_system_id", null)
226+
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
227+
}
228+
}
209229
}
210230
}
211231

@@ -218,13 +238,17 @@ EOF
218238
}
219239

220240
resource "aws_ecs_service" "service" {
221-
depends_on = [null_resource.lb_exists]
222-
name = var.name_prefix
223-
cluster = var.cluster_id
224-
task_definition = aws_ecs_task_definition.task.arn
225-
desired_count = var.desired_count
226-
propagate_tags = var.propogate_tags
227-
launch_type = length(var.capacity_provider_strategy) == 0 ? "FARGATE" : null
241+
name = var.name_prefix
242+
243+
cluster = var.cluster_id
244+
task_definition = aws_ecs_task_definition.task.arn
245+
246+
desired_count = var.desired_count
247+
propagate_tags = var.propogate_tags
248+
249+
platform_version = var.platform_version
250+
launch_type = length(var.capacity_provider_strategy) == 0 ? "FARGATE" : null
251+
228252
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
229253
deployment_maximum_percent = var.deployment_maximum_percent
230254
health_check_grace_period_seconds = var.load_balanced ? var.health_check_grace_period_seconds : null
@@ -272,6 +296,8 @@ resource "aws_ecs_service" "service" {
272296
Name = "${var.name_prefix}-service"
273297
},
274298
)
299+
300+
depends_on = [null_resource.lb_exists]
275301
}
276302

277303
# HACK: The workaround used in ecs/service does not work for some reason in this module, this fixes the following error:

variables.tf

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ variable "cluster_id" {
2424
type = string
2525
}
2626

27+
variable "platform_version" {
28+
description = "The platform version on which to run your service. Only applicable for launch_type set to FARGATE."
29+
default = "LATEST"
30+
}
31+
2732
variable "task_container_image" {
2833
description = "The image used to start a container."
2934
type = string
@@ -70,7 +75,7 @@ variable "task_definition_cpu" {
7075
}
7176

7277
variable "task_definition_memory" {
73-
description = "The soft limit (in MiB) of memory to reserve for the container."
78+
description = "The soft limit (in MiB) of memory to reserve for the task."
7479
default = 512
7580
type = number
7681
}
@@ -191,18 +196,36 @@ variable "proxy_configuration" {
191196

192197
variable "volume" {
193198
type = list
194-
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"
195-
default = []
196-
}
197-
198-
variable "docker_volume_configuration" {
199-
type = list
200-
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"
199+
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"
201200
default = []
202201
}
203202

204203
variable "task_health_check" {
205204
type = object({ command = list(string), interval = number, timeout = number, retries = number, startPeriod = number })
206205
description = "An optional healthcheck definition for the task"
207206
default = null
208-
}
207+
}
208+
209+
variable "task_container_cpu" {
210+
description = "Amount of CPU to reserve for the container."
211+
default = null
212+
type = number
213+
}
214+
215+
variable "task_container_memory" {
216+
description = "The hard limit (in MiB) of memory for the container."
217+
default = null
218+
type = number
219+
}
220+
221+
variable "task_container_memory_reservation" {
222+
description = "The soft limit (in MiB) of memory to reserve for the container."
223+
default = null
224+
type = number
225+
}
226+
227+
variable "task_container_working_directory" {
228+
description = "The working directory to run commands inside the container."
229+
default = ""
230+
type = string
231+
}

0 commit comments

Comments
 (0)