How do I scale my task count up or down in Amazon Elastic Container Service (Amazon ECS) at certain times of the day?

You can use time-based scaling to scale the task count of your Amazon ECS service up or down based on a time of day that you specify in a cron expression.

Create an Amazon CloudWatch Event rule that is triggered on the schedule defined in your cron expression. Be sure to create one rule for scaling up and another rule for scaling down. Also, create an AWS Lambda function that's invoked by your CloudWatch Event rule. This rule should update the task count of your Amazon ECS service based on your cron expression.

Create an AWS Identity and Access Management (IAM) role

Grant permission to the Lambda function so that it can update the desired count of the Amazon ECS service:

1.     Open the IAM console.

2.     In the navigation pane, choose Roles.

3.     Choose Create role, and then choose Lambda.

4.     Choose Next: Permissions.

5.     For Policy name, choose AWSLambdaBasicExecutionRole, and then choose Next:Tags.

6.     (Optional) Add a tag, and then choose Next:Review.

7.     For Role name, enter a name of your choice, and then choose Create role.

Attach an inline policy to your role

1.     Open the IAM console.

2.     In the navigation pane, choose Policies.

3.     Choose Create policy.

4.     Choose the JSON view, and then enter the following code:

    "Version": "2012-10-17",
    "Statement": [
            "Action": "ecs:UpdateService",
            "Resource": "*",
            "Effect": "Allow"

5.     Choose Review policy.

6.     For Name, enter a name for the policy.

7.     Choose Create policy.

Create a Lambda function

1.     Open the Lambda console, and then choose Create a function.

2.     For Name, enter a name of your choice.

3.     For Runtime, choose Python 2.7.

Note: Use only Python 2.7 for your runtime.

4.     For Role, choose the IAM role that you created earlier.

5.     Choose Create function.

6.     In the editor pane, enter the following:

import boto3
def handler(event, context):
    ecsregion = event['region']
    cluster_name = event['cluster']
    service_name = event['service']
    task_count = event['taskCount']
    ecs_client = boto3.client('ecs', region_name=ecsregion)
    response=ecs_client.update_service(cluster=cluster_name, service=service_name, desiredCount=int(task_count))

Create CloudWatch Event rules

Complete the following steps to create two separate CloudWatch Event rules: one rule for scaling up and another rule for scaling down.

1.     Open the CloudWatch console.

2.     Choose Rules, and then choose Create rule.

3.     Choose Cron expression, and then enter the time at which you want the CloudWatch Event rule to be triggered. For more information, see Cron Expressions.

4.     In the Targets section, choose Add target.

5.     Choose Lambda function.

6.     For Function, choose the Lambda function that you created earlier.

7.     Choose Configure input.

8.     Choose Constant (JSON text), and then enter the following input parameters: cluster, service, taskCount, and region. See the following example:

{ "cluster": "Cluster Name","service": "Service Name","taskCount": "2","region": "us-east-1"}

Note: The values for the input parameters are based on your unique requirements.

9.     Choose Configure details.

10.    Choose Update rule.

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2019-01-31