# stacks/container-apps/scheduled-task.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: >
  Creates a scheduled task based on a passed in ECS task definition
Parameters:
  # ECS Cluster ################################################################
  ECSClusterArn:
    Type: String
  ECSServiceIAMRoleArn:
    Type: String
  # Scheduling #################################################################
  ScheduleCommand:
    Type: String
    Default: run-scheduled
  ScheduleExpression:
    Type: String
  ScheduleTaskDefinitionName:
    Type: String
  ScheduleTaskDefinitionArn:
    Type: String
Resources:
   ScheduledRule:
    Type: "AWS::Events::Rule"
    Properties:
      Description: Schedule the execution of the ECS task
      ScheduleExpression: !Ref ScheduleExpression
      State: "ENABLED"
      Targets:
        - Id: "ScheduledWorkerTaskDefinitionTarget"
          Input: !Sub '{ "containerOverrides": [ { "name": "${ScheduleTaskDefinitionName}", "command": ${ScheduleCommand} } ] }'
          RoleArn: !Ref ECSServiceIAMRoleArn
          Arn: !Ref ECSClusterArn
          EcsParameters:
            TaskCount: 1
            TaskDefinitionArn: !Ref ScheduleTaskDefinitionArn

# stacks/container-apps/scheduled-task.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: >
  Creates a scheduled task based on a passed in ECS task definition
Parameters:
  # ECS Cluster ################################################################
  ECSClusterArn:
    Type: String
  ECSServiceIAMRoleArn:
    Type: String
  # Scheduling #################################################################
  ScheduleCommand:
    Type: String
    Default: run-scheduled
  ScheduleExpression:
    Type: String
  ScheduleTaskDefinitionName:
    Type: String
  ScheduleTaskDefinitionArn:
    Type: String
Resources:
  EventsIAMRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service:
                - "events.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Path: "/"
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceEventsRole"
  ScheduledRule:
    Type: "AWS::Events::Rule"
    Properties:
      Description: Schedule the execution of the ECS task
      ScheduleExpression: !Ref ScheduleExpression
      State: "ENABLED"
      Targets:
        - Id: "ScheduledWorkerTaskDefinitionTarget"
          Input: !Sub '{ "containerOverrides": [ { "name": "${ScheduleTaskDefinitionName}", "command": ${ScheduleCommand} } ] }'
          RoleArn: !GetAtt EventsIAMRole.Arn
          Arn: !Ref ECSClusterArn
          EcsParameters:
            TaskCount: 1
            TaskDefinitionArn: !Ref ScheduleTaskDefinitionArn

Add role for events to invike ecs tasks
Add role for events to invike ecs tasks
