Description
The ACS::Choice action can be used to determine the subsequent task to be executed based on conditions. For example, before you restart an instance, you want to query the status of the instance and select the task to be executed based on the returned result. If the instance is in the Running state, a task is executed to restart the instance. If the instance is in the Stopped state, a task is executed to start the instance. If the instance is in other states, a task is executed to query the status of the instance.
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 }}"
}
}
}