All Articles

ECS Scheduled Tasks

EDIT: If you’re looking for an automated way for scheduled tasks on ECS check here

This one provides an alternative to scheduled tasks for applications running ECS Fargate.

To schedule a task to run at a specific time you can use CloudWatch events (e.g. data collection, analyze payments, generate some report).

On the AWS console, it looks something like the image below. ./scheduled.png

You can use the containerOverrides command to make run the specific task and scheduled it with a regular cron expression.

However, if your application is running on (us-west-1, ap-south-1, ap-northeast-2, and eu-west-2) this is still not supported.

Click here for more information

A possible workaround (not so elegant though), is to use AWS Lambda to trigger the task for you.

The gist below is adapted for a rails application, that I’ve built in Python for the simplicity of having boto3 already included on Lambda. Feel free to adapt it to your needs.

The gist below is adapted for a rails application, that I’ve built in Python for the simplicity of having boto3 already included on Lambda. Feel free to adapt it to your needs.

import json
import boto3

def lambda_handler(event, context):
    client = boto3.client('ecs')
    c = client.describe_services(cluster=event['cluster'], services=['service'])
    subnets = c['services'][0]['networkConfiguration']['awsvpcConfiguration']['subnets']
    securityGroups= c['services'][0]['networkConfiguration']['awsvpcConfiguration']['securityGroups']

    response = client.run_task(
    cluster=event['cluster'],
    taskDefinition=event['cluster'],
    overrides={
        'containerOverrides': [
            {
                'name': 'RailsContainer',
                "command": [
                    "bundle",
                    "exec",
                    "rake",
                    event['task']
                ]
            },
        ],
    },
    launchType='FARGATE',
    networkConfiguration={
        'awsvpcConfiguration': {
            'subnets': subnets,
            'securityGroups': securityGroups,
            'assignPublicIp': 'ENABLED'
        }
    })

    return {
        'statusCode': 200,
        'body': json.dumps('Scheduled!')
    }

Now that the lambda is assembled you can trigger it to run it with CloudWatch events. ./watchlambda.png

And there you have it, an alternative to run scheduled tasks while AWS doesn’t provide support for all regions on Fargate.

So what did you think of it? Hit me up on Twitter with your thoughts and opinions.