ALIYUN::ECS::RunCommand is used to run a shell, PowerShell, or batch command on Elastic Compute Service (ECS) instances.
Syntax
{
"Type": "ALIYUN::ECS::RunCommand",
"Properties": {
"Parameters": Map,
"Description": String,
"Timeout": Integer,
"ContentEncoding": String,
"Name": String,
"WorkingDir": String,
"CommandContent": String,
"Type": String,
"Frequency": String,
"EnableParameter": Boolean,
"InstanceIds": List,
"KeepCommand": Boolean,
"Sync": Boolean,
"Tags": List,
"RunAgainOn": List,
"WindowsPasswordName": String,
"RepeatMode": String,
"ResourceGroupId": String,
"ContainerName": String,
"ContainerId": String,
"Username": String
}
}
Properties
Property | Type | Required | Editable | Description | Constraint |
Parameters | Map | No | Yes | The key-value pairs of the custom parameters that are passed in if the command includes custom parameters. For example, if the command content is echo {{name}}, you can use the Parameters property to pass in the {"name":"Jack"} key-value pair. The value of the name variable in echo {{name}} is automatically replaced by the paired Jack value to generate a new command. As a result, the echo Jack command is run. | You can specify up to 10 custom parameters. The key of a custom parameter can be up to 64 characters in length, and cannot be an empty string. The value of a custom parameter can be an empty string. After the custom parameters and the original command content are encoded in Base64, the total size can be up to 16 KB. The custom parameter names specified in the value of the Parameters property must be included in the custom parameter settings that you configured when you created the command. You can use empty strings to represent the parameters that are not specified. |
Description | String | No | No | The description of the command. | The description supports all character sets and can be up to 512 characters in length. |
Timeout | Integer | No | Yes | The timeout period for running the command. | Unit: seconds. Default value: 60. A timeout error occurs if a command cannot be run because the command process slows down or because a specific module or Cloud Assistant Agent does not exist. When a timeout error occurs, the command process is forcefully terminated. |
ContentEncoding | String | No | Yes | The encoding mode of the command content that is specified by CommandContent. | Valid values:
Note If you specify an invalid value for this property, Base64 is used to encode the command content. |
Name | String | No | No | The command name. | The name supports all character sets and can be up to 128 characters in length. |
WorkingDir | String | No | Yes | The working directory of the command on the ECS instance. | Default values:
|
CommandContent | String | Yes | Yes | The command content. The command content can be plaintext or Base64-encoded. | After the command content is encoded in Base64, the content can be up to 16 KB in size. If you set EnableParameter to true, the custom parameter feature is enabled. You can configure custom parameters based on the following rules: Specify custom parameters in the {{}} format. Within {{}}, spaces and line feeds before and after the parameter names are ignored. You can specify up to 10 custom parameters. A custom parameter name can contain letters, digits, hyphens (-), and underscores (_). A custom parameter name can be up to 64 characters in length. |
Type | String | Yes | Yes | The language type of the command. | Valid values:
|
Frequency | String | No | No | The interval at which the command is run. | None. |
EnableParameter | Boolean | No | Yes | Specifies whether to include custom parameters in the command. | Valid values:
|
InstanceIds | List | Yes | Yes | The IDs of the ECS instances. | You can specify up to 20 ECS instances. The ECS instances must be in the Running state. |
KeepCommand | Boolean | No | No | Specifies whether to retain the command after it is run. | Valid values:
|
Sync | Boolean | No | No | Specifies whether to run the command in synchronous mode. | Valid values:
|
Tags | List | No | Yes | The tags. | You can add up to 20 tags. For more information, see Tags properties. |
RunAgainOn | List | No | No | The stages of running the command again. | None. |
WindowsPasswordName | String | No | No | The name of the password that you want to use to run the command on Windows instances. | The name can be up to 255 characters in length. If you do not want to use the default System user to run the command on Windows instances, specify both WindowsPasswordName and Note If you use the root user of a Linux instance or the System user of a Windows instance to run the command, you can leave WindowsPasswordName empty. |
RepeatMode | String | No | No | The mode in which you want to run the command. | Valid values:
Default values:
Take note of the following items:
|
ResourceGroupId | String | No | No | The ID of the resource group to which the command executions belong. | Take note of the following items:
|
ContainerName | String | No | No | The container name. | Take note of the following items:
|
ContainerId | String | No | No | The container ID. | Only 64-bit hexadecimal strings are supported. You can use container IDs that are prefixed with Take note of the following items:
|
Username | String | No | Yes | The username that you want to use to run the command on ECS instances. | The username can be up to 255 characters in length.
You can also specify other usernames that already exist on the ECS instances to run the command. For security purposes, we recommend that you run Cloud Assistant commands as a regular user. For more information, see Run Cloud Assistant commands as a regular user. |
Tags syntax
"Tags": [
{
"Key": String,
"Value": String
}
]
Tags properties
Property | Type | Required | Editable | Description | Constraint |
Key | String | Yes | No | The tag key. | The tag key must be 1 to 128 characters in length, and cannot contain |
Value | String | No | No | The tag value. | The tag value can be up to 128 characters in length, and cannot contain |
Return values
Fn::GetAtt
CommandId: the command ID.
InvokeId: the ID of the command execution.
InvokeInstances: the ECS instances on which the command is run.
InvokeResults: the execution results of the command.
Examples
YAML
format
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
InstanceId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::InstanceId
Resources:
RunCommand:
Type: ALIYUN::ECS::RunCommand
Properties:
CommandContent:
Fn::Sub:
|
#!/bin/sh
yum install -y tree
Type: RunShellScript
InstanceIds:
- Ref: InstanceId
Outputs:
CommandId:
Description: The id of command created.
Value:
Fn::GetAtt:
- RunCommand
- CommandId
InvokeId:
Description: The invoke id of command.
Value:
Fn::GetAtt:
- RunCommand
- InvokeId
JSON
format
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"InstanceId": {
"Type": "String",
"AssociationProperty": "ALIYUN::ECS::Instance::InstanceId"
}
},
"Resources": {
"RunCommand": {
"Type": "ALIYUN::ECS::RunCommand",
"Properties": {
"CommandContent": {
"Fn::Sub":
"#!/bin/sh\nyum install -y tree\n"
},
"Type": "RunShellScript",
"InstanceIds": [
{
"Ref": "InstanceId"
}
]
}
}
},
"Outputs": {
"CommandId": {
"Description": "The id of command created.",
"Value": {
"Fn::GetAtt": [
"RunCommand",
"CommandId"
]
}
},
"InvokeId": {
"Description": "The invoke id of command.",
"Value": {
"Fn::GetAtt": [
"RunCommand",
"InvokeId"
]
}
}
}
}
UpdatePolicy overview
After you use ALIYUN::ECS::RunCommand to run a command on a collection of ECS instances, you can use the UpdatePolicy property to update the configurations of the ECS instances in batches. The following tables describe the UpdatePolicy property and its syntax.
UpdatePolicy syntax
"UpdatePolicy": {
"RollingUpdate": Map
}
UpdatePolicy property
Property | Type | Required | Editable | Description | Constraint |
RollingUpdate | Map | No | Yes | The update policy for the existing command. | For more information, see RollingUpdate properties. |
RollingUpdate syntax
"RollingUpdate": {
"MaxBatchSize": Integer,
"PauseTime": Integer
}
RollingUpdate properties
Property | Type | Required | Editable | Description | Constraint |
MaxBatchSize | Integer | Yes | Yes | The number of ECS instances on which you want to run the command in each batch. | Valid values: 1 to 1000. |
PauseTime | Integer | No | Yes | The interval between two batch command executions. Default value: 0. Unit: seconds. | Valid values: 0 to 3600. |
Templates before and after the update
You can use the following template to run the same command on three ECS instances:
{
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"RunCommand": {
"Type": "ALIYUN::ECS::RunCommand",
"Properties": {
"CommandContent": "xxx",
"Type": "RunShellScript",
"Sync": true,
"InstanceIds": [
"i-2zxxxx",
"i-2zxxxx",
"i-2zxxxx",
]
}
}
}
}
After the stack that contains the ECS instances is created, you can add the UpdatePolicy property to the template to update the configurations of the ECS instances:
{
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"RunCommand": {
"Type": "ALIYUN::ECS::RunCommand",
"Properties": {
"CommandContent": "xxx",
"Type": "RunShellScript",
"Sync": true,
"InstanceIds": [
"i-2zxxxx",
"i-2zxxxx",
"i-2zxxxx",
"i-2zxxxx",
"i-2zxxxx",
]
},
"UpdatePolicy": {
"RollingUpdate": {
"MaxBatchSize": 2
"PauseTime": 10
}
}
}
}
}
In the preceding sample code, MaxBatchSize is set to 2. The number of ECS instances indicated by InstanceIds is updated from 3 to 5. During the stack update process, the configurations of the original three ECS instances are updated in two batches in the specified sequence. Each batch separately contains two and one instances. The command is run on the new two ECS instances at a time.