

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

# 新增 AWS AppConfig Agent Lambda 延伸模組
<a name="appconfig-integration-lambda-extensions-add"></a>

若要使用 AWS AppConfig Agent Lambda 延伸模組，您需要將延伸模組新增至 Lambda。這可以透過將 AWS AppConfig Agent Lambda 擴充功能作為 layer 新增至 Lambda 函數，或啟用 Lambda 函數上的擴充功能作為容器映像來完成。

**注意**  
 AWS AppConfig 延伸模組與執行時間無關，並支援所有執行時間。

**開始之前**  
啟用 AWS AppConfig Agent Lambda 延伸模組之前，請執行下列動作：
+ 整理 Lambda 函數中的組態，以便將其外部化 AWS AppConfig。
+ 建立 AWS AppConfig 成品和組態資料，包括功能旗標或自由格式組態資料。如需詳細資訊，請參閱[在 中建立功能旗標和自由格式組態資料 AWS AppConfig](creating-feature-flags-and-configuration-data.md)。
+ 將 `appconfig:StartConfigurationSession`和 `appconfig:GetLatestConfiguration` 新增至 Lambda 函數執行角色所使用的 AWS Identity and Access Management (IAM) 政策。如需詳細資訊，請參閱《AWS Lambda 開發人員指南》**中的 [AWS Lambda 執行角色](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html)。如需許可的詳細資訊 AWS AppConfig ，請參閱*《服務授權參考*》中的 [的動作、資源和條件索引鍵 AWS AppConfig](https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsappconfig.html)。

## 使用 layer 和 ARN 新增 AWS AppConfig Agent Lambda 擴充功能
<a name="appconfig-integration-lambda-extensions-enabling"></a>

若要使用 AWS AppConfig Agent Lambda 延伸模組，請將延伸模組做為 layer 新增至 Lambda 函數。如需如何將 layer 新增至函數的詳細資訊，請參閱《 *AWS Lambda 開發人員指南*》中的[設定擴充](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html#using-extensions-config)功能。 AWS Lambda 主控台中延伸的名稱為 **AWS-AppConfig-Extension**。另請注意，當您將延伸項目做為圖層新增至 Lambda 時，您必須指定 Amazon Resource Name (ARN)。從下列其中一個與平台以及您建立 Lambda AWS 區域 的位置對應的清單中選擇 ARN。
+ [x86-64 平台](appconfig-integration-lambda-extensions-versions.md#appconfig-integration-lambda-extensions-enabling-x86-64)
+ [ARM64 平台](appconfig-integration-lambda-extensions-versions.md#appconfig-integration-lambda-extensions-enabling-ARM64)

如果您想要在將延伸模組新增至函數之前進行測試，您可以使用下列程式碼範例來驗證該延伸模組是否正常運作。

```
import urllib.request
                

def lambda_handler(event, context):
    url = f'http://localhost:2772/applications/application_name/environments/environment_name/configurations/configuration_name'
    config = urllib.request.urlopen(url).read()
    return config
```

若要進行測試，請為 Python 建立新的 Lambda 函數、新增延伸模組，然後執行 Lambda 函數。執行 Lambda 函數之後， AWS AppConfig Lambda 函數會傳回您為 http://localhost:2772 路徑指定的組態。如需有關建立 Lambda 函數的資訊，請參閱《 *AWS Lambda 開發人員指南*》中的[使用主控台建立 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/getting-started-create-function.html)。

**重要**  
您可以在日誌中檢視 AWS AppConfig Agent Lambda 延伸模組的 AWS Lambda 日誌資料。日誌項目的字首為 `appconfig agent`。範例如下。  

```
[appconfig agent] 2024/05/07 04:19:01 ERROR retrieve failure for 'SourceEventConfig:SourceEventConfigEnvironment:SourceEventConfigProfile': StartConfigurationSession: api error AccessDenied: User: arn:aws:sts::0123456789:assumed-role/us-east-1-LambdaRole/extension1 is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::0123456789:role/test1 (retry in 60s)
```