ステップ 1: カスタム AWS AppConfig 拡張機能の Lambda 関数を作成する - AWS AppConfig

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ステップ 1: カスタム AWS AppConfig 拡張機能の Lambda 関数を作成する

ほとんどのユースケースでは、カスタム拡張機能を作成するには、拡張機能で定義された計算と処理を実行する AWS Lambda 関数を作成する必要があります。このセクションには、カスタム AWS AppConfig 拡張機能の Lambda 関数サンプルコードが含まれています。このセクションには、ペイロードリクエストとレスポンスリファレンスの詳細も含まれています。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 拡張機能を操作するためのペイロードリクエストとレスポンスのリファレンスの詳細について説明します。

リクエスト構造

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' }
リクエスト構造

事前作成済みのホスト設定バージョン

{ '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' } }

デプロイ開始前

{ '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' }
非同期イベント

デプロイ開始時、デプロイステップ時、デプロイ中

{ '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' }
応答の構造

次の例は、カスタム AWS AppConfig 拡張機能からのリクエストに応答して Lambda 関数が返す内容を示しています。

PRE_* 同期イベント - 正常なレスポンス

内容を変換したい場合は、次の手順を実行します。

"Content": "SomeBase64EncodedByteArray"

AT_* 同期イベント - 成功レスポンス

デプロイの次のステップを制御する (デプロイを続行するかロールバックする) 場合は、レスポンスの Directiveおよび Description 属性。

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

Directive は、 CONTINUEまたは の 2 つの値をサポートします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" }] }