Exemplos de Lambda usando SDK para SAP ABAP - Exemplos de código do SDK da AWS

Há mais exemplos de AWS SDK disponíveis no repositório AWSDoc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Exemplos de Lambda usando SDK para SAP ABAP

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK para SAP ABAP com o Lambda.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço específicas, é possível ver as ações contextualizadas em seus devidos cenários e exemplos entre serviços.

Cenários são exemplos de código que mostram como realizar uma tarefa específica chamando várias funções dentro do mesmo serviço.

Cada exemplo inclui um link para GitHub, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Ações

O exemplo de código a seguir mostra como criar uma função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a CreateFunctionreferência da API AWS SDK for SAP ABAP.

O exemplo de código a seguir mostra como excluir uma função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a DeleteFunctionreferência da API AWS SDK for SAP ABAP.

O exemplo de código a seguir mostra como obter uma função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a GetFunctionreferência da API AWS SDK for SAP ABAP.

O exemplo de código a seguir mostra como invocar uma função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter os detalhes da API, consulte Invoke na Referência da API do AWS SDK para SAP ABAP.

O exemplo de código a seguir mostra como listar funções do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a ListFunctionsreferência da API AWS SDK for SAP ABAP.

O exemplo de código a seguir mostra como atualizar um código de função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a UpdateFunctionCodereferência da API AWS SDK for SAP ABAP.

O exemplo de código a seguir mostra como atualizar uma configuração de função do Lambda.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.

Cenários

O código de exemplo a seguir mostra como:

  • Crie um perfil do IAM e uma função do Lambda e carregue o código de manipulador.

  • Invoque essa função com um único parâmetro e receba resultados.

  • Atualize o código de função e configure usando uma variável de ambiente.

  • Invoque a função com novos parâmetros e receba resultados. Exiba o log de execução retornado.

  • Liste as funções para sua conta e limpe os recursos.

Para obter mais informações, consulte Criar uma função do Lambda no console.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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.