

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

# 管理 AWS IoT 規則
<a name="iot-managae-rule"></a>

您可以使用下列動作來管理您的 AWS IoT 規則。

**Topics**
+ [標記規則](#iot-create-rule-tagging)
+ [檢視規則](#iot-view-rules)
+ [刪除規則](#iot-delete-rule)

## 標記規則
<a name="iot-create-rule-tagging"></a>

您可以套用標記，將另一層特殊性新增至新規則或現有規則。標記會利用規則中的鍵/值對，讓您更能控制規則套用至 AWS IoT 資源和服務的方式和位置。例如，您可以將規則範圍限制為僅適用於進行發行前測試的試用版環境 (`Key=environment, Value=beta`)，或擷取所有僅從特定端點傳送至 `iot/test` 的主題，並將這兩項資料存放在 Amazon S3 儲存貯體中。

### IAM 政策範例
<a name="iot-create-rule-tagging-iam-example"></a>

對於示範如何授予規則標記許可的範例，請考慮使用者執行以下命令，以建立規則並將其標記為僅適用於試用版環境。

在該範例中，替換：
+ {{MyTopleName}} 為規則的名稱。
+ {{myrule.json}} 為政策文件的名稱。

```
aws iot create-topic-rule 
    --rule-name {{MyTopicRuleName}} 
    --topic-rule-payload file://{{myrule.json}}
    --tags "environment=beta"
```

這個範例必須使用下列 IAM 政策：

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": 
    {
        "Action": [ "iot:CreateTopicRule", "iot:TagResource" ],
        "Effect": "Allow",
        "Resource": [
            "arn:aws:iot:us-east-1:123456789012:rule/{{MyTopicRuleName}}"
        ]
    }
}
```

以上範例說明名為 `MyTopicRuleName` 並僅適用於試用版環境的新建規則。政策聲明中的 `iot:TagResource` 與 `MyTopicRuleName` 特別呼叫，允許在建立或更新 `MyTopicRuleName` 時予以標記。建立規則時使用的參數 `--tags "environment=beta"` 將 `MyTopicRuleName` 範圍限制為只有您的測試版環境。如果您移除參數 `--tags "environment=beta"`，`MyTopicRuleName` 將適用於所有環境。

如需根據 AWS IoT 規則建立 IAM 角色和政策的詳細資料，請參閱 [授予 AWS IoT 規則所需的存取權](iot-create-role.md)

如需標記資源的相關資訊，請參閱 [標記您的 AWS IoT 資源](tagging-iot.md)。

## 檢視規則
<a name="iot-view-rules"></a>

請使用 [list-topic-rules](https://docs.aws.amazon.com/cli/latest/reference/iot/list-topic-rules.html) 命令來列出規則：

```
aws iot list-topic-rules
```

請使用 [get-topic-rule](https://docs.aws.amazon.com/cli/latest/reference/iot/get-topic-rule.html) 命令來取得規則的相關資訊：

```
aws iot get-topic-rule --rule-name {{myrule}}
```

## 刪除規則
<a name="iot-delete-rule"></a>

規則結束使用後即可刪除。

**刪除規則 (AWS CLI)**  
請使用 [delete-topic-rule](https://docs.aws.amazon.com/cli/latest/reference/iot/delete-topic-rule.html) 命令來刪除規則：

```
aws iot delete-topic-rule --rule-name {{myrule}}
```