Skip to content

Commit

Permalink
Adding scheduler for ECS Tasks (#41)
Browse files Browse the repository at this point in the history
* Adding scheduler for ECS Tasks

* Running fmt

* terraform-docs: automated update action

---------

Co-authored-by: mvsnogueira-dnx <[email protected]>
  • Loading branch information
mvsnogueira-dnx and mvsnogueira-dnx authored Apr 2, 2023
1 parent 0030478 commit 488b522
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ In addition you have the option to create or not :
| dynamic\_stickiness | Target Group stickiness. Used in dynamic block. | `any` | `[]` | no |
| ecs\_service\_capacity\_provider\_strategy | (Optional) The capacity provider strategy to use for the service. Can be one or more. These can be updated without destroying and recreating the service only if set to [] and not changing from 0 capacity\_provider\_strategy blocks to greater than 0, or vice versa. | `list` | <pre>[<br> {}<br>]</pre> | no |
| efs\_mapping | A map of efs volume ids and paths to mount into the default task definition | `map(string)` | `{}` | no |
| enable\_schedule | Enables schedule to shut down and start up instances outside business hours. | `bool` | `false` | no |
| fargate\_spot | Set true to use FARGATE\_SPOT capacity provider by default (only when launch\_type=FARGATE) | `bool` | `false` | no |
| healthcheck\_interval | n/a | `string` | `"10"` | no |
| healthcheck\_matcher | The HTTP codes to use when checking for a successful response from a target | `number` | `200` | no |
Expand Down Expand Up @@ -134,6 +135,8 @@ In addition you have the option to create or not :
| port | Port for target group to listen | `number` | `80` | no |
| protocol | Protocol to use (HTTP or HTTPS) | `string` | `"HTTP"` | no |
| redirects | Map of path redirects to add to the listener | `map` | `{}` | no |
| schedule\_cron\_start | Cron expression to define when to trigger a start of the auto-scaling group. E.g. 'cron(00 21 ? \* SUN-THU \*)' to start at 8am UTC time. | `string` | `""` | no |
| schedule\_cron\_stop | Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 09 ? \* MON-FRI \*)' to start at 8am UTC time | `string` | `""` | no |
| security\_groups | The security groups associated with the task or service | `any` | `null` | no |
| service\_deployment\_maximum\_percent | Maximum percentage of tasks to run during deployments | `number` | `200` | no |
| service\_deployment\_minimum\_healthy\_percent | Minimum healthy percentage during deployments | `number` | `100` | no |
Expand Down
17 changes: 17 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,20 @@ variable "alarm_ecs_running_tasks_threshold" {
default = 0
description = "Alarm when the number of ecs service running tasks is lower than a certain value. CloudWatch Container Insights must be enabled for the cluster."
}

variable "enable_schedule" {
default = false
description = "Enables schedule to shut down and start up instances outside business hours."
}

variable "schedule_cron_start" {
type = string
default = ""
description = "Cron expression to define when to trigger a start of the auto-scaling group. E.g. 'cron(00 21 ? * SUN-THU *)' to start at 8am UTC time."
}

variable "schedule_cron_stop" {
type = string
default = ""
description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 09 ? * MON-FRI *)' to start at 8am UTC time"
}
30 changes: 30 additions & 0 deletions appautoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,33 @@ resource "aws_appautoscaling_policy" "scale_custom" {
}
}
}

resource "aws_appautoscaling_scheduled_action" "scale_service_out" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-scale-out"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = var.schedule_cron_stop
timezone = "UTC"

scalable_target_action {
min_capacity = 0
max_capacity = 0
}
}

resource "aws_appautoscaling_scheduled_action" "scale_service_in" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-scale-in"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = var.schedule_cron_start
timezone = "UTC"

scalable_target_action {
min_capacity = var.autoscaling_min
max_capacity = var.autoscaling_max
}
}

0 comments on commit 488b522

Please sign in to comment.