

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

# 逐步解說：建立自訂 AWS AppConfig 擴充功能
<a name="working-with-appconfig-extensions-creating-custom"></a>

若要建立自訂 AWS AppConfig 擴充功能，請完成下列任務。在稍後的主題中，會更詳細地描述每個任務。

**注意**  
您可以在 GitHub 上檢視自訂 AWS AppConfig 擴充功能的範例：  
[使用 Systems Manager `blocked day` 變更行事曆透過暫時行事曆防止部署的範例延伸模組](https://github.com/aws-samples/aws-appconfig-change-calendar-extn)
[避免秘密使用 git-secrets 洩漏至組態資料的範例延伸](https://github.com/aws-samples/aws-appconfig-git-secrets-extn)
[避免個人身分識別資訊 (PII) 使用 Amazon Comprehend 洩漏至組態資料的範例延伸](https://github.com/aws-samples/aws-appconfig-pii-extn)

**1. [建立 AWS Lambda 函數](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-creating-custom-lambda.html)**  
對於大多數使用案例，若要建立自訂延伸模組，您必須建立 AWS Lambda 函數來執行延伸模組中定義的任何運算和處理。此規則的例外是，如果您建立[AWS 撰寫通知延伸](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined.html)項目的*自訂*版本，以新增或移除動作點。如需此例外狀況的詳細資訊，請參閱 [步驟 3：建立自訂 AWS AppConfig 延伸模組](working-with-appconfig-extensions-creating-custom-extensions.md)。

**2. [設定自訂擴充功能的許可](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-creating-custom-permissions.html)**  
若要設定自訂擴充功能的許可，您可以執行下列其中一項操作：  
+ 建立包含`InvokeFunction`許可的 AWS Identity and Access Management (IAM) 服務角色。
+ 使用 Lambda [AddPermission](https://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html) API 動作建立資源政策。
本演練說明如何建立 IAM 服務角色。

**3. [建立延伸模組](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-creating-custom-extensions.html)**  
您可以使用 AWS AppConfig 主控台，或從 或 SDK 呼叫 [CreateExtension](https://docs.aws.amazon.com/appconfig/2019-10-09/APIReference/API_CreateExtension.html) API 動作 AWS CLI AWS Tools for PowerShell來建立擴充功能。演練使用 主控台。

**4. [建立延伸模組關聯](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-creating-custom-association.html)**  
您可以使用 AWS AppConfig 主控台，或從 或 SDK 呼叫 [CreateExtensionAssociation](https://docs.aws.amazon.com/appconfig/2019-10-09/APIReference/API_CreateExtensionAssociation.html) API 動作 AWS CLI AWS Tools for PowerShell，來建立延伸關聯。演練使用 主控台。

**5. 執行叫用延伸模組的動作**  
建立關聯之後，當該資源發生延伸定義的動作點時， 會 AWS AppConfig 叫用延伸。例如，如果您關聯包含`PRE_CREATE_HOSTED_CONFIGURATION_VERSION`動作的延伸模組，則每次建立新的託管組態版本時都會叫用該延伸模組。

本節中的主題說明建立自訂 AWS AppConfig 擴充功能時涉及的每個任務。每個任務都會在客戶想要建立擴充功能，以自動將組態備份到 Amazon Simple Storage Service (Amazon S3) 儲存貯體的使用案例中描述。延伸項目會在建立託管組態 (`PRE_CREATE_HOSTED_CONFIGURATION_VERSION`) 或部署 () 時執行`PRE_START_DEPLOYMENT`。

**Topics**
+ [步驟 1：為自訂 AWS AppConfig 延伸模組建立 Lambda 函數](working-with-appconfig-extensions-creating-custom-lambda.md)
+ [步驟 2：設定自訂 AWS AppConfig 擴充功能的許可](working-with-appconfig-extensions-creating-custom-permissions.md)
+ [步驟 3：建立自訂 AWS AppConfig 延伸模組](working-with-appconfig-extensions-creating-custom-extensions.md)
+ [步驟 4：建立自訂擴充功能的 AWS AppConfig 擴充功能關聯](working-with-appconfig-extensions-creating-custom-association.md)

# 步驟 1：為自訂 AWS AppConfig 延伸模組建立 Lambda 函數
<a name="working-with-appconfig-extensions-creating-custom-lambda"></a>

對於大多數使用案例，若要建立自訂擴充功能，您必須建立 AWS Lambda 函數來執行擴充功能中定義的任何運算和處理。本節包含自訂 AWS AppConfig 擴充功能的 Lambda 函數範例程式碼。本節也包含承載請求和回應參考詳細資訊。如需有關建立 Lambda 函數的資訊，請參閱《 *AWS Lambda 開發人員指南*》中的 [Lambda 入門](https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html)。

## 範本程式碼
<a name="working-with-appconfig-extensions-creating-custom-lambda-code-sample"></a>

叫用 Lambda 函數時，下列範例程式碼會自動將 AWS AppConfig 組態備份到 Amazon S3 儲存貯體。每當建立新組態或部署時，就會備份組態。範例使用延伸參數，因此不需要在 Lambda 函數中硬式編碼儲存貯體名稱。透過使用延伸參數，使用者可以將延伸模組連接至多個應用程式，並將組態備份至不同的儲存貯體。程式碼範例包含註解，以進一步說明函數。

** AWS AppConfig 延伸模組的範例 Lambda 函數**

```
from datetime import datetime
import base64
import json

import boto3


def lambda_handler(event, context):
    print(event)
    
    # Extensions that use the PRE_CREATE_HOSTED_CONFIGURATION_VERSION and PRE_START_DEPLOYMENT 
    # action points receive the contents of AWS AppConfig configurations in Lambda event parameters.
    # Configuration contents are received as a base64-encoded string, which the lambda needs to decode 
    # in order to get the configuration data as bytes. For other action points, the content 
    # of the configuration isn't present, so the code below will fail.
    config_data_bytes = base64.b64decode(event["Content"])
    
    # You can specify parameters for extensions. The CreateExtension API action lets you define  
    # which parameters an extension supports. You supply the values for those parameters when you 
    # create an extension association by calling the CreateExtensionAssociation API action.
    # The following code uses a parameter called S3_BUCKET to obtain the value specified in the 
    # extension association. You can specify this parameter when you create the extension 
    # later in this walkthrough.
    extension_association_params = event.get('Parameters', {})
    bucket_name = extension_association_params['S3_BUCKET']
    write_backup_to_s3(bucket_name, config_data_bytes)
    
    # The PRE_CREATE_HOSTED_CONFIGURATION_VERSION and PRE_START_DEPLOYMENT action points can 
    # modify the contents of a configuration. The following code makes a minor change 
    # for the purposes of a demonstration.
    old_config_data_string = config_data_bytes.decode('utf-8')
    new_config_data_string = old_config_data_string.replace('hello', 'hello!')
    new_config_data_bytes = new_config_data_string.encode('utf-8')
    
    # The lambda initially received the configuration data as a base64-encoded string 
    # and must return it in the same format.
    new_config_data_base64string = base64.b64encode(new_config_data_bytes).decode('ascii')
    
    return {
        'statusCode': 200,
        # If you want to modify the contents of the configuration, you must include the new contents in the 
        # Lambda response. If you don't want to modify the contents, you can omit the 'Content' field shown here.
        'Content': new_config_data_base64string
    }


def write_backup_to_s3(bucket_name, config_data_bytes):
    s3 = boto3.resource('s3')
    new_object = s3.Object(bucket_name, f"config_backup_{datetime.now().isoformat()}.txt")
    new_object.put(Body=config_data_bytes)
```

如果您想要在此演練期間使用此範例，請將其儲存為名稱，**MyS3ConfigurationBackUpExtension**並複製函數的 Amazon Resource Name (ARN)。您可以在下一節中建立 AWS Identity and Access Management (IAM) 擔任角色時指定 ARN。您可以在建立擴充功能時指定 ARN 和名稱。

## 承載參考
<a name="working-with-appconfig-extensions-creating-custom-lambda-payload"></a>

本節包含使用自訂 AWS AppConfig 擴充功能的承載請求和回應參考詳細資訊。

**請求結構**  
*AtDeploymentTick*

```
{
    'InvocationId': 'o2xbtm7',
    'Parameters': {
        'ParameterOne': 'ValueOne',
        'ParameterTwo': 'ValueTwo'
    },
    'Type': 'OnDeploymentStart',
    'Application': {
        'Id': 'abcd123'
    },
    'Environment': {
        'Id': 'efgh456'
    },
    'ConfigurationProfile': {
        'Id': 'ijkl789',
        'Name': 'ConfigurationName'
    },
    'DeploymentNumber': 2,
    'Description': 'Deployment description',
    'ConfigurationVersion': '2',
    'DeploymentState': 'DEPLOYING',
    'PercentageComplete': '0.0'
}
```

**請求結構**  
*PreCreateHostedConfigurationVersion*

```
{
    'InvocationId': 'vlns753', // id for specific invocation
    'Parameters': {
        'ParameterOne': 'ValueOne',
        'ParameterTwo': 'ValueTwo'
    },
    'ContentType': 'text/plain',
    'ContentVersion': '2',
    'Content': 'SGVsbG8gZWFydGgh', // Base64 encoded content
    'Application': {
        'Id': 'abcd123',
        'Name': 'ApplicationName'
    },
    'ConfigurationProfile': {
        'Id': 'ijkl789',
        'Name': 'ConfigurationName'
    },
    'Description': '',
    'Type': 'PreCreateHostedConfigurationVersion',
    'PreviousContent': {
        'ContentType': 'text/plain',
        'ContentVersion': '1',
        'Content': 'SGVsbG8gd29ybGQh'
    }
}
```

*PreStartDeployment*

```
{
    'InvocationId': '765ahdm',
    'Parameters': {
        'ParameterOne': 'ValueOne',
        'ParameterTwo': 'ValueTwo'
    },
    'ContentType': 'text/plain',
    'ContentVersion': '2',
    'Content': 'SGVsbG8gZWFydGgh',
    'Application': {
        'Id': 'abcd123',
        'Name': 'ApplicationName'
    },
    'Environment': {
        'Id': 'ibpnqlq',
        'Name': 'EnvironmentName'
    },
    'ConfigurationProfile': {
        'Id': 'ijkl789',
        'Name': 'ConfigurationName'
    },
    'DeploymentNumber': 2,
    'Description': 'Deployment description',
    'Type': 'PreStartDeployment'
}
```

**非同步事件**  


*OnStartDeployment、OnDeploymentStep、OnDeployment*

```
{
    'InvocationId': 'o2xbtm7',
    'Parameters': {
        'ParameterOne': 'ValueOne',
        'ParameterTwo': 'ValueTwo'
    },
    'Type': 'OnDeploymentStart',
    'Application': {
        'Id': 'abcd123'
    },
    'Environment': {
        'Id': 'efgh456'
    },
    'ConfigurationProfile': {
        'Id': 'ijkl789',
        'Name': 'ConfigurationName'
    },
    'DeploymentNumber': 2,
    'Description': 'Deployment description',
    'ConfigurationVersion': '2'
}
```

**回應結構**  
下列範例顯示 Lambda 函數傳回的內容，以回應自訂 AWS AppConfig 擴充功能的請求。

*PRE\$1\$1 同步事件 - 成功回應*

如果您想要轉換內容，請使用下列項目：

```
"Content": "SomeBase64EncodedByteArray"
```

*AT\$1\$1 同步事件 - 成功回應*

如果您想要控制部署的後續步驟 （繼續部署或復原），請在回應中設定 `Directive`和 `Description` 屬性。

```
"Directive": "ROLL_BACK"
"Description" "Deployment event log description"
```

`Directive` 支援兩個值： `CONTINUE`或 `ROLL_BACK`。在承載回應中使用這些列舉來控制部署的後續步驟。

*同步事件 - 成功回應*

如果您想要轉換內容，請使用下列項目：

```
"Content": "SomeBase64EncodedByteArray"
```

如果您不想轉換內容，則不傳回任何內容。

*非同步事件 - 成功回應*

不傳回任何內容。

*所有錯誤事件*

```
{
        "Error": "BadRequestError",
        "Message": "There was malformed stuff in here",
        "Details": [{
            "Type": "Malformed",
            "Name": "S3 pointer",
            "Reason": "S3 bucket did not exist"
        }]
    }
```

# 步驟 2：設定自訂 AWS AppConfig 擴充功能的許可
<a name="working-with-appconfig-extensions-creating-custom-permissions"></a>

使用下列程序來建立和設定 AWS Identity and Access Management (IAM) 服務角色 （或*擔任角色*)。 AWS AppConfig 使用此角色來叫用 Lambda 函數。

**建立 IAM 服務角色並允許 AWS AppConfig 擔任該角色**

1. 在以下網址開啟 IAM 主控台：[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。

1. 在導覽窗格中，選擇**角色**，然後選擇**建立角色**。

1. 在**選取信任實體的類型**下，選擇**自訂信任政策**。

1. 將下列 JSON 政策貼到**自訂信任政策**欄位中。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": {
           "Service": "appconfig.amazonaws.com"
         },
         "Action": "sts:AssumeRole"
       }
     ]
   }
   ```

------

   選擇**下一步**。

1. 在**新增許可**頁面上，選擇**建立政策**。**Create policy (建立政策)** 頁面隨即在新標籤中開啟。

1. 選擇 **JSON** 索引標籤，然後將下列許可政策貼入編輯器。`lambda:InvokeFunction` 動作用於`PRE_*`動作點。`lambda:InvokeAsync` 動作用於`ON_*`動作點。將 *Lambda ARN* 取代為 Lambda 的 Amazon Resource Name (ARN)。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Sid": "VisualEditor0",
         "Effect": "Allow",
         "Action": [
           "lambda:InvokeFunction",
           "lambda:InvokeAsync"
         ],
         "Resource": "arn:aws:lambda:us-east-1:111122223333:function:function-name"
       }
     ]
   }
   ```

------

1. 選擇下**一步：標籤**。

1. 在**新增標籤 （選用）** 頁面上，新增一或多個鍵值對，然後選擇**下一步：檢閱**。

1. 在**檢閱政策**頁面上，輸入名稱和描述，然後選擇**建立政策**。

1. 在自訂信任政策的瀏覽器索引標籤上，選擇重新整理圖示，然後搜尋您剛建立的許可政策。

1. 選取許可政策的核取方塊，然後選擇**下一步**。

1. 在**名稱、檢閱和建立**頁面上，在**角色名稱**方塊中輸入名稱，然後輸入描述。

1. 選擇 **Create role** (建立角色)。系統會讓您回到 **Roles (角色)** 頁面。在橫幅中選擇**檢視角色**。

1. 複製 ARN。您可以在建立擴充功能時指定此 ARN。

# 步驟 3：建立自訂 AWS AppConfig 延伸模組
<a name="working-with-appconfig-extensions-creating-custom-extensions"></a>



延伸定義一或多個在 AWS AppConfig 工作流程期間執行的動作。例如， AWS 撰寫的`AWS AppConfig deployment events to Amazon SNS`延伸包含將通知傳送至 Amazon SNS 主題的動作。當您與 互動或 代表您 AWS AppConfig 執行程序 AWS AppConfig 時，會叫用每個動作。這些稱為*動作點*。 AWS AppConfig extensions 支援下列動作點：

**PRE\$1\$1 動作點**：在請求驗證之後，但在 執行與`PRE_*`動作點名稱對應的活動之前 AWS AppConfig ，會套用在動作點上設定的延伸動作。這些動作叫用會與請求同時處理。如果提出多個請求，動作調用會依序執行。另請注意，`PRE_*`動作點接收並可以變更組態的內容。 `PRE_*`動作點也可以回應錯誤並防止動作發生。
+ `PRE_CREATE_HOSTED_CONFIGURATION_VERSION`
+ `PRE_START_DEPLOYMENT`

**ON\$1\$1 動作點**：延伸項目也可以使用`ON_*`動作點與 AWS AppConfig 工作流程平行執行。 `ON_*` 動作點會以非同步方式叫用。 `ON_*`動作點不會接收組態的內容。如果延伸模組在`ON_*`動作點期間發生錯誤，服務會忽略錯誤並繼續工作流程。
+ `ON_DEPLOYMENT_START`
+ `ON_DEPLOYMENT_STEP`
+ `ON_DEPLOYMENT_BAKING`
+ `ON_DEPLOYMENT_COMPLETE`
+ `ON_DEPLOYMENT_ROLLED_BACK`

**AT\$1\$1 動作點**：在`AT_*`動作點上設定的延伸動作會與工作流程同步和平行 AWS AppConfig 叫用。如果延伸模組在`AT_*`動作點期間發生錯誤，服務會停止工作流程並復原部署。
+ `AT_DEPLOYMENT_TICK`

`AT_DEPLOYMENT_TICK` 動作點支援第三方監控整合。 `AT_DEPLOYMENT_TICK` 在組態部署處理協調期間調用 。如果您使用第三方監控解決方案 （例如 Datadog 或 New Relic)，您可以建立 AWS AppConfig 擴充功能，在`AT_DEPLOYMENT_TICK`動作點檢查警示，並在觸發警示時復原部署，做為安全防護機制。

如果您使用 Datadog 或 New Relic 等第三方監控解決方案，您可以建立 AWS AppConfig 擴充功能，在`AT_DEPLOYMENT_TICK`動作點檢查警示，並在觸發警示時復原部署，做為安全防護機制。如需詳細資訊，請參閱 GitHub 上的下列 Datadog 和 New Relic 整合範例：
+ [Datadog](https://github.com/aws-samples/aws-appconfig-tick-extn-for-datadog)
+ [New Relic](https://github.com/aws-samples/sample-aws-appconfig-tick-extn-for-newrelic)

如需 AWS AppConfig 延伸模組的詳細資訊，請參閱下列主題：
+ [使用延伸模組擴展 AWS AppConfig 工作流程](working-with-appconfig-extensions.md)
+ [逐步解說：建立自訂 AWS AppConfig 擴充功能](working-with-appconfig-extensions-creating-custom.md)

**擴充功能範例**  
下列範例延伸定義一個呼叫動作點`PRE_CREATE_HOSTED_CONFIGURATION_VERSION`的動作。在 `Uri`欄位中，動作會指定本演練稍早所建立 `MyS3ConfigurationBackUpExtension` Lambda 函數的 Amazon Resource Name (ARN)。此動作也會指定本演練稍早建立的 AWS Identity and Access Management (IAM) 擔任角色 ARN。

**擴充 AWS AppConfig 功能範例**

```
{
    "Name": "MySampleExtension",
    "Description": "A sample extension that backs up configurations to an S3 bucket.",
    "Actions": {
        "PRE_CREATE_HOSTED_CONFIGURATION_VERSION": [
            {
                "Name": "PreCreateHostedConfigVersionActionForS3Backup",
                "Uri": "arn:aws:lambda:aws-region:111122223333:function:MyS3ConfigurationBackUpExtension",
                "RoleArn": "arn:aws:iam::111122223333:role/ExtensionsTestRole"
            }
        ]
    },
    "Parameters" : {
        "S3_BUCKET": {
            "Required": false
        }
    }
}
```

**注意**  
若要在建立延伸項目時檢視請求語法和欄位描述，請參閱 *AWS AppConfig API 參考*中的 [CreateExtension](https://docs.aws.amazon.com/appconfig/2019-10-09/APIReference/API_CreateExtension.html) 主題。

**建立延伸模組 （主控台）**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/appconfig/](https://console.aws.amazon.com/systems-manager/appconfig/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **AWS AppConfig**。

1. 在**延伸項目**索引標籤上，選擇**建立延伸項目**。

1. 針對**延伸項目名稱**，輸入唯一的名稱。為了本演練的目的，請輸入 **MyS3ConfigurationBackUpExtension**。或者，輸入描述。

1. 在**動作**區段中，選擇**新增動作**。

1. 在**名稱**欄位中輸入唯一的名稱。為了本演練的目的，請輸入 **PreCreateHostedConfigVersionActionForS3Backup**。此名稱說明 動作所使用的動作點和延伸用途。

1. 在**動作點**清單中，選擇 **PRE\$1CREATE\$1HOSTED\$1CONFIGURATION\$1VERSION**。

1. 針對 **Uri**，選擇 **Lambda 函數**，然後在 **Lambda 函數**清單中選擇函數。如果您沒有看到函數，請確認您位於建立函數 AWS 區域 的相同位置。

1. 針對 **IAM 角色**，選擇您在此演練中稍早建立的角色。

1. 在**延伸參數 （選用）** 區段中，選擇**新增參數**。

1. 針對**參數名稱**，輸入名稱。為了本演練的目的，請輸入 **S3\$1BUCKET**。

1. 重複步驟 5–11，為動作點建立第二個`PRE_START_DEPLOYMENT`動作。

1. 選擇**建立延伸**模組。

# 自訂 AWS 撰寫的通知擴充功能
<a name="working-with-appconfig-extensions-creating-custom-notification"></a>

您不需要建立 Lambda 或擴充功能，即可使用[AWS 撰寫的通知擴充功能](https://docs.aws.amazon.com/appconfig/latest/userguide/working-with-appconfig-extensions-about-predefined.html)。您可以直接建立延伸關聯，然後執行呼叫其中一個支援動作點的操作。根據預設， AWS 撰寫的通知延伸支援下列動作點：
+ `ON_DEPLOYMENT_START`
+ `ON_DEPLOYMENT_COMPLETE`
+ `ON_DEPLOYMENT_ROLLED_BACK`

如果您建立`AWS AppConfig deployment events to Amazon SNS`擴充功能和`AWS AppConfig deployment events to Amazon SQS`擴充功能的自訂版本，您可以指定要接收通知的動作點。

**注意**  
`AWS AppConfig deployment events to EventBridge` 延伸模組不支援`PRE_*`動作點。如果您想要移除指派給 AWS 撰寫版本的一些預設動作點，您可以建立自訂版本。

如果您建立撰寫的通知延伸項目的自訂版本， AWS 則不需要建立 Lambda 函數。您只需在新延伸版本的 `Uri` 欄位中指定 Amazon Resource Name (ARN)。
+ 對於自訂 EventBridge 通知延伸，在`Uri`欄位中輸入 EventBridge 預設事件的 ARN。
+ 對於自訂 Amazon SNS 通知延伸，在`Uri`欄位中輸入 Amazon SNS 主題的 ARN。
+ 對於自訂 Amazon SQS 通知延伸，在`Uri`欄位中輸入 Amazon SQS 訊息佇列的 ARN。

# 步驟 4：建立自訂擴充功能的 AWS AppConfig 擴充功能關聯
<a name="working-with-appconfig-extensions-creating-custom-association"></a>

若要建立延伸模組，或設定 AWS 撰寫的延伸模組，您可以定義在使用特定 AWS AppConfig 資源時呼叫延伸模組的動作點。例如，您可以選擇在啟動特定應用程式的組態部署時，執行`AWS AppConfig deployment events to Amazon SNS`擴充功能並接收 Amazon SNS 主題的通知。定義哪些動作點叫用特定 AWS AppConfig 資源的延伸稱為*延伸關聯*。延伸關聯是延伸與 AWS AppConfig 資源之間的指定關係，例如應用程式或組態描述檔。

單一 AWS AppConfig 應用程式可以包含多個環境和組態設定檔。如果您將延伸項目與應用程式或環境建立關聯， 會針對與應用程式或環境資源相關的任何工作流程 AWS AppConfig 叫用延伸項目，如果適用的話。

例如，假設您有一個名為 MobileApps AWS AppConfig 的應用程式，其中包含名為 AccessList 的組態設定檔。假設 MobileApps 應用程式包含 Beta、整合和生產環境。您可以為撰寫的 Amazon SNS AWS 通知延伸模組建立延伸模組關聯，並將延伸模組與 MobileApps 應用程式建立關聯。每當應用程式組態部署到三個環境中的任何一個環境時，就會叫用 Amazon SNS 通知延伸模組。

使用下列程序，透過 AWS AppConfig 主控台建立 AWS AppConfig 延伸關聯。

**建立延伸關聯 （主控台）**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/appconfig/](https://console.aws.amazon.com/systems-manager/appconfig/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **AWS AppConfig**。

1. 在**延伸**項目索引標籤上，選擇延伸項目的選項按鈕，然後選擇**新增至資源**。為了本演練的目的，請選擇 **MyS3ConfigurationBackUpExtension**。

1. 在**延伸資源詳細資訊**區段中，針對**資源類型**選擇 AWS AppConfig 資源類型。根據您選擇的資源， 會 AWS AppConfig 提示您選擇其他資源。為了本演練的目的，請選擇**應用程式**。

1. 在清單中選擇應用程式。

1. 在**參數**區段中，確認 **S3\$1BUCKET** 已列在**金鑰**欄位中。在**值**欄位中，貼上 Lambda 延伸模組的 ARN。例如：`arn:aws:lambda:aws-region:111122223333:function:MyS3ConfigurationBackUpExtension`。

1. 選擇**建立與資源的關聯**。

建立關聯之後，您可以建立`hosted`為其 指定的新組態設定檔，以叫用 `MyS3ConfigurationBackUpExtension`延伸模組`SourceUri`。在建立新組態的工作流程中， AWS AppConfig 會遇到 `PRE_CREATE_HOSTED_CONFIGURATION_VERSION` 動作點。遇到此動作點會叫用 `MyS3ConfigurationBackUpExtension` 延伸，其會自動將新建立的組態備份到延伸關聯 `Parameter` 區段中指定的 S3 儲存貯體。