Contoh Lambda menggunakan for SDK SAP ABAP - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh Lambda menggunakan for SDK SAP ABAP

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for SAP ABAP with Lambda.

Dasar-dasar adalah contoh kode yang menunjukkan kepada Anda bagaimana melakukan operasi penting dalam suatu layanan.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Hal-hal mendasar

Contoh kode berikut ini menunjukkan cara:

  • Buat IAM peran dan fungsi Lambda, lalu unggah kode handler.

  • Panggil fungsi dengan satu parameter dan dapatkan hasil.

  • Perbarui kode fungsi dan konfigurasikan dengan variabel lingkungan.

  • Panggil fungsi dengan parameter baru dan dapatkan hasil. Tampilkan log eksekusi yang dikembalikan.

  • Buat daftar fungsi untuk akun Anda, lalu bersihkan sumber daya.

Untuk informasi selengkapnya, lihat Membuat fungsi Lambda dengan konsol.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. "Create an AWS Identity and Access Management (IAM) role that grants AWS Lambda permission to write to logs." DATA(lv_policy_document) = `{` && `"Version":"2012-10-17",` && `"Statement": [` && `{` && `"Effect": "Allow",` && `"Action": [` && `"sts:AssumeRole"` && `],` && `"Principal": {` && `"Service": [` && `"lambda.amazonaws.com"` && `]` && `}` && `}` && `]` && `}`. TRY. DATA(lo_create_role_output) = lo_iam->createrole( iv_rolename = iv_role_name iv_assumerolepolicydocument = lv_policy_document iv_description = 'Grant lambda permission to write to logs' ). MESSAGE 'IAM role created.' TYPE 'I'. WAIT UP TO 10 SECONDS. " Make sure that the IAM role is ready for use. " CATCH /aws1/cx_iamentityalrdyexex. MESSAGE 'IAM role already exists.' TYPE 'E'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iammalformedplydocex. MESSAGE 'Policy document in the request is malformed.' TYPE 'E'. ENDTRY. TRY. lo_iam->attachrolepolicy( iv_rolename = iv_role_name iv_policyarn = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ). MESSAGE 'Attached policy to the IAM role.' TYPE 'I'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamplynotattachableex. MESSAGE 'Service role policies can only be attached to the service-linked role for their service.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. " Create a Lambda function and upload handler code. " " Lambda function performs 'increment' action on a number. " TRY. lo_lmd->createfunction( iv_functionname = iv_function_name iv_runtime = `python3.9` iv_role = lo_create_role_output->get_role( )->get_arn( ) iv_handler = iv_handler io_code = io_initial_zip_file iv_description = 'AWS Lambda code example' ). MESSAGE 'Lambda function created.' TYPE 'I'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. " Verify the function is in Active state " WHILE lo_lmd->getfunction( iv_functionname = iv_function_name )->get_configuration( )->ask_state( ) <> 'Active'. IF sy-index = 10. EXIT. " Maximum 10 seconds. " ENDIF. WAIT UP TO 1 SECONDS. ENDWHILE. "Invoke the function with a single parameter and get results." TRY. DATA(lv_json) = /aws1/cl_rt_util=>string_to_xstring( `{` && `"action": "increment",` && `"number": 10` && `}` ). DATA(lo_initial_invoke_output) = lo_lmd->invoke( iv_functionname = iv_function_name iv_payload = lv_json ). ov_initial_invoke_payload = lo_initial_invoke_output->get_payload( ). " ov_initial_invoke_payload is returned for testing purposes. " DATA(lo_writer_json) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). CALL TRANSFORMATION id SOURCE XML ov_initial_invoke_payload RESULT XML lo_writer_json. DATA(lv_result) = cl_abap_codepage=>convert_from( lo_writer_json->get_output( ) ). MESSAGE 'Lambda function invoked.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdinvrequestcontex. MESSAGE 'Unable to parse request body as JSON.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdunsuppedmediatyp00. MESSAGE 'Invoke request body does not have JSON as its content type.' TYPE 'E'. ENDTRY. " Update the function code and configure its Lambda environment with an environment variable. " " Lambda function is updated to perform 'decrement' action also. " TRY. lo_lmd->updatefunctioncode( iv_functionname = iv_function_name iv_zipfile = io_updated_zip_file ). WAIT UP TO 10 SECONDS. " Make sure that the update is completed. " MESSAGE 'Lambda function code updated.' TYPE 'I'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. TRY. DATA lt_variables TYPE /aws1/cl_lmdenvironmentvaria00=>tt_environmentvariables. DATA ls_variable LIKE LINE OF lt_variables. ls_variable-key = 'LOG_LEVEL'. ls_variable-value = NEW /aws1/cl_lmdenvironmentvaria00( iv_value = 'info' ). INSERT ls_variable INTO TABLE lt_variables. lo_lmd->updatefunctionconfiguration( iv_functionname = iv_function_name io_environment = NEW /aws1/cl_lmdenvironment( it_variables = lt_variables ) ). WAIT UP TO 10 SECONDS. " Make sure that the update is completed. " MESSAGE 'Lambda function configuration/settings updated.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. "Invoke the function with new parameters and get results. Display the execution log that's returned from the invocation." TRY. lv_json = /aws1/cl_rt_util=>string_to_xstring( `{` && `"action": "decrement",` && `"number": 10` && `}` ). DATA(lo_updated_invoke_output) = lo_lmd->invoke( iv_functionname = iv_function_name iv_payload = lv_json ). ov_updated_invoke_payload = lo_updated_invoke_output->get_payload( ). " ov_updated_invoke_payload is returned for testing purposes. " lo_writer_json = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). CALL TRANSFORMATION id SOURCE XML ov_updated_invoke_payload RESULT XML lo_writer_json. lv_result = cl_abap_codepage=>convert_from( lo_writer_json->get_output( ) ). MESSAGE 'Lambda function invoked.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdinvrequestcontex. MESSAGE 'Unable to parse request body as JSON.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdunsuppedmediatyp00. MESSAGE 'Invoke request body does not have JSON as its content type.' TYPE 'E'. ENDTRY. " List the functions for your account. " TRY. DATA(lo_list_output) = lo_lmd->listfunctions( ). DATA(lt_functions) = lo_list_output->get_functions( ). MESSAGE 'Retrieved list of Lambda functions.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. ENDTRY. " Delete the Lambda function. " TRY. lo_lmd->deletefunction( iv_functionname = iv_function_name ). MESSAGE 'Lambda function deleted.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. " Detach role policy. " TRY. lo_iam->detachrolepolicy( iv_rolename = iv_role_name iv_policyarn = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ). MESSAGE 'Detached policy from the IAM role.' TYPE 'I'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamplynotattachableex. MESSAGE 'Service role policies can only be attached to the service-linked role for their service.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. " Delete the IAM role. " TRY. lo_iam->deleterole( iv_rolename = iv_role_name ). MESSAGE 'IAM role deleted.' TYPE 'I'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. CATCH /aws1/cx_rt_service_generic INTO lo_exception. DATA(lv_error) = lo_exception->get_longtext( ). MESSAGE lv_error TYPE 'E'. ENDTRY.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanCreateFunction.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. lo_lmd->createfunction( iv_functionname = iv_function_name iv_runtime = `python3.9` iv_role = iv_role_arn iv_handler = iv_handler io_code = io_zip_file iv_description = 'AWS Lambda code example' ). MESSAGE 'Lambda function created.' TYPE 'I'. CATCH /aws1/cx_lmdcodesigningcfgno00. MESSAGE 'Code signing configuration does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdcodeverification00. MESSAGE 'Code signature failed one or more validation checks for signature mismatch or expiration.' TYPE 'E'. CATCH /aws1/cx_lmdinvalidcodesigex. MESSAGE 'Code signature failed the integrity check.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.

Contoh kode berikut menunjukkan cara menggunakanDeleteFunction.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. lo_lmd->deletefunction( iv_functionname = iv_function_name ). MESSAGE 'Lambda function deleted.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.

Contoh kode berikut menunjukkan cara menggunakanGetFunction.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. oo_result = lo_lmd->getfunction( iv_functionname = iv_function_name ). " oo_result is returned for testing purposes. " MESSAGE 'Lambda function information retrieved.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.
  • Untuk API detailnya, lihat GetFunction AWSSDKuntuk SAP ABAP API referensi.

Contoh kode berikut menunjukkan cara menggunakanInvoke.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. DATA(lv_json) = /aws1/cl_rt_util=>string_to_xstring( `{` && `"action": "increment",` && `"number": 10` && `}` ). oo_result = lo_lmd->invoke( " oo_result is returned for testing purposes. " iv_functionname = iv_function_name iv_payload = lv_json ). MESSAGE 'Lambda function invoked.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdinvrequestcontex. MESSAGE 'Unable to parse request body as JSON.' TYPE 'E'. CATCH /aws1/cx_lmdinvalidzipfileex. MESSAGE 'The deployment package could not be unzipped.' TYPE 'E'. CATCH /aws1/cx_lmdrequesttoolargeex. MESSAGE 'Invoke request body JSON input limit was exceeded by the request payload.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. CATCH /aws1/cx_lmdunsuppedmediatyp00. MESSAGE 'Invoke request body does not have JSON as its content type.' TYPE 'E'. ENDTRY.
  • Untuk API detailnya, lihat Memanggil AWS SDKuntuk SAP ABAP API referensi.

Contoh kode berikut menunjukkan cara menggunakanListFunctions.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. oo_result = lo_lmd->listfunctions( ). " oo_result is returned for testing purposes. " DATA(lt_functions) = oo_result->get_functions( ). MESSAGE 'Retrieved list of Lambda functions.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.

Contoh kode berikut menunjukkan cara menggunakanUpdateFunctionCode.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. oo_result = lo_lmd->updatefunctioncode( " oo_result is returned for testing purposes. " iv_functionname = iv_function_name iv_zipfile = io_zip_file ). MESSAGE 'Lambda function code updated.' TYPE 'I'. CATCH /aws1/cx_lmdcodesigningcfgno00. MESSAGE 'Code signing configuration does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdcodeverification00. MESSAGE 'Code signature failed one or more validation checks for signature mismatch or expiration.' TYPE 'E'. CATCH /aws1/cx_lmdinvalidcodesigex. MESSAGE 'Code signature failed the integrity check.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.

Contoh kode berikut menunjukkan cara menggunakanUpdateFunctionConfiguration.

SDKuntuk SAP ABAP
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

TRY. oo_result = lo_lmd->updatefunctionconfiguration( " oo_result is returned for testing purposes. " iv_functionname = iv_function_name iv_runtime = iv_runtime iv_description = 'Updated Lambda function' iv_memorysize = iv_memory_size ). MESSAGE 'Lambda function configuration/settings updated.' TYPE 'I'. CATCH /aws1/cx_lmdcodesigningcfgno00. MESSAGE 'Code signing configuration does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdcodeverification00. MESSAGE 'Code signature failed one or more validation checks for signature mismatch or expiration.' TYPE 'E'. CATCH /aws1/cx_lmdinvalidcodesigex. MESSAGE 'Code signature failed the integrity check.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.