

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 管理您的成本分配標籤 AWS CLI
<a name="Tagging.Managing.CLI"></a>

您可以使用 AWS CLI 新增、修改或移除成本分配標籤。

成本分配標籤會套用至 ElastiCache 叢集。要加標籤的叢集是使用 ARN (Amazon Resource Name) 來指定。

範例 ARN：`arn:aws:elasticache:us-west-2:1234567890:cluster:my-cluster`

**Topics**
+ [使用 列出標籤 AWS CLI](#Tagging.Managing.CLI.List)
+ [使用 新增標籤 AWS CLI](#Tagging.Managing.CLI.Add)
+ [使用 修改標籤 AWS CLI](#Tagging.Managing.CLI.Modify)
+ [使用 移除標籤 AWS CLI](#Tagging.Managing.CLI.Remove)

## 使用 列出標籤 AWS CLI
<a name="Tagging.Managing.CLI.List"></a>

您可以使用 AWS CLI 來列出現有 ElastiCache 資源上的標籤，方法是使用 [list-tags-for-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/list-tags-for-resource.html)操作。

下列程式碼使用 AWS CLI 列出 us-west-2 `my-cluster` 區域中 Memcached 叢集上的標籤。

針對 Linux、macOS 或 Unix：

```
aws elasticache list-tags-for-resource \
  --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}}
```

針對 Windows：

```
aws elasticache list-tags-for-resource ^
  --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}}
```

下列程式碼使用 AWS CLI 列出區域 us-west-2 `my-cluster-001` 中`my-cluster`叢集中 Valkey 或 Redis OSS 節點上的標籤。

針對 Linux、macOS 或 Unix：

```
aws elasticache list-tags-for-resource \
  --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}}
```

針對 Windows：

```
aws elasticache list-tags-for-resource ^
  --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}}
```

此操作的輸出看起來應該類似以下，這是資源上所有標籤的清單。

```
{
   "TagList": [
      {
         "Value": "10110",
         "Key": "CostCenter"
      },
      {
         "Value": "EC2",
         "Key": "Service"
      }
   ]
}
```

如果資源上沒有標籤，輸出會是空的標籤清單。

```
{
   "TagList": []
}
```

如需詳細資訊，請參閱適用於 ElastiCache AWS CLI 的 [list-tags-for-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/list-tags-for-resource.html)。

## 使用 新增標籤 AWS CLI
<a name="Tagging.Managing.CLI.Add"></a>

您可以使用 使用 CLI [add-tags-to-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/add-tags-to-resource.html) 操作 AWS CLI ，將標籤新增至現有的 ElastiCache 資源。如果標籤索引鍵不存在於資源上，則索引鍵和值會新增至資源。如果索引鍵已存在於資源上，則與該索引鍵相關聯的值會更新為新的值。

下列程式碼使用 AWS CLI 將索引鍵 `Service` 和 `Region` 與值 `elasticache`和 `us-west-2`分別新增至區域 us-west-2  `my-cluster-001` 中叢集中的節點。 `my-cluster`

**Memcached**

針對 Linux、macOS 或 Unix：

```
aws elasticache add-tags-to-resource \
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}} \
 --tags Key={{Service}},Value={{elasticache}} \
        Key={{Region}},Value={{us-west-2}}
```

針對 Windows：

```
aws elasticache add-tags-to-resource ^
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}} ^
 --tags Key={{Service}},Value={{elasticache}} ^
        Key={{Region}},Value={{us-west-2}}
```

**Redis**

針對 Linux、macOS 或 Unix：

```
aws elasticache add-tags-to-resource \
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}} \
 --tags Key={{Service}},Value={{elasticache}} \
        Key={{Region}},Value={{us-west-2}}
```

針對 Windows：

```
aws elasticache add-tags-to-resource ^
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}} ^
 --tags Key={{Service}},Value={{elasticache}} ^
        Key={{Region}},Value={{us-west-2}}
```

此操作的輸出看起來應該類似以下，這是在操作後資源上所有標籤的清單。

```
{
   "TagList": [
      {
         "Value": "elasticache",
         "Key": "Service"
      },
      {
         "Value": "us-west-2",
         "Key": "Region"
      }
   ]
}
```

如需詳細資訊，請參閱適用於 ElastiCache AWS CLI 的 [add-tags-to-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/add-tags-to-resource.html)。

當您使用 操作建立新叢集時 AWS CLI ，您也可以使用 將標籤新增至叢集[create-cache-cluster](https://docs.aws.amazon.com/cli/latest/reference/elasticache/create-cache-cluster.html)。使用 ElastiCache 管理主控台建立叢集時無法新增標籤。建立叢集之後，您可以接著使用主控台來將標籤新增至叢集。

## 使用 修改標籤 AWS CLI
<a name="Tagging.Managing.CLI.Modify"></a>

您可以使用 AWS CLI 來修改 ElastiCache 叢集上的標籤。

若要修改標籤：
+ 使用 [add-tags-to-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/add-tags-to-resource.html) 來新增標籤和值，或是變更與現有標籤關聯的值。
+ 使用 [remove-tags-from-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/remove-tags-from-resource.html) 來從資源移除指定的標籤。

這兩項操作的輸出會是指定叢集上標籤和其值的清單。

## 使用 移除標籤 AWS CLI
<a name="Tagging.Managing.CLI.Remove"></a>

您可以使用 AWS CLI 操作，從現有的 ElastiCache for Memcached 叢集中移除標籤[remove-tags-from-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/remove-tags-from-resource.html)。

對於 Memcached，下列程式碼使用 AWS CLI ，`Region`從 us-west-2 區域中叢集`my-cluster-001`中的節點移除具有金鑰 `Service`和 `my-cluster`的標籤。

針對 Linux、macOS 或 Unix：

```
aws elasticache remove-tags-from-resource \
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}} \
 --tag-keys {{PM Service}}
```

針對 Windows：

```
aws elasticache remove-tags-from-resource ^
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster}} ^
 --tag-keys {{PM Service}}
```

對於 Redis OSS，下列程式碼使用 AWS CLI ，`Region`從 us-west-2 區域中叢集`my-cluster-001`中的節點移除具有金鑰 `Service`和 `my-cluster`的標籤。

針對 Linux、macOS 或 Unix：

```
aws elasticache remove-tags-from-resource \
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}} \
 --tag-keys {{PM Service}}
```

針對 Windows：

```
aws elasticache remove-tags-from-resource ^
 --resource-name arn:aws:elasticache:{{us-west-2:0123456789:cluster:my-cluster-001}} ^
 --tag-keys {{PM Service}}
```

此操作的輸出看起來應該類似以下，這是在操作後資源上所有標籤的清單。

```
{
   "TagList": []
}
```

如需詳細資訊，請參閱適用於 ElastiCache AWS CLI 的 [remove-tags-from-resource](https://docs.aws.amazon.com/cli/latest/reference/elasticache/remove-tags-from-resource.html)。