Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation Container overrides

I've created the taskDefinition using cloudformation and in the ContainerDefinitions I have Command to run the application with daily as parameter.

TaskDefinition: 
  Type: AWS::ECS::TaskDefinition
  Properties:
    :
    :
    ContainerDefinitions:
    - Name: test-report
      :
      :
      Command:
      - "./test_report"
      - daily

And I will want to create TaskSchedule for different frequency (eg. daily, weekly, monthly etc).

I know that in the AWS console I can go to Edit Scheduled Task > Schedule Targets > Container override and update the command override with ./test_report, weekly.

But how to override command in cloudformation taskSchedule? Is that something to do in Targets property??

  TaskSchedule:
    Type: AWS::Events::Rule
    Properties:
      :
      Targets:

Thanks in advance

like image 231
hobbyking Avatar asked Dec 24 '22 06:12

hobbyking


2 Answers

@toske's answer is now out-of-date. You can set container overrides using the Input parameter to the target. For example:

  LogicalResource:
    Type: AWS::Events::Rule
    Properties:
      Description: "a scheduled task"
      Name: my_scheduled_task
      ScheduleExpression: "cron(* * * * ? *)"
      State: ENABLED
      RoleArn: !GetAtt MyRole.Arn
      Targets:
        - Id: fargate
          Arn: !GetAtt MyCluster.Arn
          RoleArn: !GetAtt MyTaskRole.Arn
          Input: '{ "containerOverrides": [{"name": "container", "command": [ "python", "manage.py", "my_awesome_management_command", "-a" ]}]}'
          EcsParameters:
             ....
like image 79
2ps Avatar answered Jan 03 '23 15:01

2ps


Within cloudformation template, CloudWatch event rule schedule is defined using cron expressions (or alternatively rate expressions). You can't override entrypoint from the rule itself, but you can create task definition for each of the report types (frequencies). I was unable to find task command override in the UI or the cloudformation documentation.

TaskDefinitionDaily: 
  Type: AWS::ECS::TaskDefinition
  Properties:
     # other task definition elements here
     ContainerDefinitions:
       # other container definitions here
        Command: 
          - "./test_report"
          - daily

TaskDefinitionWeekly: 
  Type: AWS::ECS::TaskDefinition
  Properties:
     # other task definition elements here
     ContainerDefinitions:
       # other container definitions here
        Command: 
          - "./test_report"
          - weekly

TaskDefinitionMonthly: 
  Type: AWS::ECS::TaskDefinition
  Properties:
     # other task definition elements here
     ContainerDefinitions:
       # other container definitions here
        Command: 
          - "./test_report"
          - monthly


# daily schedule at midnight UTC      
ScheduledRuleDaily: 
  Type: "AWS::Events::Rule"
  Properties: 
    Description: "ScheduledRule"
    ScheduleExpression: "cron(00 00 * * ? *)"
    State: "ENABLED"
    Targets:
          - Arn: !GetAtt 
              - MyCluster
              - Arn
            RoleArn: !GetAtt 
              - ECSTaskRole
              - Arn
            Id: id1
            EcsParameters:
              TaskCount: 1
              TaskDefinitionArn: !Ref TaskDefinitionDaily


# weekly schedule at midnight UTC   
ScheduledRuleWeekly: 
  Type: "AWS::Events::Rule"
  Properties: 
    Description: "ScheduledRule"
    ScheduleExpression: "cron(00 00 ? * 1 *)"
    State: "ENABLED"
    Targets:
          - Arn: !GetAtt 
              - MyCluster
              - Arn
            RoleArn: !GetAtt 
              - ECSTaskRole
              - Arn
            Id: id1
            EcsParameters:
              TaskCount: 1
              TaskDefinitionArn: !Ref TaskDefinitionWeekly

# monthly schedule  at midnight UTC     
ScheduledRuleMonthly: 
  Type: "AWS::Events::Rule"
  Properties: 
    Description: "ScheduledRule"
    ScheduleExpression: "cron(00 00 1 * ? *)"
    State: "ENABLED"
    Targets:
          - Arn: !GetAtt 
              - MyCluster
              - Arn
            RoleArn: !GetAtt 
              - ECSTaskRole
              - Arn
            Id: id1
            EcsParameters:
              TaskCount: 1
              TaskDefinitionArn: !Ref TaskDefinitionMonthly

You can read more on event rule expressions on amazon documentation website.

like image 36
toske Avatar answered Jan 03 '23 14:01

toske