-1

I am trying to generate a random key in a lambda using kms.generateRandom. I tried the sample mentioned in the document but it does not return anything. No error no Data. Has anybody used it? Do I have to grant any access to my lambda? Below is the code I'm trying

/* The following example uses AWS KMS to generate 32 bytes of random data. */

 var params = {
  NumberOfBytes: 32// The length of the random data, specified in number of bytes.
 };
 kms.generateRandom(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    Plaintext: <Binary String>// The random data.
   }
   */
 });
MadhaviP
  • 1
  • 2
  • There's nothing in the code you posted that would "return" anything. It should be printing something to the logs though. What is showing up in the Lambda function's logs? – Mark B Oct 25 '19 at 11:07
  • I meant console does not log anything. Nothing gets logged in cloudWatchLog. – MadhaviP Oct 25 '19 at 11:25
  • if using callbacks, you need to specify the callback on completion. It makes sense that no console is being logged because your lambda function finishes while `generateRandom` is being executed – LostJon Oct 25 '19 at 12:31

1 Answers1

0

Improved the code as below and that helped me find out the problem with access rights.

let result = await new Promise((resolve, reject) => {
        kms.generateRandom(params, function(err, data) {
            if (err)
            {
                console.log('Error occurred: ' + err, err.stack);
                reject();
            } // an error occurred
            else
            {
                console.log('Data: ' + data);
                resolve(data);
            } // successful response
        });
    });

I have updated IAM policy to have something like this:

"Action": [
    "kms:*"
  ],
"Resource": "*"

and it returns me data as below: Data: {"Plaintext":{"type":"Buffer","data":[238,80,205,34,52,91,37,158,167,126,109,222,246,0,59,132,116,169,49,35,244,19,146,86,104,72,190,227,89,196,156,201]}}

Code to convert the buffer to string is below:

let test = new Buffer.from(data.Plaintext); console.log('buff:' + test.toString('base64'));

MadhaviP
  • 1
  • 2