Managing Cron Jobs with AWS ECS Scheduled Tasks

Photo by JJ Ying on Unsplash

Managing Cron Jobs with AWS ECS Scheduled Tasks

·

4 min read

Cron jobs are an essential component of any modern application that requires scheduled tasks to be executed at specific intervals. However, managing cron jobs can be a tedious task, especially when the application scales up. Amazon Web Services (AWS) Elastic Container Service (ECS) can be used to schedule tasks that require cron jobs. In this blog post, we will explore how AWS ECS scheduled tasks can be used for things that require cron jobs.

AWS ECS Scheduled Tasks: AWS ECS Scheduled Tasks is a feature that allows users to run tasks on a scheduled basis. These tasks can be defined in a JSON or YAML file and can be scheduled to run at a specific time or on a recurring basis. The tasks are executed by a task scheduler, which runs in the ECS service. The task scheduler is responsible for launching new tasks and stopping existing tasks based on the schedule.

AWS ECS Scheduled Tasks are implemented using AWS CloudFormation, which is an Infrastructure-as-Code (IaC) tool that enables you to define your infrastructure as code. Using AWS CloudFormation, you can define your AWS resources as a collection of templates, which can be versioned and stored in a source code repository.

Sample Code: Let's consider an example where we need to run a task on a scheduled basis. In this example, we will run a task that sends an email every day at 10:00 AM using AWS Simple Email Service (SES). To accomplish this, we will create an AWS ECS scheduled task that will execute a Docker container at the specified time.

Step 1: Create an AWS CloudFormation Template Create a new CloudFormation stack and use the following code to define the ECS task definition, CloudWatch Event Rule and CloudWatch Event Target.

Resources:
  EmailTask:
    Type: 'AWS::ECS::TaskDefinition'
    Properties:
      Family: 'email-task'
      NetworkMode: awsvpc
      ContainerDefinitions:
        - Name: 'email-task-container'
          Image: 'my-email-task-image:latest'
          Essential: true
          Environment:
            - Name: 'AWS_REGION'
              Value: !Ref 'AWS::Region'
          Command: ['/usr/local/bin/python3', 'send_email.py']
          LogConfiguration:
            LogDriver: 'awslogs'
            Options:
              awslogs-group: !Ref 'LogGroup'
              awslogs-region: !Ref 'AWS::Region'
              awslogs-stream-prefix: 'ecs'
      RequiresCompatibilities:
        - 'FARGATE'
        - 'EC2'

  ScheduledTaskRule:
    Type: 'AWS::Events::Rule'
    Properties:
      Description: 'Schedule for Email Task'
      ScheduleExpression: 'cron(0 10 * * ? *)'
      State: 'ENABLED'
      Targets:
        - Id: 'EmailTaskTarget'
          Arn: !GetAtt EmailTask.Arn

  ScheduledTaskPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !GetAtt 'EmailTask.Arn'
      Principal: 'events.amazonaws.com'
      SourceArn: !GetAtt ScheduledTaskRule.Arn

  LogGroup:
    Type: 'AWS::Logs::LogGroup'
    Properties:
      LogGroupName: 'email-task-logs'

Step 2: Create the Task Container Image Create the Docker container image that will be used to run the email task. In this example, we will create a Python script that uses the AWS SDK for Python (Boto3) to send an email using AWS SES. Here's an example script:

import boto3

def send_email():
    client = boto3.client('ses', region_name='us-west-2')
    response = client.send_email(
        Destination={
            'ToAddresses': [
                'recipient@example.com',
            ],
        },
        Message={
            'Body': {
                'Text': {
                    'Charset': 'UTF-8',
                    'Data': 'This is a test email.',
                },
            },
            'Subject': {
                'Charset': 'UTF-8',
                'Data': 'Test Email',
            },
        },
        Source='sender@example.com',
    )

    return response

Step 3: Create the AWS ECS Task Definition Next, we will create an ECS task definition using the CloudFormation template. The task definition specifies the container image, environment variables, command, and other parameters required to run the task.

Step 4: Create the AWS ECS Cluster After creating the task definition, we need to create an ECS cluster where the task will be executed. The cluster can be created using the AWS Management Console or using AWS CloudFormation.

Step 5: Test the Scheduled Task Once the AWS ECS cluster is created, the scheduled task will be automatically executed at the specified time. You can check the logs in CloudWatch Logs to ensure that the task was executed successfully.

Conclusion: In this blog post, we have explored how AWS ECS scheduled tasks can be used for things that require cron jobs. Using AWS ECS scheduled tasks, you can easily schedule and manage tasks that need to be executed on a scheduled basis. With the help of the sample code provided in this blog post, you can create your own scheduled tasks and manage them using AWS CloudFormation. AWS ECS scheduled tasks offer a simple and scalable solution for managing cron jobs in your applications