How to use the KMS function from aws-sdk

Find comprehensive JavaScript aws-sdk.KMS code examples handpicked from public code repositorys.

aws-sdk.KMS is a module in the AWS SDK for JavaScript that provides methods for interacting with the AWS Key Management Service (KMS).

19
20
21
22
23
24
25
26
27
// The encrypted message and the ID of the KMS key used for encryption
const encryptedMessage = encryptedData; // The encrypted message in base64 format
const kmsKeyId = '4b86b61e-8815-446a-bdea-b64a8a0f7676'; // The ID of the KMS key used for encryption

// Create an instance of the KMS client
const kms = new AWS.KMS();

// Decrypt the data key using KMS
const eventData = await decryptData(kms, kmsKeyId, encryptedMessage);
fork icon1
star icon0
watch icon0

316
317
318
319
320
321
322
323
324
325
lookup.defaultConfiguration(s3Url, function(err, masterConfigJSON) {
  if (err) return context.abort();
  Object.keys(masterConfigJSON).forEach(function(key) {
    if (context.oldParameters.hasOwnProperty(key)) {
      if (context.oldParameters[key].indexOf('secure') > -1) {
        var kms = new AWS.KMS({
          region: context.stackRegion,
          maxRetries: 10
        });
        var valueToDecrypt = context.oldParameters[key].replace(/^secure:/, '');
fork icon0
star icon0
watch icon1

+ 25 other calls in file

How does aws-sdk.KMS work?

aws-sdk.KMS provides a way to interact with the AWS Key Management Service (KMS) using JavaScript code. AWS KMS is a service that allows you to create and manage cryptographic keys used for encryption and decryption of your data, and provides features such as key rotation and auditing. With the aws-sdk.KMS module, you can perform operations such as creating and managing KMS keys, encrypting and decrypting data using those keys, and managing access to the keys. To use the aws-sdk.KMS module, you need to have an AWS account and AWS credentials, and you will need to create an instance of the AWS.KMS class provided by the module. Once you have an instance of this class, you can call its methods to perform KMS operations. For example, you can create a KMS key by calling the createKey method, encrypt data using the encrypt method, and decrypt data using the decrypt method. aws-sdk.KMS also provides support for asynchronous operations using Promises, as well as streaming operations for handling large amounts of data. Overall, aws-sdk.KMS provides a convenient way to manage cryptographic keys and perform encryption and decryption operations in your JavaScript code using the AWS Key Management Service.

16
17
18
19
20
21
22
23
24
25
26
27
28


const AWS = require('aws-sdk');
const crypto = require('crypto');
const qs = require('querystring');


var kmsClient = new AWS.KMS();


var docClient = new AWS.DynamoDB.DocumentClient();
var cognitoSP = new AWS.CognitoIdentityServiceProvider();
var codesTableName = process.env.STORAGE_AMPLIFYIDENTITYBROKERCODESTABLE_NAME;
fork icon0
star icon0
watch icon1

+ 18 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const AWS = require("aws-sdk");

// Create a new instance of the KMS client
const kms = new AWS.KMS({ region: "us-west-2" });

// Create a KMS key
kms.createKey({}, (err, data) => {
  if (err) console.error(err);
  else {
    const keyId = data.KeyMetadata.KeyId;
    console.log(`Created KMS key with ID ${keyId}`);

    // Encrypt some data using the KMS key
    const plaintext = "Hello, world!";
    const params = {
      KeyId: keyId,
      Plaintext: plaintext,
    };
    kms.encrypt(params, (err, data) => {
      if (err) console.error(err);
      else {
        const ciphertext = data.CiphertextBlob;
        console.log(`Encrypted data: ${ciphertext.toString("base64")}`);

        // Decrypt the data using the KMS key
        const params = {
          CiphertextBlob: ciphertext,
        };
        kms.decrypt(params, (err, data) => {
          if (err) console.error(err);
          else {
            const decryptedPlaintext = data.Plaintext.toString("utf8");
            console.log(`Decrypted data: ${decryptedPlaintext}`);
          }
        });
      }
    });
  }
});

In this example, we first create a new instance of the AWS.KMS class provided by the aws-sdk module. We specify the region option as 'us-west-2', which indicates the AWS region where we want to create the KMS key. We then call the createKey method of the kms object to create a new KMS key. This method takes an empty object as its first argument, and a callback function that will be called with either an error object or a data object containing information about the newly created KMS key. If the createKey method succeeds, we extract the ID of the new KMS key from the data object and log a message indicating that the key was created. We then use the KMS key to encrypt some data ('Hello, world!') by calling the encrypt method of the kms object. This method takes an object as its argument that specifies the KMS key ID and the plaintext data to be encrypted. If the encrypt method succeeds, we extract the ciphertext blob from the data object and log a message indicating the encrypted data. Finally, we use the KMS key to decrypt the encrypted data by calling the decrypt method of the kms object. This method takes an object as its argument that specifies the ciphertext blob to be decrypted. If the decrypt method succeeds, we extract the plaintext from the data object and log a message indicating the decrypted data.