diff --git a/README.md b/README.md index 7e31978..c8f9a4e 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ module "start_ec2_instance" { documendb_schedule = "false" ec2_schedule = "true" ecs_schedule = "false" + ecs_task_desired_count = 1 rds_schedule = "false" redshift_schedule = "false" cloudwatch_alarm_schedule = "false" @@ -85,6 +86,7 @@ module "start_ec2_instance" { | documendb_schedule | Enable scheduling on documentdb resources | bool | `"false"` | no | | ec2_schedule | Enable scheduling on ec2 instance resources | bool | `"false"` | no | | ecs_schedule | Enable scheduling on ecs services resources | bool | `"false"` | no | +| ecs_task_desired_count | Number of desired ecs tasks to be launched | number | 1 | no | | rds_schedule | Enable scheduling on rds resources | bool | `"false"` | no | | redshift_schedule | Enable scheduling on redshift resources | bool | `"false"` | no | | cloudwatch_alarm_schedule | Enable scheduleding on cloudwatch alarm resources | bool | `"false"` | no | diff --git a/main.tf b/main.tf index 3a36b1b..b29a27a 100644 --- a/main.tf +++ b/main.tf @@ -277,6 +277,7 @@ resource "aws_lambda_function" "this" { DOCUMENTDB_SCHEDULE = tostring(var.documentdb_schedule) EC2_SCHEDULE = tostring(var.ec2_schedule) ECS_SCHEDULE = tostring(var.ecs_schedule) + ECS_TASK_DESIRED_COUNT = tostring(var.ecs_task_desired_count) RDS_SCHEDULE = tostring(var.rds_schedule) REDSHIFT_SCHEDULE = tostring(var.redshift_schedule) AUTOSCALING_SCHEDULE = tostring(var.autoscaling_schedule) diff --git a/package/scheduler/ecs_handler.py b/package/scheduler/ecs_handler.py index 293d89b..277ba70 100644 --- a/package/scheduler/ecs_handler.py +++ b/package/scheduler/ecs_handler.py @@ -1,5 +1,6 @@ """ecs service scheduler.""" +from os import getenv from typing import Dict, List import boto3 @@ -20,6 +21,7 @@ def __init__(self, region_name=None) -> None: else: self.ecs = boto3.client("ecs") self.tag_api = FilterByTags(region_name=region_name) + self.ecs_task_desired_count = int(getenv("ECS_TASK_DESIRED_COUNT")) def stop(self, aws_tags: List[Dict]) -> None: """Aws ecs instance stop function. @@ -72,7 +74,7 @@ def start(self, aws_tags: List[Dict]) -> None: cluster_name = service_arn.split("/")[-2] try: self.ecs.update_service( - cluster=cluster_name, service=service_name, desiredCount=1 + cluster=cluster_name, service=service_name, desiredCount=self.ecs_task_desired_count ) print(f"Start ECS Service {service_name} on Cluster {cluster_name}") except ClientError as exc: diff --git a/variables.tf b/variables.tf index 2b7063c..535a005 100644 --- a/variables.tf +++ b/variables.tf @@ -85,6 +85,12 @@ variable "ecs_schedule" { default = false } +variable "ecs_task_desired_count" { + description = "Define desired task count for ecs service" + type = number + default = 1 +} + variable "rds_schedule" { description = "Enable scheduling on rds resources" type = any