

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 AWS Config 自定义 Lambda 规则管理已删除的资源
<a name="evaluate-config_develop-rules-delete"></a>

报告已删除资源的规则应返回 `NOT_APPLICABLE` 的评估结果，以避免不必要的规则评估。

当您删除资源时， AWS Config 会`ResourceDeleted`为创建一个 `configurationItem` with `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 编写规则](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_components.html#evaluate-config_components_logic)和。 RDKlib