Description
ACS::Choice determines which task to execute next based on conditions. For example, before restarting an instance, you query its status and route to a different task accordingly: if Running, execute a reboot task; if Stopped, execute a start task; otherwise, execute a status query task.
Syntax
Tasks:
- Name: chooseNextTaskToRun # The name of the task.
Action: ACS::Choice
Properties:
DefaultTask: task1 # The default task that is executed if no condition specified by the Choices attribute is met after the chooseNextTaskToRun task is executed. The name of the task1 task cannot be the name of a parameter. It must be a string that specifies a task name.
Choices:
- When: condition1 # The first condition for judgment.
NextTask: task2 # The task that is executed if the first condition is met after the chooseNextTaskToRun task is executed. The name of the task2 task cannot be the name of a parameter. It must be a string that specifies a task name.
- When: condition2 # The second condition for judgment.
NextTask: task3 # The task that is executed if the second condition is met after the chooseNextTaskToRun task is executed. The name of the task3 task cannot be the name of a parameter. It must be a string that specifies a task name.
# If necessary, you can use more WHEN conditions in the Choices attribute.
#...{
"Tasks": [
{
"Name": "chooseNextTaskToRun",
"Action": "ACS::Choice",
"Properties": {
"DefaultTask": "task1",
"Choices": [
{
"When": "condition1",
"NextTask": "task2"
},
{
"When": "condition2",
"NextTask": "task3"
}
]
}
}
]
}Examples
FormatVersion: OOS-2019-06-01
Description:
en: Bulky restarts the ECS instances.
name-en: ACS-ECS-BulkyRebootInstances
Parameters:
targets:
Type: Json
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
OOSAssumeRole:
Description:
en: The RAM role to be assumed by OOS.
Type: String
Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: getInstance
Description:
en: Views the ECS instances.
Action: 'ACS::SelectTargets'
Properties:
ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
Filters:
- '{{ targets }}'
Outputs:
instanceIds:
Type: List
ValueSelector: 'Instances.Instance[].InstanceId'
status:
Type: String
ValueSelector: Instances.Instance[].Status
- Name: whetherStatusRunning
Action: 'ACS::Choice'
Description:
en: Starts the ECS instances
Properties:
DefaultTask: rebootInstance
Choices:
- When:
'Fn::Equals':
- Stopped
- '{{ describeInstances.status }}'
NextTask: startInstance
- Name: startInstance
Action: 'ACS::ECS::StartInstance'
Description:
en: Starts the ECS instances
Properties:
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
- Name: rebootInstance
Action: 'ACS::ECS::RebootInstance'
Description:
en: Restarts the ECS instances
Properties:
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "Bulky restarts the ECS instances.",
"name-en": "ACS-ECS-BulkyRebootInstances",
},
"Parameters": {
"targets": {
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance"
}
},
"rateControl": {
"Description": {
"en": "Concurrency ratio of task execution.",
},
"Type": "Json",
"AssociationProperty": "RateControl"
},
"OOSAssumeRole": {
"Description": {
"en": "The RAM role to be assumed by OOS.",
},
"Type": "String",
"Default": "OOSServiceRole"
}
},
"RamRole": "{{ OOSAssumeRole }}",
"Tasks": [
{
"Name": "getInstance",
"Description": {
"en": "Views the ECS instances.",
},
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"Filters": [
"{{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
},
{
"Name": "describeInstances",
"Action": "ACS::ExecuteAPI",
"Description": "Views A ECS instances Status.",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceIds": "{{ getInstance.instanceIds }}"
}
},
"Outputs": {
"status": {
"Type": "String",
"ValueSelector": "Instances.Instance[].Status"
}
}
},
{
"Name": "whetherStatusRunning",
"Action": "ACS::Choice",
"Properties": {
"DefaultTask": "describeInstancesFinally",
"Choices": [
{
"When": {
"Fn::Equals": [
"Running",
"{{ describeInstances.status }}"
]
},
"NextTask": "rebootInstance"
},
{
"When": {
"Fn::Equals": [
"Stopped",
"{{ describeInstances.status }}"
]
},
"NextTask": "startInstance"
}
]
}
},
{
"Name": "startInstance",
"Action": "ACS::ECS::StartInstance",
"Description": {
"en": "Starts the ECS instances.",
},
"Properties": {
"instanceId": "{{ ACS::TaskLoopItem }}"
},
"Loop": {
"RateControl": "{{ rateControl }}",
"Items": "{{ getInstance.instanceIds }}"
}
},
{
"Name": "rebootInstance",
"Action": "ACS::ECS::RebootInstance",
"Description": {
"en": "Restarts the ECS instances.",
},
"Properties": {
"instanceId": "{{ ACS::TaskLoopItem }}"
},
"Loop": {
"RateControl": "{{ rateControl }}",
"Items": "{{ getInstance.instanceIds }}"
}
},
{
"Name": "describeInstancesFinally",
"Action": "ACS::ExecuteAPI",
"Description": "Views the ECS instances Status.",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceIds": "{{ getInstance.instanceIds }}"
}
},
"Outputs": {
"status": {
"Type": "String",
"ValueSelector": "Instances.Instance[].Status"
}
}
}
],
"Outputs": {
"instanceIds": {
"Type": "List",
"Value": "{{ getInstance.instanceIds }}"
}
}
}