Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Langkah 1: Buat fungsi Lambda untuk kustom AWS AppConfig luas
Untuk sebagian besar kasus penggunaan, untuk membuat ekstensi khusus, Anda harus membuat AWS Lambda fungsi untuk melakukan perhitungan dan pemrosesan apa pun yang ditentukan dalam ekstensi. Bagian ini mencakup kode sampel fungsi Lambda untuk AWS AppConfig ekstensi khusus. Bagian ini juga mencakup permintaan muatan dan detail referensi respons. Untuk informasi tentang membuat fungsi Lambda, lihat Mem ulai dengan Lambda di Panduan Peng AWS Lambda embang.
Kode sampel
Kode contoh berikut untuk fungsi Lambda, saat dipanggil, secara otomatis mencadangkan AWS AppConfig konfigurasi ke bucket Amazon S3. Konfigurasi dicadangkan setiap kali konfigurasi baru dibuat atau diterapkan. Sampel menggunakan parameter ekstensi sehingga nama bucket tidak harus di-hardcode dalam fungsi Lambda. Dengan menggunakan parameter ekstensi, pengguna dapat melampirkan ekstensi ke beberapa aplikasi dan mencadangkan konfigurasi ke bucket yang berbeda. Sampel kode termasuk komentar untuk menjelaskan fungsinya lebih lanjut.
Contoh fungsi Lambda untuk ekstensi AWS AppConfig
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)
Jika Anda ingin menggunakan sampel ini selama panduan ini, simpan dengan nama MyS3ConfigurationBackUpExtension dan salin Nama Sumber Daya Amazon (ARN) untuk fungsi tersebut. Anda menentukan ARN saat Anda membuat peran asumsi AWS Identity and Access Management (IAM) di bagian berikutnya. Anda menentukan ARN dan nama saat Anda membuat ekstensi.
Referensi muatan
Bagian ini mencakup permintaan muatan dan detail referensi respons untuk bekerja dengan AWS AppConfig ekstensi khusus.
Struktur permintaan
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' }
Struktur permintaan
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' }
Peristiwa asinkron
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' }
Struktur respons
Contoh berikut menunjukkan apa yang dikembalikan fungsi Lambda Anda sebagai respons terhadap permintaan dari AWS AppConfig ekstensi khusus.
PRE_* Peristiwa sinkron - respons berhasil
Jika Anda ingin mengubah konten, gunakan yang berikut ini:
"Content": "SomeBase64EncodedByteArray"
AT_* Peristiwa sinkron - respons berhasil
Jika Anda ingin mengontrol langkah penerapan berikutnya (melanjutkan penerapan atau memutarnya kembali), set Directive dan Description atribut dalam respons.
"Directive": "ROLL_BACK" "Description" "Deployment event log description"
Directivemendukung dua nilai: CONTINUE atauROLL_BACK. Gunakan enum ini dalam respons muatan Anda untuk mengontrol langkah penerapan berikutnya.
Peristiwa sinkron - respons yang berhasil
Jika Anda ingin mengubah konten, gunakan yang berikut ini:
"Content": "SomeBase64EncodedByteArray"
Jika Anda tidak ingin mengubah konten, tidak mengembalikan apa pun.
Peristiwa asinkron - respons yang berhasil
Tidak mengembalikan apa-apa.
Semua peristiwa kesalahan
{ "Error": "BadRequestError", "Message": "There was malformed stuff in here", "Details": [{ "Type": "Malformed", "Name": "S3 pointer", "Reason": "S3 bucket did not exist" }] }