AWS Cloudformation容器替代

时间:2018-06-26 02:07:36

标签: amazon-web-services amazon-cloudformation amazon-ecs

我已经使用cloudformation创建了taskDefinition,并且在ContainerDefinitions中,我有Commanddaily作为参数来运行应用程序。

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

我将要为不同的频率(例如每天,每周,每月等)创建TaskSchedule

我知道我可以在AWS控制台中转到Edit Scheduled Task> Schedule Targets> Container override,并用command override更新./test_report, weekly

但是如何在cloudformation taskSchedule中覆盖命令?这是Targets属性中要做的事情吗?

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

预先感谢

2 个答案:

答案 0 :(得分:1)

在cloudformation模板中,CloudWatch事件规则计划是使用cron表达式(或为rate表达式)定义的。您不能从规则本身覆盖入口点,但是可以为每种报告类型(频率)创建任务定义。我在UI或cloudformation文档中找不到任务命令覆盖。

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

您可以在amazon documentation website上阅读有关事件规则表达式的更多信息。

答案 1 :(得分:1)

@toske的答案现在已过期。您可以使用Input参数为目标设置容器替代。例如:

  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", "ingest_scene7_metadata", "-a" ]}]}'
          EcsParameters:
             ....