This topic describes a program that is used to automatically add creator tags to new resources. These tags indicate the users that create the resources. This program improves the efficiency of cost allocation. You can use the trail feature provided by ActionTrail to implement this program.

Prerequisites

The following Alibaba Cloud services are activated:

  • Resource Access Management (RAM)
  • ActionTrail
  • Function Compute
  • Log Service
Note RAM and ActionTrail are free of charge. In terms of Function Compute and Log Service, if the number of used resources exceeds the free quotas, you need to pay the extra fees. For more information, see Billing of Log Service and Billing of Function Compute.

Introduction

ActionTrail records the operations logs of an Alibaba Cloud account. These logs can be consumed in real time. In this case, you can deploy a consumption program. When the program processes a resource creation event, it calls the related Tag API operation and adds a creator tag to the resource that you want to create. This way, you can allocate the costs of this resource in the User Center based on the tag. The following figure shows the detailed process.

Flowchart
  1. Create a trail in ActionTrail.

    You can use the trail feature provided by ActionTrail to record operations logs on the cloud and ship these logs to Log Service.

  2. Consume log data in Log Service.

    You can use Function Compute, Apache Flink, or a custom program to consume log data. In this topic, Function Compute is used.

    After the log data is consumed, Function Compute calls the related Tag API operation to add a creator tag to the resource that you want to create.

  3. View bills in the User Center.

    You can use the Split Bill, Bill Analysis, or Cost Center feature to view the bills of the resource based on the creator tag.

Deploy a consumption program

In this section, a consumption program is deployed to add a creator tag to a new virtual private cloud (VPC).

  1. Visit OpenAPI Explorer.
  2. In the dialog box that appears, click OK to clone code to Cloud Shell.
  3. Run the following command to create a trail and deploy the consumption program:
    ./install.sh
  4. After the program is deployed, wait for at least one minute. Then, run the following command to create a VPC:
    sleep 60
    CREATED_VPC_ID=`aliyun vpc CreateVpc --RegionId cn-huhehaote | jq ".VpcId"`
  5. Wait for one to five minutes and run the following command to check whether a creator tag is added to the created VPC:
    aliyun vpc DescribeVpcs --RegionId cn-huhehaote --VpcId $CREATED_VPC_ID | jq ".Vpcs.Vpc[0].Tags"

    If the following information is returned, the creator tag is added to the created VPC.

    {
      "Tag": [
        {
          "Key": "created_by",
          "Value": "ram-user:22135730502024****"
        }
      ]
    }

(Optional) Delete resources

Run the following command to delete all resources created in the program. After the resources are deleted, the consumption program becomes unavailable.

./uninstall.sh

(Optional) Modify a resource type

The preceding program can be used to automatically add creator tags to the following types of resources:

  • Elastic Compute Service (ECS): ECS instances, cloud disks, snapshots, security groups, images, key pairs, and launch templates
  • ApsaraDB RDS instances
  • Server Load Balancer (SLB) instances
  • VPC: VPCs and vSwitches

You can also modify the configuration file of the program to enable the program to support more resource types. These resource types must support ActionTrail and tags. For more information, see Types of resources that support ActionTrail and Alibaba Cloud services that support tags.

  1. In the upper-right corner of Cloud Shell, click the Editor icon. Then, copy the content in the resource_type_arn_event_mapping.csv file to a CSV file on your on-premises machine.

    Path of the resource_type_arn_event_mapping.csv file:

    actiontrail-best-practice-auto-tagging/fc-auto-tagging/src/main/resources/resource_type_arn_event_mapping.csv Modify the configuration file
  2. Modify the resource types in the CSV file on the on-premises machine.
  3. Copy the new content to the resource_type_arn_event_mapping.csv file.
  4. Run the following command to deploy the program again:
    ./reinstall.sh