Skip to content

Commit

Permalink
Add validation on ecs task size variable (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsjc authored Nov 9, 2023
1 parent 3f4faf6 commit 1b4e8d6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion terraform/modules/services/nwp_consumer/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,50 @@ variable ecs-task_type {
default = "task"
}

variable ecs-task_size {

type = object({
cpu = number
memory = number
})

description = <<EOT
ecs-task_size = {
cpu : "CPU units for the ECS task"
memory : "Memory units (MB) for the ECS task"
}
ecs-task_size: "Size of the ECS task in terms of compute and memory"
EOT

default = {
cpu = 1024
memory = 5120
}

validation {
condition = length(keys(var.ecs-task_size)) == 2
error_message = "Variable ecs-task_size must have exactly two keys: cpu and memory."
}
validation {
condition = contains([256, 512, 1024, 2048, 4096, 8192], var.ecs-task_size.cpu)
error_message = "CPU must be one of 256, 512, 1024, 2048, 4096, or 8192."
}
validation {
// See https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#data
// for valid CPU and memory values.
condition = (
var.ecs-task_size.cpu == 256 ? contains([512, 1024, 2048], var.ecs-task_size.memory) :
var.ecs-task_size.cpu == 512 ? contains(range(1024, 4096, 1024), var.ecs-task_size.memory) :
var.ecs-task_size.cpu == 1024 ? contains(range(2048, 8192, 1024), var.ecs-task_size.memory) :
var.ecs-task_size.cpu == 2048 ? contains(range(4096, 16384, 1024), var.ecs-task_size.memory) :
var.ecs-task_size.cpu == 4096 ? contains(range(8192, 30720, 1024), var.ecs-task_size.memory) :
var.ecs-task_size.cpu == 8192 ? contains(range(16384, 61440, 4096), var.ecs-task_size.memory) :
true
)
error_message = "Invalid combination of CPU and memory."
}
}

variable ecs-task_cpu {
type = number
description = "CPU units for the ECS task"
Expand All @@ -95,6 +139,6 @@ variable ecs-task_cpu {
variable ecs-task_memory {
type = number
description = "Memory units (MB) for the ECS task"
default = 5012
default = 5120
}

0 comments on commit 1b4e8d6

Please sign in to comment.