

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

# 管理 AWS Config 自訂 Lambda 規則的已刪除資源
<a name="evaluate-config_develop-rules-delete"></a>

報告已刪除資源的規則，應傳回 `NOT_APPLICABLE` 的評估結果，以避免不必要的規則評估。

當您刪除資源時， 會使用 `configurationItem``ResourceDeleted`為 AWS Config 建立 `configurationItemStatus`。您可以使用此中繼資料來檢查規則是否報告已刪除的資源。如需組態項目的詳細資訊，請參閱《[概念 \$1 組態項目](https://docs.aws.amazon.com/config/latest/developerguide/config-concepts.html#config-items.html)》。

包含下列程式碼片段，以檢查已刪除的資源，並在 AWS Config 自訂 Lambda 規則報告已刪除的資源`NOT_APPLICABLE`時，將評估結果設定為 ：

------
#### [ Custom Lambda Rules (Node.js) ]

```
// Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable.
function isApplicable(configurationItem, event) {
    checkDefined(configurationItem, 'configurationItem');
    checkDefined(event, 'event');
    const status = configurationItem.configurationItemStatus;
    const eventLeftScope = event.eventLeftScope;
    return (status === 'OK' || status === 'ResourceDiscovered') && eventLeftScope === false;
}
```

------
#### [ Custom Lambda Rules (Python) ]

```
# Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable.
def is_applicable(configurationItem, event):
    try:
        check_defined(configurationItem, 'configurationItem')
        check_defined(event, 'event')
    except:
        return True
    status = configurationItem['configurationItemStatus']
    eventLeftScope = event['eventLeftScope']
    if status == 'ResourceDeleted':
        print("Resource Deleted, setting Compliance Status to NOT_APPLICABLE.")
    return (status == 'OK' or status == 'ResourceDiscovered') and not eventLeftScope
```

------

**注意**  
AWS Config 受管規則和 AWS Config 自訂政策規則預設會處理此行為。  
如果您使用 AWS Config 開發套件 (RDK) 和 AWS Config 開發套件程式庫 (RDKlib) 使用 Python 建立 AWS Config 自訂 lambd 規則，則匯入[的評估器](https://github.com/awslabs/aws-config-rdklib/blob/master/rdklib/evaluator.py#L56)類別將檢查此行為。如需如何使用 RDK 和 RDKLib 撰寫規則的相關資訊，請參閱《[使用 RDK 和 RDKLib 撰寫規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_components.html#evaluate-config_components_logic)》。