Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule ECS tasks on AWS Fargate

I have created a Task Definition on Elastic Container Service and have successfully run it in a Fargate cluster. However when I create a Scheduled Task in said cluster the option for "Launch Type" is hardcoded to EC2. Is there a way, perhaps through the command line to schedule the task to run on Fargate?

enter image description here

like image 337
Jared S Avatar asked Dec 06 '17 18:12

Jared S


People also ask

How do you schedule a fargate task?

AWS Fargate tasks can be scheduled through the Fargate console, the CloudWatch Events console, and the AWS CLI. You can also now set AWS Fargate as an ECS task target in CloudWatch Events, allowing you to launch tasks in response to changes that happen in your AWS resources.

How do I run an ECS scheduled task manually?

Open the Amazon ECS console at https://console.aws.amazon.com/ecs/ . In the navigation pane, choose Task Definitions and select the task definition to run. To run the latest revision of a task definition, select the box to the left of the task definition to run.

Are fargate tasks always running?

When used this way, tasks are persistent -- your containers will continue to run even if no requests are received, even when running on Fargate. Since your containers are always running, there is no warmup time caused by Fargate.


4 Answers

Heads up ! This is now supported in AWS:

https://aws.amazon.com/about-aws/whats-new/2018/08/aws-fargate-now-supports-time-and-event-based-task-scheduling/

Although not in some regions - As at Apr-19 it still wasn't supported in EU-west-2 (London). Check the table at the top of this page to see if it's supported in the region you want: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduled_tasks.html

like image 171
David Lin Avatar answered Nov 02 '22 00:11

David Lin


There seem to be no way of scheduling a task on FARGATE.

Only way it can be done right now seems to be by having your 'scheduler' external to ECS. I did it with a lambda. You can also use something like a jenkins or a simple cron task that fires the aws-cli command to ECS, in both these cases though you will need an instance always running.

I wrote a lambda that accepts the params (overrides) to be sent to the ECS task and has the schedule the task was supposed to have.

Update: It seems there is a schedule tab in FARGATE Cluster details now that will allow you set cron schedules on ECS tasks.

like image 20
NikhilWanpal Avatar answered Nov 01 '22 23:11

NikhilWanpal


While the AWS Documentation gives you ways to do this through CloudFormation, it seems like they've not yet released this feature anyway. I have been trying to do something similar and ran into the same issue. Once it does become available, this link from the aws docs should be useful. Here's how they suggest doing it, but I keep running into errors saying NetworkConfiguration is not recognized and LaunchType is not recognized.

 "EcsParameters": { 
        "Group": "string",
        "LaunchType": "string",
        "NetworkConfiguration": { 
           "awsvpcConfiguration": { 
              "AssignPublicIp": "string",
              "SecurityGroups": [ "string" ],
              "Subnets": [ "string" ]
           }
        },

Update: Here is an alternative that did end up working for me through the aws events put-targets command on the aws cli!

Make sure your aws cli is up to date. This method fails for older versions of the cli. run this to update: pip install awscli --upgrade --user

After that, you should be good to go. Use the aws events put-targets --rule <value> --targets <value> command. Make sure that before you run this command you have a rule already defined on your cluster. If not, you can do that with the aws events put-rule cmd too. Refer to the AWS docs for put-rule, and for put-targets.

An example of a rule from the documentation is given below:

aws events put-rule --name "DailyLambdaFunction" --schedule-expression "cron(0 9 * * ? *)"

The put-targets command that worked for me is this:

aws events put-targets --rule cli-RS-rule --targets '{"Arn": "arn:aws:ecs:1234/cluster/clustername","EcsParameters": {"LaunchType": "FARGATE","NetworkConfiguration": {"awsvpcConfiguration": {"AssignPublicIp": "ENABLED", "SecurityGroups": [ "sg-id1233" ], "Subnets": [ "subnet-1234" ] }},"TaskCount": 1,"TaskDefinitionArn": "arn:aws:ecs:1234:task-definition/taskdef"},"Id": "sampleID111","RoleArn": "arn:aws:iam:1234:role/eventrole"}'
like image 20
tanvi Avatar answered Nov 02 '22 01:11

tanvi


You can create a CloudWatch rule that uses a schedule as the event source and an ESC task as the target.

like image 32
André Avatar answered Nov 02 '22 00:11

André