AWS KMS examples using SDK for SAP ABAP - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

AWS KMS examples using SDK for SAP ABAP

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for SAP ABAP with AWS KMS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Get started

The following code example shows how to get started using AWS Key Management Service.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_kms->listkeys( ). MESSAGE 'Retrieved KMS keys list.' TYPE 'I'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ListKeys in AWS SDK for SAP ABAP API reference.

Actions

The following code example shows how to use CreateAlias.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_alias_name = 'alias/my-key-alias' " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' lo_kms->createalias( iv_aliasname = iv_alias_name iv_targetkeyid = iv_key_id ). MESSAGE 'Alias created successfully.' TYPE 'I'. CATCH /aws1/cx_kmsalreadyexistsex. MESSAGE 'Alias already exists.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmsinvalidaliasnameex. MESSAGE 'Invalid alias name.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see CreateAlias in AWS SDK for SAP ABAP API reference.

The following code example shows how to use CreateGrant.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_grantee_principal = 'arn:aws:iam::123456789012:role/my-role' " it_operations contains 'Encrypt', 'Decrypt', 'GenerateDataKey' oo_result = lo_kms->creategrant( iv_keyid = iv_key_id iv_granteeprincipal = iv_grantee_principal it_operations = it_operations ). MESSAGE 'Grant created successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see CreateGrant in AWS SDK for SAP ABAP API reference.

The following code example shows how to use CreateKey.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_description = 'Created by the AWS SDK for SAP ABAP' oo_result = lo_kms->createkey( iv_description = iv_description ). MESSAGE 'KMS key created successfully.' TYPE 'I'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. CATCH /aws1/cx_kmslimitexceededex. MESSAGE 'Limit exceeded for KMS resources.' TYPE 'E'. ENDTRY.
  • For API details, see CreateKey in AWS SDK for SAP ABAP API reference.

The following code example shows how to use Decrypt.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_ciphertext_blob contains the encrypted data oo_result = lo_kms->decrypt( iv_keyid = iv_key_id iv_ciphertextblob = iv_ciphertext_blob ). MESSAGE 'Text decrypted successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsincorrectkeyex. MESSAGE 'Incorrect key for decryption.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see Decrypt in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DeleteAlias.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_alias_name = 'alias/my-key-alias' lo_kms->deletealias( iv_aliasname = iv_alias_name ). MESSAGE 'Alias deleted successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Alias not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see DeleteAlias in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DescribeKey.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' oo_result = lo_kms->describekey( iv_keyid = iv_key_id ). DATA(lo_key) = oo_result->get_keymetadata( ). MESSAGE 'Retrieved key information successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see DescribeKey in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DisableKey.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' lo_kms->disablekey( iv_keyid = iv_key_id ). MESSAGE 'KMS key disabled successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see DisableKey in AWS SDK for SAP ABAP API reference.

The following code example shows how to use EnableKey.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' lo_kms->enablekey( iv_keyid = iv_key_id ). MESSAGE 'KMS key enabled successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see EnableKey in AWS SDK for SAP ABAP API reference.

The following code example shows how to use EnableKeyRotation.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' lo_kms->enablekeyrotation( iv_keyid = iv_key_id ). MESSAGE 'Key rotation enabled successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmsunsupportedopex. MESSAGE 'Operation not supported for this key.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.

The following code example shows how to use Encrypt.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_plaintext contains the data to encrypt oo_result = lo_kms->encrypt( iv_keyid = iv_key_id iv_plaintext = iv_plaintext ). MESSAGE 'Text encrypted successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see Encrypt in AWS SDK for SAP ABAP API reference.

The following code example shows how to use GenerateDataKey.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_keyspec = 'AES_256' oo_result = lo_kms->generatedatakey( iv_keyid = iv_key_id iv_keyspec = 'AES_256' ). MESSAGE 'Data key generated successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see GenerateDataKey in AWS SDK for SAP ABAP API reference.

The following code example shows how to use GetKeyPolicy.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' oo_result = lo_kms->getkeypolicy( iv_keyid = iv_key_id iv_policyname = 'default' ). MESSAGE 'Retrieved key policy successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see GetKeyPolicy in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListAliases.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_kms->listaliases( ). MESSAGE 'Retrieved KMS aliases list.' TYPE 'I'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ListAliases in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListGrants.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' oo_result = lo_kms->listgrants( iv_keyid = iv_key_id ). MESSAGE 'Retrieved grants list.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ListGrants in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListKeyPolicies.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' oo_result = lo_kms->listkeypolicies( iv_keyid = iv_key_id ). MESSAGE 'Retrieved key policies list.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ListKeyPolicies in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListKeys.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_kms->listkeys( ). MESSAGE 'Retrieved KMS keys list.' TYPE 'I'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ListKeys in AWS SDK for SAP ABAP API reference.

The following code example shows how to use PutKeyPolicy.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_policy = '{"Version":"2012-10-17", "Statement": [...]}' lo_kms->putkeypolicy( iv_keyid = iv_key_id iv_policyname = 'default' iv_policy = iv_policy ). MESSAGE 'Key policy updated successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmsmalformedplydocex. MESSAGE 'Malformed policy document.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see PutKeyPolicy in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ReEncrypt.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_source_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_destination_key_id = 'arn:aws:kms:us-east-1:123456789012:key/5678dcba-56cd-78ef-90ab-5678901234cd' " iv_ciphertext_blob contains the encrypted data oo_result = lo_kms->reencrypt( iv_sourcekeyid = iv_source_key_id iv_destinationkeyid = iv_destination_key_id iv_ciphertextblob = iv_ciphertext_blob ). MESSAGE 'Ciphertext reencrypted successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsincorrectkeyex. MESSAGE 'Incorrect source key for decryption.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see ReEncrypt in AWS SDK for SAP ABAP API reference.

The following code example shows how to use RetireGrant.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_grant_token = 'AQpAM2RhZ...' lo_kms->retiregrant( iv_granttoken = iv_grant_token ). MESSAGE 'Grant retired successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Grant not found.' TYPE 'E'. CATCH /aws1/cx_kmsinvgranttokenex. MESSAGE 'Invalid grant token.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see RetireGrant in AWS SDK for SAP ABAP API reference.

The following code example shows how to use RevokeGrant.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_grant_id = '1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p' lo_kms->revokegrant( iv_keyid = iv_key_id iv_grantid = iv_grant_id ). MESSAGE 'Grant revoked successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Grant or key not found.' TYPE 'E'. CATCH /aws1/cx_kmsinvalidgrantidex. MESSAGE 'Invalid grant ID.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see RevokeGrant in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ScheduleKeyDeletion.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_pending_window_days = 7 oo_result = lo_kms->schedulekeydeletion( iv_keyid = iv_key_id iv_pendingwindowindays = iv_pending_window_days ). MESSAGE 'Key scheduled for deletion.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.

The following code example shows how to use Sign.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' (asymmetric key) " iv_message contains the message to sign " iv_signing_algorithm = 'RSASSA_PSS_SHA_256' oo_result = lo_kms->sign( iv_keyid = iv_key_id iv_message = iv_message iv_signingalgorithm = iv_signing_algorithm ). MESSAGE 'Message signed successfully.' TYPE 'I'. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmsinvalidkeyusageex. MESSAGE 'Key cannot be used for signing.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see Sign in AWS SDK for SAP ABAP API reference.

The following code example shows how to use TagResource.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

DATA lt_tags TYPE /aws1/cl_kmstag=>tt_taglist. TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_tag_key = 'Environment' " iv_tag_value = 'Production' APPEND NEW /aws1/cl_kmstag( iv_tagkey = iv_tag_key iv_tagvalue = iv_tag_value ) TO lt_tags. lo_kms->tagresource( iv_keyid = iv_key_id it_tags = lt_tags ). MESSAGE 'Tag added to KMS key successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmstagexception. MESSAGE 'Invalid tag format.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see TagResource in AWS SDK for SAP ABAP API reference.

The following code example shows how to use UpdateAlias.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_alias_name = 'alias/my-key-alias' " iv_target_key_id = 'arn:aws:kms:us-east-1:123456789012:key/5678dcba-56cd-78ef-90ab-5678901234cd' lo_kms->updatealias( iv_aliasname = iv_alias_name iv_targetkeyid = iv_target_key_id ). MESSAGE 'Alias updated successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Alias or key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see UpdateAlias in AWS SDK for SAP ABAP API reference.

The following code example shows how to use Verify.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' (asymmetric key) " iv_message contains the original message " iv_signature contains the signature to verify " iv_signing_algorithm = 'RSASSA_PSS_SHA_256' oo_result = lo_kms->verify( iv_keyid = iv_key_id iv_message = iv_message iv_signature = iv_signature iv_signingalgorithm = iv_signing_algorithm ). DATA(lv_valid) = oo_result->get_signaturevalid( ). IF lv_valid = abap_true. MESSAGE 'Signature is valid.' TYPE 'I'. ELSE. MESSAGE 'Signature is invalid.' TYPE 'I'. ENDIF. CATCH /aws1/cx_kmsdisabledexception. MESSAGE 'The key is disabled.' TYPE 'E'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinvalidsigex. MESSAGE 'Invalid signature.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • For API details, see Verify in AWS SDK for SAP ABAP API reference.