ACS-TAG-MonitorTagEcsInstanceAndRunCommand

Updated at:

Template name

ACS-TAG-MonitorTagEcsInstanceAndRunCommand: Monitors tag events and runs a Cloud Assistant command when a specified tag is added to an ECS instance.

Run now

Template description

Monitors tag events and runs a Cloud Assistant command when a specified tag is added to an ECS instance.

Template type

Automation

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Constraints

tagKey

The tag key to monitor.

String

Yes

tagValue

The tag value to monitor.

String

Yes

commandContent

The content of the command.

String

Yes

Maximum length: 16384

regionId

The ID of the region.

String

No

{{ ACS::RegionId }}

commandType

The type of the command.

String

No

RunShellScript

workingDir

The working directory for the command.

String

No

""

timeout

The timeout period in seconds.

Number

No

600

enableParameter

Whether the command contains encrypted or custom parameters.

Boolean

No

False

username

The username for running the command.

String

No

""

windowsPasswordName

The password name of the user who runs the command on a Windows instance.

String

No

""

rateControl

The concurrency settings for task execution.

Json

No

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

OOSAssumeRole

The RAM role that OOS assumes.

String

No

""

Output parameters

Parameter

Description

Type

tagEventContent

String

instanceId

String

isTriggerExecute

String

Access policy required to run this template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

Details

ACS-TAG-MonitorTagEcsInstanceAndRunCommand details

Template content

FormatVersion: OOS-2019-06-01
Description:
    en: Monitors tag events and runs a Cloud Assistant command when a specified tag is added to an ECS instance.
    zh-cn: Monitors tag events and runs a Cloud Assistant command when a specified tag is added to an ECS instance.
    name-en: ACS-TAG-MonitorTagEcsInstanceAndRunCommand
    name-zh-cn: Monitors tag events and runs a Cloud Assistant command when a specified tag is added to an ECS instance.
    categories:
      - event-trigger
      - tag_event_trigger
Parameters:
  regionId:
    Label:
      en: Region ID
      zh-cn: Region ID
    Type: String
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  tagKey:
    Label:
      en: Tag key to monitor
      zh-cn: Tag key to monitor
    Type: String
  tagValue:
    Label:
      en: Tag value to monitor
      zh-cn: Tag value to monitor
    Description:
      en: When you tag an ECS instance, <font color='red'>the command fails if the instance is in the stopped state.</font>
      zh-cn: When you tag an ECS instance, <font color='red'>the command fails if the instance is in the stopped state.</font>
    Type: String
  commandType:
    Label:
      en: Command type
      zh-cn: Command type
    AssociationPropertyMetadata:
      LocaleKey: ECSCommandType
    Type: String
    AllowedValues:
      - RunShellScript
      - RunPythonScript
      - RunPerlScript
      - RunBatScript
      - RunPowerShellScript
    Default: RunShellScript
  commandContent:
    Label:
      en: Command content
      zh-cn: Command content
    Type: String
    MaxLength: 16384
    AssociationProperty: ALIYUN::OOS::Command::CommandContent
    AssociationPropertyMetadata:
      CommandType: ${commandType}
  workingDir:
    Description:
      en: 'For Linux instances, the default directory is /root, which is the home directory of the root user. For Windows instances, the default directory is the directory where the Cloud Assistant client process is located, such as C:\Windows\System32. Make sure that the directory you enter is correct.'
      zh-cn: 'For Linux instances, the default directory is /root, which is the home directory of the root user. For Windows instances, the default directory is the directory where the Cloud Assistant client process is located, such as C:\Windows\System32. Make sure that the directory you enter is correct.'
    Label:
      en: Working directory
      zh-cn: Working directory
    Type: String
    Default: ''
    AssociationPropertyMetadata:
      Value:
        - Condition:
            Fn::Or:
              - Fn::Equals:
                  - ${commandType}
                  - RunShellScript
              - Fn::Equals:
                  - ${commandType}
                  - RunPythonScript
              - Fn::Equals:
                  - ${commandType}
                  - RunPerlScript
          Value: /root
        - Condition:
            Fn::Or:
              - Fn::Equals:
                  - ${commandType}
                  - RunBatScript
              - Fn::Equals:
                  - ${commandType}
                  - RunPowerShellScript
          Value: C:\\Windows\\System32
  timeout:
    Label:
      en: Timeout (seconds)
      zh-cn: Timeout (seconds)
    Type: Number
    Default: 600
  enableParameter:
    Label:
      en: Contains encrypted or custom parameters
      zh-cn: Contains encrypted or custom parameters
    Type: Boolean
    Default: false
  username:
    Description:
      en: The username used to run the command on the ECS instance. The username can be up to 255 characters long. For Linux instances, the command is run as the root user by default. For Windows instances, the command is run as the System user by default. Make sure that the username you enter is correct. <a href="https://www.alibabacloud.com/help/doc-detail/203771.html" target="_blank">Learn more</a>
      zh-cn: The username used to run the command on the ECS instance. The username can be up to 255 characters long. For Linux instances, the command is run as the root user by default. For Windows instances, the command is run as the System user by default. Make sure that the username you enter is correct. <a href="https://www.alibabacloud.com/help/doc-detail/203771.html" target="_blank">Learn more</a>
    Label:
      en: Username
      zh-cn: Username
    Type: String
    Default: ''
  windowsPasswordName:
    Label:
      en: Windows password name
      zh-cn: Windows password name
    Type: String
    Default: ''
    AssociationProperty: ALIYUN::OOS::SecretParameter::Value
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Or:
            - Fn::Equals:
                - ${commandType}
                - RunBatScript
            - Fn::Equals:
                - ${commandType}
                - RunPowerShellScript
  rateControl:
    Label:
      en: Rate control
      zh-cn: Rate control
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Label:
      en: OOS assume role
      zh-cn: OOS assume role
    Description:
      en: The RAM role for OOS to assume.
      zh-cn: The RAM role for OOS to assume.
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: eventTrigger
    Description:
      en: Monitors instance tag changes.
      zh-cn: Monitors instance tag changes.
    Action: ACS::EventTrigger
    Properties:
      Product: tag
      Name:
        - Tag:ChangeOnResource
      Level:
        - INFO
      RegionId:
        - '{{ regionId }}'
      Content:
        product:
          - ecs
        resourceType:
          - instance
        changedTagKeys:
          - '{{ tagKey }}'
    Outputs:
      instanceId:
        ValueSelector: .content.resourceId
        Type: String
      content:
        ValueSelector: .content
        Type: String
      isAddTag:
        ValueSelector: .content.tags|.{{tagKey}}=="{{tagValue}}"|tostring
        Type: String

  - Name: whetherTagSmc
    Action: ACS::Choice
    Properties:
      DefaultTask: 'ACS::END'
      Choices:
        - When:
            Fn::Equals:
              - 'true'
              - '{{ eventTrigger.isAddTag }}'
          NextTask: runCommand

  - Name: runCommand
    Action: ACS::ECS::RunCommand
    Description:
      en: Runs a Cloud Assistant command.
      zh-cn: Runs a Cloud Assistant command.
    Properties:
      regionId: '{{ regionId }}'
      commandContent: '{{ commandContent }}'
      instanceId: '{{ eventTrigger.instanceId }}'
      commandType: '{{ commandType }}'
      workingDir: '{{ workingDir }}'
      timeout: '{{ timeout }}'
      enableParameter: '{{ enableParameter }}'
      username: '{{ username }}'
      windowsPasswordName: '{{ windowsPasswordName }}'
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
Outputs:
  tagEventContent:
    Value: '{{ eventTrigger.content }}'
    Type: String
  instanceId:
    Value: '{{ eventTrigger.instanceId }}'
    Type: String
  isTriggerExecute:
    Value: '{{ eventTrigger.isAddTag }}'
    Type: String