Ricardo Peres
Ricardo Peres

Reputation: 14555

Running an ECS Task on a Schedule With .NET

I am currently using the IAmazonECS.RunTaskAsync API to run a task on ECS immediately. Now, I would like to postpone it to some point in the future. According to the documentation, it should be possible, but I haven't found a way to do it.

Upvotes: 1

Views: 507

Answers (1)

joaofs
joaofs

Reputation: 486

You can use CloudWatchEvents from Amazon.CloudWatchEvents namespace for that.

var putRuleRequest = new PutRuleRequest
{
   Name = "test",
   RoleArn = "IAM_ROLE_ARN",
   ScheduleExpression = "rate(5 minutes)",
   State = RuleState.ENABLED,
};

var putRuleResponse = client.PutRuleAsync(putRuleRequest).Result;

var putTargets = new PutTargetsRequest()
{
   Rule = putRuleResponse.RuleArn,
   Targets =
   {
       new Target
       {
           Arn = "cluster arn",
           RoleArn = "ecs events role",
           EcsParameters = new EcsParameters
           {
               TaskDefinitionArn = "arn for the task definition",
               TaskCount = 1
           }
       }
   }       
};

var response = await client.PutTargetsAsync(putTargets);

https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

Upvotes: 1

Related Questions