使用 for 的 Lambda 示例 SDK SAP ABAP - Amazon SDK对于 SAP ABAP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 for 的 Lambda 示例 SDK SAP ABAP

以下代码示例向您展示了如何使用SAPABAP带有 Lambda 的 for 来执行操作和实现常见场景。 Amazon SDK

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

场景是向您展示如何通过在一个服务中调用多个函数或与其他 Amazon Web Services 服务结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

操作

以下代码示例显示了如何使用CreateFunction

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅CreateFunction中的Amazon SDK以供SAPABAPAPI参考

以下代码示例显示了如何使用DeleteFunction

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅DeleteFunction中的Amazon SDK以供SAPABAPAPI参考

以下代码示例显示了如何使用GetFunction

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅GetFunction中的Amazon SDK以供SAPABAPAPI参考

以下代码示例显示了如何使用Invoke

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅调用AmazonSDK以供SAPABAPAPI参考

以下代码示例显示了如何使用ListFunctions

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅ListFunctions中的Amazon SDK以供SAPABAPAPI参考

以下代码示例显示了如何使用UpdateFunctionCode

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.
  • 有关API详细信息,请参阅UpdateFunctionCode中的Amazon SDK以供SAPABAPAPI参考

以下代码示例显示了如何使用UpdateFunctionConfiguration

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.

场景

以下代码示例展示了如何:

  • 创建IAM角色和 Lambda 函数,然后上传处理程序代码。

  • 使用单个参数来调用函数并获取结果。

  • 更新函数代码并使用环境变量进行配置。

  • 使用新参数来调用函数并获取结果。显示返回的执行日志。

  • 列出账户函数,然后清除函数。

有关更多信息,请参阅使用控制台创建 Lambda 函数

SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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.