SDK for SAP ABAP を使用した Lambda の例 - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

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

SDK for SAP ABAP を使用した Lambda の例

次のコード例は、Lambda で AWS SDK for SAP ABAP を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。このリンクには GitHub、コンテキスト内でコードをセットアップして実行する方法の手順が記載されています。

アクション

次のコード例は、Lambda 関数を作成する方法を示しています。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、CreateFunctionAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

次のコード例は、Lambda 関数を削除する方法を示しています。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、DeleteFunctionAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

次のコード例では、Lambda 関数を取得する方法を示します。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、GetFunctionAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

次のコード例は、Lambda 関数を呼び出す方法を示しています。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、「AWS SDK for SAP ABAP API リファレンス」の「Invoke」を参照してください。

以下のコード例では、Lambda 関数を一覧表示する方法を示します。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、ListFunctionsAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

次のコード例では、Lambda 関数を更新する方法を示します。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、UpdateFunctionCodeAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

次のコード例では、Lambda 関数の設定を更新する方法を示します。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.
  • API の詳細については、UpdateFunctionConfigurationAWS「 SDK for SAP ABAP API リファレンス」の「」を参照してください。

シナリオ

次のコードサンプルは、以下の操作方法を示しています。

  • IAM ロールと Lambda 関数を作成し、ハンドラーコードをアップロードします。

  • 1 つのパラメーターで関数を呼び出して、結果を取得します。

  • 関数コードを更新し、環境変数で設定します。

  • 新しいパラメーターで関数を呼び出して、結果を取得します。返された実行ログを表示します。

  • アカウントの関数を一覧表示し、リソースをクリーンアップします。

詳細については、「コンソールで Lambda 関数を作成する」を参照してください。

SDK for SAP ABAP
注記

には他にもがあります GitHub。用例一覧を検索し、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.