All Products
Search
Document Center

Elastic Compute Service:Use OOS to add tags to multiple ECS resources at a time

Last Updated:Mar 28, 2024

If you want to add a tag to multiple Elastic Compute Service (ECS) resources that reside in the same region, you can use CloudOps Orchestration Service (OOS) to add the tag to the ECS resources at a time. This allows you to classify and manage ECS resources in a fine-grained manner. For example, you can collect cost statistics and perform O&M and monitoring on ECS resources. This topic describes how to use the OOS console to add tags to multiple ECS resources at a time.

Background information

You can use OOS custom templates to add a tag to multiple resources of ECS and other Alibaba Cloud services. For more information about the services that support tags, see ECS resources that support tags. In this topic, a custom template is created in OOS to add the owner:zhangsan tag to multiple ECS instances that reside in the same region.

Important

The resources to which you want to add tags must reside in the same region.

Step 1: Create a custom policy and a RAM role

Create a Resource Access Management (RAM) role named OOSServiceRole for OOS and attach policies to the role.

  1. Log on to the RAM console with an Alibaba Cloud account.

  2. Create a custom policy named OOSAutoBindTag.

    For more information, see Create custom policies. The following policy is created in this step.

    Note

    This policy is used to grant permissions on ECS instances, and the permission in the policy is set to ecs:DescribeInstances. You can set the permission based on your business requirements. For example, if you want to add a tag to multiple security groups, you can replace ecs:DescribeInstances with ecs:DescribeSecurityGroups.

    {
        "Version": "1",
        "Statement": [
            {
                "Action": [
                    "ecs:DescribeInstances",
                    "ecs:TagResources"
                ],
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
  3. Create a RAM role named OOSServiceRole.

  4. Attach the custom policy to the RAM role.

    For more information, see Grant permissions to a RAM role. In this step, the OOSAutoBindTag custom policy is attached to the OOSServiceRole RAM role.

  5. Attach the AliyunOOSFullAccess system policy to the OOSServiceRole RAM role.

Step 2: Create a custom template

  1. Log on to the CloudOps Orchestration Service (OOS) console.

  2. In the left-side navigation pane, choose Automated Task > Custom Template.

  3. In the top navigation bar, select the region and resource group to which the resource belongs. 地域

  4. Click Create Template.

  5. Click the YAML tab to edit the template code. Sample code:

    FormatVersion: OOS-2019-06-01
    Description: Tag Resources Without The Specified Tags
    Parameters:
      tags:
        Type: Json
        Description:
          en: The tags to select ECS instances.
           
        AssociationProperty: Tags
      regionId:
        Type: String
        Description:
          en: The region to select ECS instances.
           
      OOSAssumeRole:
        Description:
          en: The RAM role to be assumed by OOS.
           
        Type: String
        Default: OOSServiceRole
    RamRole: OOSServiceRole
    Tasks:
      - Name: getInstancesByTags
        Action: 'ACS::ExecuteAPI'
        Description: ''
        Properties:
          Service: ECS
          API: DescribeInstances
          Parameters:
            Tags: '{{ tags }}'
            RegionId: '{{ regionId }}'
        Outputs:
          InstanceIds:
            Type: List
            ValueSelector: 'Instances.Instance[].InstanceId'
      - Name: getAllInstances
        Action: 'ACS::ExecuteAPI'
        Description: ''
        Properties:
          Service: ECS
          API: DescribeInstances
          Parameters:
            RegionId: '{{regionId}}'
        Outputs:
          InstanceIds:
            Type: List
            ValueSelector: 'Instances.Instance[].InstanceId'
      - Name: TagResources_ECS_Instances
        Action: 'ACS::ExecuteAPI'
        Description:
           
          en: 'tag ecs instances, which are without the specified tags.'
        Properties:
          Service: ECS
          API: TagResources
          Parameters:
            Tags: '{{ tags }}'
            RegionId: '{{regionId}}'
            ResourceType: Instance
            ResourceIds:
              - '{{ACS::TaskLoopItem}}'
        Loop:
          MaxErrors: 100%
          Concurrency: 20
          Items:
            'Fn::Difference':
              - '{{ getAllInstances.InstanceIds }}'
              - '{{ getInstancesByTags.InstanceIds }}'
    Outputs:
      InstanceIds:
        Type: List
        Value:
          'Fn::Difference':
            - '{{ getAllInstances.InstanceIds }}'
            - '{{ getInstancesByTags.InstanceIds }}'
  6. Click Create Template. In the dialog box that appears, set the name of the custom template to OOSAutoBindTag.

    You can also configure the Tag, Resource Group, and Version Description parameters based on your business requirements.

  7. Click Complete Create.

Step 3: Execute the custom template

  1. In the left-side navigation pane, choose Automated Task > Custom Template.

  2. On the Custom Template page, find the OOSAutoBindTag custom template and click Create Execution in the Actions column.

  3. Use the default execution mode or select a different mode. Then, click Next Step: Parameter Settings.

  4. Configure the parameters and click Next Step: OK. The following table describes the parameters.

    Parameter

    Description

    Example

    tags

    Select the tag added to the ECS instance.

    owner:zhangsan

    regionId

    Enter the region of the ECS instances to which you want to add the tag.

    cn-shanghai

    OOSAssumeRole

    Specify the RAM role that can be assumed by OOS.

    OOSServiceRole

  5. Click Create.

    After the template is executed, the execution details page appears. You can view the result on this page.

    Note

    If the template fails to be executed, you can view logs to identify the cause of the failure and make adjustments accordingly.

References