本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
對於大多數用例,要創建自定義擴展,必須創建一個 AWS Lambda 函數來執行擴展中定義的任何計算和處理。本節包含自訂擴充功能的 Lambda 函數範例程式 AWS AppConfig 碼。本節還包括有效負載請求和響應參考詳細信息。如需建立 Lambda 函數的相關資訊,請參閱開AWS Lambda 發人員指南中的開始使用 Lambda。
範本程式碼
下列 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 資源名稱 (ARN)。您可以在下一節中指定建立 AWS Identity and Access Management (IAM) 假定角色ARN時。您可以在建立副檔名時指定ARN和名稱。
有效載荷參
本節包含使用自訂 AWS AppConfig 延伸模組的承載要求和回應參考詳細資料。
請求結構
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 擴充功能的要求所傳回的內容。
同步事件-成功回應
如果要轉換內容,請使用以下內容:
"Content": "SomeBase64EncodedByteArray"
如果您不想轉換內容,則不返回任何內容。
異步事件-成功響應
什麼都不返回。
所有錯誤事件
{
"Error": "BadRequestError",
"Message": "There was malformed stuff in here",
"Details": [{
"Type": "Malformed",
"Name": "S3 pointer",
"Reason": "S3 bucket did not exist"
}]
}