Creating a scheduled task using the Amazon CLI
This topic describes how to create a scheduled task using the Amazon CLI. The scheduled task is created using the CloudWatch Events API. For more information, see What is Amazon CloudWatch Events? in the Amazon CloudWatch Events User Guide.
Complete the following prerequisites:
-
Set up an Amazon account and an ecsEventsRole associated with your account.
-
Install and configure the Amazon CLI version 2. For more information, see Installing the Amazon CLI version 2 and Amazon Command Line Interface.
-
A registered task definition. If you haven't yet created and registered a task definition, see Getting started with the console using Linux containers on Amazon Fargate.
-
An Amazon EC2 Linux instance running on your default ECS cluster. For instructions on how to create these resources.
Before you verify the scheduling results, make sure that the cluster isn't running a service or task. From the ECS console, delete the cluster tasks and service before trying the example.
To create a scheduled task (Amazon CLI)
-
Create the CloudWatch Events rule. This example creates a rule named
MyRule1
that's started every day at 12:00pm UTC. You can change the time so that it's more convenient for verifying the schedule results. The first time placeholder is minutes and the second placeholder is UTC hours. For other examples of rule expressions, see Schedule Expressions for Rules in the Amazon CloudWatch Events User Guide.aws events put-rule \ --schedule-expression "
cron(0 12 * * ? *)
" --nameMyRule1
-
Add the details of your Amazon ECS cluster and task definition as a target for the CloudWatch Events rule. Specify the cluster and task definition using the full Amazon Resource Name (ARN). The launch type and network configuration must be defined either in the task definition or the
put-targets
command line. When using Fargate, the network configuration must be defined asawsvpc
.In this example, the target is defined as the
default
cluster in which to run a Fargate task based on thefirst-run-task-definition:1
task definition. A count of one task is scheduled to run according toMyRule1
. AnecsEventsRole
IAM role is assigned to the target. The launch type isFARGATE
and the network configuration is defined asawsvpc
with a security groups and a public subnet. The command is run from the ECS instance in the default cluster. For more information aboutput-targets
, see put-targets. The cluster and task definition must already be created. Otherwise, you receive an error. Create a local file named
scheduledtask.json
with the following contents:[{ "Id": "1", "Arn": "arn:aws:ecs:
us-east-1
:123456789012
:cluster/default", "RoleArn": "arn:aws:iam::123456789012
:role/ecsEventsRole
", "EcsParameters": { "TaskDefinitionArn": "arn:aws:ecs:us-east-1
:123456789012
:task-definition/first-run-task-definition:1
", "TaskCount": 1, "LaunchType": "FARGATE", "NetworkConfiguration": { "awsvpcConfiguration": { "Subnets": ["subnet1
"], "SecurityGroups": ["secgroup1
"], "AssignPublicIp": "ENABLED" } }, "PlatformVersion": "LATEST
" } }]Use the following command to create the target:
aws events put-targets \ --rule "
MyRule1
" \ --targets file://scheduledtask.json