

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

# 標記 AWS Device Farm 資源
<a name="tagging"></a>

AWS Device Farm 使用 AWS 資源群組標記 API。此 API 可讓您使用*標籤*來管理 AWS 帳戶中的資源。您可以將標籤新增至資源，例如專案和測試執行。

您可以使用標籤執行以下動作：
+ 整理 AWS 帳單，以反映成本結構。方式是註冊以取得包含標籤鍵值的 AWS 帳戶帳單。接著，若要查看合併資源的成本，請根據具有相同標籤鍵值的資源來整理您的帳單資訊。例如，您可以使用應用程式名稱來標記數個資源，然後整理帳單資訊以查看該應用程式跨多項服務的總成本。如需詳細資訊，請參閱《*關於 AWS Billing and Cost Management》中的成本*[分配和標記](https://docs.aws.amazon.com//awsaccountbilling/latest/aboutv2/cost-alloc-tags.html)。
+ 透過 IAM 政策控制存取。若要執行此作業，請建立允許使用標籤值條件來存取資源或資源集的政策。
+ 識別並管理具有特定屬性做為標籤的執行，例如用於測試的分支。

 如需標記資源的詳細資訊，請參閱[標記最佳實務](https://d1.awsstatic.com/whitepapers/aws-tagging-best-practices.pdf)白皮書。

**Topics**
+ [標記資源](#tagging-resources)
+ [依標籤查詢資源](#tagging-looking-up-resources)
+ [移除資源的標籤](#tagging-removing-tags)

## 標記資源
<a name="tagging-resources"></a>

AWS 資源群組標記 API 可讓您在資源上新增、移除或修改標籤。如需詳細資訊，請參閱 [AWS 資源群組標記 API 參考](https://docs.aws.amazon.com//resourcegroupstagging/latest/APIReference/Welcome.html)。

若要標記資源，請使用`resourcegroupstaggingapi`端點中的 [https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_TagResources.html](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_TagResources.html)操作。此操作會取得支援服務的 ARN 清單以及金鑰/值對的清單。此值是選用的。空字串表示該標籤不應有任何值。例如：以下 Python 範例使用值為 `release` 的標籤 `build-config` 來標記一連串的專案 ARN：

```
import boto3

client = boto3.client('resourcegroupstaggingapi')

client.tag_resources(ResourceARNList=["arn:aws:devicefarm:us-west-2:111122223333:project:123e4567-e89b-12d3-a456-426655440000",
                                      "arn:aws:devicefarm:us-west-2:111122223333:project:123e4567-e89b-12d3-a456-426655441111",
                                      "arn:aws:devicefarm:us-west-2:111122223333:project:123e4567-e89b-12d3-a456-426655442222"]
                     Tags={"build-config":"release", "git-commit":"8fe28cb"})
```

標籤值為非必要。若要設定沒有值的標籤，請在指定值時使用空字串 (`""`)。一個標籤只能有一個值。資源的標籤先前具有的任何值都將被新值覆寫。

## 依標籤查詢資源
<a name="tagging-looking-up-resources"></a>

若要依標籤查閱資源，請從 `resourcegrouptaggingapi` 端點使用 `GetResources` 操作。此操作採用一連串非必要的篩選條件，傳回符合指定條件的資源。如果沒有篩選條件，則會傳回所有標記的資源。`GetResources` 操作允許您根據下列條件篩選資源
+ 標籤值
+ 資源類型 (例如 `devicefarm:run`)

如需詳細資訊，請參閱 [AWS 資源群組標記 API 參考](https://docs.aws.amazon.com//resourcegroupstagging/latest/APIReference/Welcome.html)。

下列範例會使用值`stack`為 的標籤查詢 Device Farm 桌面瀏覽器測試工作階段 (`devicefarm:testgrid-session` 資源）`production`：

```
import boto3
client = boto3.client('resourcegroupstaggingapi')
sessions = client.get_resources(ResourceTypeFilters=['devicefarm:testgrid-session'],
                                TagFilters=[
                                  {"Key":"stack","Values":["production"]}
                                ])
```

## 移除資源的標籤
<a name="tagging-removing-tags"></a>

若要移除標籤，請使用 `UntagResources` 操作，指定資源清單以及要移除的標籤：

```
import boto3
client = boto3.client('resourcegroupstaggingapi')
client.UntagResources(ResourceARNList=["arn:aws:devicefarm:us-west-2:111122223333:project:123e4567-e89b-12d3-a456-426655440000"], TagKeys=["RunCI"])
```