How to schedule ECS Services in AWS easily

This is my solution which is highly based on a great AWS employee (Alfredo J).

Most likely if you are reading this article, you might know this is not something possible to do without a workaround in AWS. You might think of using a scheduled task or…


This content originally appeared on DEV Community and was authored by Federico Navarrete

This is my solution which is highly based on a great AWS employee (Alfredo J).

Most likely if you are reading this article, you might know this is not something possible to do without a workaround in AWS. You might think of using a scheduled task or complex solutions but after a while, Alfredo from Mexico supported me to bring this solution to all of you.

First, you need to create a Lambda function and add this Python script:

import json
import boto3
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

client = boto3.client('ecs')

def lambda_handler(event, context):
    cluster = event["cluster"]
    service_names = event["service_names"]
    service_desired_count = int(event["service_desired_count"])

    for service_name in service_names.split(","):
        response = client.update_service(
            cluster=cluster,
            service=service_name,
            desiredCount=service_desired_count
            )

        logger.info("Updated {0} service in {1} cluster with desire count set to {2} tasks".format(service_name, cluster, service_desired_count))

    return {
        'statusCode': 200,
        'new_desired_count': service_desired_count
    }

The script expects the following variables in a JSON format:

{
  "cluster": "clusterName",
  "service_names": "service1,service2",
  "service_desired_count": "0"
}

Where:

  • cluster is the name of the cluster you want to modify.
  • service_names is an array for the collection of services.
  • service_desired_count is the number of desired services. 0 is to stop the service/s, any other number is to start the service/s.

After everything is created you need to create some rules in Amazon EventBridge (formerly, CloudWatch Events). Here, you define the event you want to trigger based on the schedule that you expect. This is an example:

aws example

If something fails, you need to double-check that the created IAM role has the required policies like: ecs:UpdateService. You can check this from the logs.


This content originally appeared on DEV Community and was authored by Federico Navarrete


Print Share Comment Cite Upload Translate Updates
APA

Federico Navarrete | Sciencx (2021-07-28T13:52:58+00:00) How to schedule ECS Services in AWS easily. Retrieved from https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/

MLA
" » How to schedule ECS Services in AWS easily." Federico Navarrete | Sciencx - Wednesday July 28, 2021, https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/
HARVARD
Federico Navarrete | Sciencx Wednesday July 28, 2021 » How to schedule ECS Services in AWS easily., viewed ,<https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/>
VANCOUVER
Federico Navarrete | Sciencx - » How to schedule ECS Services in AWS easily. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/
CHICAGO
" » How to schedule ECS Services in AWS easily." Federico Navarrete | Sciencx - Accessed . https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/
IEEE
" » How to schedule ECS Services in AWS easily." Federico Navarrete | Sciencx [Online]. Available: https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/. [Accessed: ]
rf:citation
» How to schedule ECS Services in AWS easily | Federico Navarrete | Sciencx | https://www.scien.cx/2021/07/28/how-to-schedule-ecs-services-in-aws-easily/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.