Skip to content

Commit

Permalink
Merge pull request #23 from DNXLabs/add_schedulle_on_off
Browse files Browse the repository at this point in the history
add_schedulle_on_off
  • Loading branch information
maurofrigini-dnx authored Nov 21, 2024
2 parents 1278e4a + 34fc264 commit d9ed67b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ In addition you have the option to create or not :
| create\_iam\_codedeployrole | Create Codedeploy IAM Role for ECS or not. | `bool` | `true` | no |
| deployment\_controller | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS, EXTERNAL. | `string` | `"CODE_DEPLOY"` | no |
| efs\_mapping | A map of efs volume ids and paths to mount into the default task definition | `map(string)` | `{}` | no |
| enable\_schedule | Enable scheduling for ECS service | `bool` | `false` | no |
| fargate\_spot | Set true to use FARGATE\_SPOT capacity provider by default (only when launch\_type=FARGATE) | `bool` | `false` | no |
| hosted\_zone | Hosted Zone to create DNS record for this app | `string` | `""` | no |
| hostname | Hostname to create DNS record for this app | `string` | `""` | no |
Expand All @@ -97,6 +98,9 @@ In addition you have the option to create or not :
| placement\_constraints | Rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. | <pre>list(object({<br> type = string<br> expression = string<br> }))</pre> | `[]` | no |
| port | Port for target group to listen | `string` | `"80"` | no |
| ports | Port for target group to listen | <pre>list(object({<br> port = number<br> protocol = string<br> }))</pre> | <pre>[<br> {<br> "port": 80,<br> "protocol": "tcp"<br> }<br>]</pre> | no |
| schedule\_cron\_start | Cron expression to start the ECS service | `string` | `""` | no |
| schedule\_cron\_stop | Cron expression to stop the ECS service | `string` | `""` | no |
| schedule\_timezone | Timezone for the scheduled actions | `string` | `"UTC"` | no |
| security\_group\_ecs\_nodes\_inbound\_cidrs | ECS Nodes inbound allowed CIDRs for the security group. | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
| security\_group\_nlb\_inbound\_cidrs | NLB inbound allowed CIDRs for the security group. | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
| security\_groups | The security groups associated with the task or service | `any` | `null` | no |
Expand Down
24 changes: 24 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,27 @@ variable "codedeploy_deployment_config_name" {
default = "CodeDeployDefault.ECSAllAtOnce"
description = "Specifies the deployment configuration for CodeDeploy"
}

variable "enable_schedule" {
description = "Enable scheduling for ECS service"
type = bool
default = false
}

variable "schedule_cron_start" {
description = "Cron expression to start the ECS service"
type = string
default = ""
}

variable "schedule_cron_stop" {
description = "Cron expression to stop the ECS service"
type = string
default = ""
}

variable "schedule_timezone" {
description = "Timezone for the scheduled actions"
type = string
default = "UTC"
}
30 changes: 30 additions & 0 deletions appautoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ resource "aws_appautoscaling_policy" "scale_cpu" {
}
}
}

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 d9ed67b

Please sign in to comment.