

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

# 使用 偵測不合規的 Lambda 部署和組態 AWS Config
<a name="governance-config-detection"></a>

除了[主動評估](governance-config.md)之外， AWS Config 也可以被動偵測不符合控管政策的資源部署和組態。這一點很重要，因為控管政策會隨著您的組織學習和實作新的最佳實務而演進。

請考慮在部署或更新 Lambda 函數時設定全新政策的情形：所有 Lambda 函數都必須始終使用特定且經過核准的 Lambda 層版本。您可以將 AWS Config 設為監控新的或更新函數的層組態。如果 AWS Config 偵測到未使用已核准 layer 版本的函數，則會將該函數標記為不合規資源。您可以選擇使用 AWS Systems Manager 自動化文件指定修復動作， AWS Config 以設定 自動修復資源。例如，您可以使用 在 Python 中撰寫自動化文件 適用於 Python (Boto3) 的 AWS SDK，這會更新不合規函數以指向核准的 layer 版本。因此， 同時 AWS Config 做為偵測和修正控制，自動化合規管理。

我們將此過程分解為三個重要的實作階段：

 ![\[The three implementation phases are identify, notify, and deploy remediation.\]](http://docs.aws.amazon.com/zh_tw/lambda/latest/dg/images/governance-config-detective-1.png) 

## 階段 1：確定存取資源
<a name="governance-config-detective-identify"></a>

首先在您的帳戶 AWS Config 中啟用 ，並將其設定為記錄 AWS Lambda 函數。這可讓 AWS Config 觀察何時建立或更新 Lambda 函數。然後，您可以設定[自訂政策規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_cfn-guard.html)，以檢查使用 AWS CloudFormation Guard 語法的特定策略違規情況。Guard 規則採用下列一般形式：

```
rule name when condition { assertion }
```

以下是一條範例規則，用於檢查確保層未被設為舊的層版本：

```
rule desiredlayer when configuration.layers !empty {
    some configuration.layers[*].arn != CONFIG_RULE_PARAMETERS.OldLayerArn
}
```

我們來了解一下規則語法和結構：
+ **規則名稱：**在提供的範例中，規則的名稱為 `desiredlayer`。
+ **條件：**此子句指定應該檢查規則的條件。在提供的範例中，條件為 `configuration.layers !empty`。這意味著只有當組態中的 `layers` 屬性不為空時，才應評估資源。
+ **聲明：**在 `when` 子句後，聲明將決定檢查什麼規則。聲明 `some configuration.layers[*].arn != CONFIG_RULE_PARAMETERS.OldLayerArn` 會檢查任何 Lambda 層 ARN 是否與 `OldLayerArn` 值不相符。如果不相符，聲明為 true 且規則通過；否則，它將會失敗。

`CONFIG_RULE_PARAMETERS` 是一組使用 AWS Config 規則設定的特殊參數。在這種情況下，`OldLayerArn` 是 `CONFIG_RULE_PARAMETERS` 內的一項參數。它允許使用者提供其認為舊的或已棄用的特定 ARN 值，然後，規則會檢查任何 Lambda 函數是否使用這個舊的 ARN 值。

## 階段 2：視覺化與設計
<a name="governance-config-detective-visualize"></a>

AWS Config 會收集組態資料，並將該資料存放在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中。您可以使用 [Amazon Athena](https://aws.amazon.com/athena/) 直接從 S3 儲存貯體查詢這些資料。藉助 Athena，您可以在組織層級彙整此類資料，為所有帳戶產生資源組態的整體全貌。若要設定資源組態資料的彙總，請參閱 AWS 雲端操作和管理部落格上的[使用 Athena 和 Amazon Quick 視覺化 AWS Config 資料](https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/)。

以下是使用特定層 ARN 識別所有 Lambda 函數的範例 Athena 查詢：

```
WITH unnested AS (
    SELECT
      item.awsaccountid AS account_id,
      item.awsregion AS region,
      item.configuration AS lambda_configuration,
      item.resourceid AS resourceid,
      item.resourcename AS resourcename,
      item.configuration AS configuration,
      json_parse(item.configuration) AS lambda_json
    FROM
      default.aws_config_configuration_snapshot,
      UNNEST(configurationitems) as t(item)
    WHERE
      "dt" = 'latest'
      AND item.resourcetype = 'AWS::Lambda::Function'
  )
  
  SELECT DISTINCT
    region as Region,
    resourcename as FunctionName,
    json_extract_scalar(lambda_json, '$.memorySize') AS memory_size,
    json_extract_scalar(lambda_json, '$.timeout') AS timeout,
    json_extract_scalar(lambda_json, '$.version') AS version
  FROM
    unnested
  WHERE
    lambda_configuration LIKE '%arn:aws:lambda:us-east-1:111122223333:layer:AnyGovernanceLayer:24%'
```

以下是查詢的結果：

 ![\[Query results in Athena console.\]](http://docs.aws.amazon.com/zh_tw/lambda/latest/dg/images/governance-config-detective-2.png) 

透過彙總整個組織 AWS Config 的資料，您可以使用 [Amazon Quick](https://aws.amazon.com/quicksight/) 建立儀表板。透過將 Athena 結果匯入 Quick，您可以將 Lambda 函數遵循 layer 版本規則的程度視覺化。此儀表板可以突出顯示合規和不合規的資源，依[下一個區段](#governance-config-detective-implement)中所述協助您確定強制政策。下圖是一個範例儀表板，它會報告在組織內部套用至函數的層版本的分佈情況。

 ![\[Example Quick dashboard shows distribution of layer versions in Lambda functions.\]](http://docs.aws.amazon.com/zh_tw/lambda/latest/dg/images/governance-config-detective-3.png) 

## 階段 3：實作與強制執行
<a name="governance-config-detective-implement"></a>

您現在可以選擇性地將您在[階段 1](#governance-config-detective-identify) 中建立的層版本規則與透過 Systems Manager 自動化文件執行的修補動作配對，該自動化文件是您使用 適用於 Python (Boto3) 的 AWS SDK編寫的 Python 指令碼。此指令碼會為每個 Lambda 函數呼叫 [UpdateFunctionConfiguration](https://docs.aws.amazon.com/lambda/latest/api/API_UpdateFunctionConfiguration.html) API 動作，使用新的層 ARN 更新函數組態。或者，您可以讓指令碼提交提取請求至程式碼儲存庫，以更新層 ARN。這樣，未來的程式碼部署也會使用正確的層 ARN 進行更新。