All Products
Search
Document Center

CloudOps Orchestration Service:ACS-ECS-ScheduleToStartAndStopOrStopAndStartInstances

Last Updated:Nov 27, 2024

Template name

ACS-ECS-ScheduleToStartAndStopOrStopAndStartInstances

Execute Now

Template description

Starts and then stops or stops and then starts Elastic Compute Service (ECS) instances as scheduled.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Header

Description

Data type

Required

Default Value

Limit

targets

The instances that you want to start or stop.

Json

Yes

dailyStartTime

The time when the instances are started each day.

String

Yes

dailyStopTime

The time when the instances are stopped each day.

String

Yes

regionId

The region ID.

String

No

{{ ACS::RegionId }}

startFirstOrStopFirst

The mode in which the instances are started or stopped.

String

No

StartFirst

weekdays

The cycle of the scheduled task.

String

No

*

triggerEndDate

The end time of the scheduled task.

String

No

2099-12-01T00:00:00Z

stoppedMode

The stop mode.

String

No

KeepCharging

hibernate

Specifies whether to hibernate the instances.

Boolean

No

False

rateControl

The rate control settings.

Json

No

{'Mode': 'Concurrency', 'MaxErrors': 0, 'Concurrency': 10}

OOSAssumeRole

The RAM role that is assumed by CloudOps Orchestration Service (OOS).

String

No

AliyunServiceRoleForOOSInstanceScheduler

Output parameters

N/A.

Permission policy that is required to execute the template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:DescribeInstances",
                "ecs:StartInstance",
                "ecs:StopInstance"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

References

ACS-ECS-ScheduleToStartAndStopOrStopAndStartInstances

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: Schedule to start and stop ECS instances  
  name-en: ACS-ECS-ScheduleToStartAndStopOrStopAndStartInstances   
  categories:
    - time_trigger
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId       
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  targets:
    Type: Json
    Label:
      en: TargetInstance       
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: ALIYUN::ECS::Instance
      RegionId: regionId
  startFirstOrStopFirst:
    Type: String
    Label:
      en: StartFirstOrStopFirst      
    AllowedValues:
      - StartFirst
      - StopFirst
    Default: StartFirst
    AssociationPropertyMetadata:
      LocaleKey: OOSStopInstanceMode
  weekdays:
    Label:
      en: The Cycle of task      
    Type: String
    AssociationProperty: TimeTriggerWeekly
    Default: '*'
  dailyStartTime:
    Label:
      en: DailyStartTime       
    Type: String
    AssociationProperty: DateTime
    AssociationPropertyMetadata:
      Format: HH:mm:ssZ
  dailyStopTime:
    Label:
      en: DailyStopTime       
    Type: String
    AssociationProperty: DateTime
    AssociationPropertyMetadata:
      Format: HH:mm:ssZ
  triggerEndDate:
    Label:
      en: The end date of time trigger       
    Type: String
    AssociationProperty: DateTime
    AssociationPropertyMetadata:
      Format: YYYY-MM-DDTHH:mm:ssZ
    Default: '2099-12-01T00:00:00Z'
  stoppedMode:
    Label:
      en: StoppedMode     
    Description:
      en: <p class="p">The stop mode that is set when stopping a pay-as-you-go ECS instance. Value:</p> <ul class="ul"> <li class="li"Save downtime mode. For the conditions for <code class="ph codeph">StopCharging</code> to take effect, please refer to <span><a target="_blank" href="https://help.aliyun.com/document_detail/63353.html" >Enable Conditions for Pay-As-You-Go Instance Savings Downtime Mode</a></span>. </li> <li class="li">Normal shutdown mode. Charges continue even after the instance is stopped. </li> </ul>
        </li> <li class="li"> Normal shutdown mode: The instance continues to be billed after it is stopped. </li> </ul>
    Type: String
    AllowedValues:
      - StopCharging
      - KeepCharging
    Default: KeepCharging
    AssociationPropertyMetadata:
      LocaleKey: ECSStoppedMode
  hibernate:
    Label:
      en: WhetherToHibernateTheInstance      
    Type: Boolean
    Default: false
  rateControl:
    Label:
      en: RateControl      
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole      
    Type: String
    Default: AliyunServiceRoleForOOSInstanceScheduler
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: timerTrigger
    Action: ACS::TimerTrigger
    Description:
      en: Triggers a task as scheduled by specifying Cron expression      
    Properties:
      Type: cron
      EndDate: '{{ triggerEndDate }}'
      Expression:
        Fn::Join:
          - ' '
          - - '0'
            - Fn::Select:
                - 1
                - Fn::Split:
                    - ':'
                    - Fn::If:
                        - Fn::Equals:
                            - StartFirst
                            - '{{startFirstOrStopFirst}}'
                        - '{{ dailyStartTime }}'
                        - '{{ dailyStopTime }}'
            - Fn::Select:
                - 0
                - Fn::Split:
                    - ':'
                    - Fn::If:
                        - Fn::Equals:
                            - StartFirst
                            - '{{startFirstOrStopFirst}}'
                        - '{{ dailyStartTime }}'
                        - '{{ dailyStopTime }}'
            - '?'
            - '*'
            - '{{ weekdays }}'
  - Name: getInstance
    Description:
      en: Views the ECS instances       
    Action: ACS::SelectTargets
    Properties:
      ResourceType: ALIYUN::ECS::Instance
      RegionId: '{{regionId}}'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: Instances.Instance[].InstanceId
  - Name: startInstanceFirst
    Action: ACS::ECS::StartInstance
    Description:
      en: Starts the ECS instances       
    When:
      Fn::Equals:
        - StartFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      regionId: '{{ regionId }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: sleepToStopTime
    Action: ACS::Sleep
    Description:
      en: Sleep to instance stop time       
    When:
      Fn::Equals:
        - StartFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      Duration:
        Fn::DurationBetween:
          - '{{ dailyStartTime }}'
          - '{{ dailyStopTime }}'
  - Name: stopInstanceSecond
    Action: ACS::ECS::StopInstance
    Description:
      en: Stops the ECS instances      
    When:
      Fn::Equals:
        - StartFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      regionId: '{{ regionId }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
      stoppedMode: '{{ stoppedMode }}'
      hibernate: '{{ hibernate }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: stopInstanceFirst
    Action: ACS::ECS::StopInstance
    Description:
      en: Stop the ECS instances first       
    When:
      Fn::Equals:
        - StopFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      regionId: '{{ regionId }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
      stoppedMode: '{{ stoppedMode }}'
      hibernate: '{{ hibernate }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: sleepToStartTime
    Action: ACS::Sleep
    Description:
      en: Sleep to instance stop time     
    When:
      Fn::Equals:
        - StopFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      Duration:
        Fn::DurationBetween:
          - '{{ dailyStopTime }}'
          - '{{ dailyStartTime }}'
  - Name: startInstance
    Action: ACS::ECS::StartInstance
    Description:
      en: Starts the ECS instances       
    When:
      Fn::Equals:
        - StopFirst
        - '{{startFirstOrStopFirst}}'
    Properties:
      regionId: '{{ regionId }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'