title: Decrypt multiple env. variables nodejs - AWS Lambda
body: <p>I'm having difficulty decrypting multiple environment variables in nodejs for an AWS lambda. I've looked at the code sample supplied in the console and the following two related questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/44633822/nodejs-async-issue-while-decrypting-aws-kms-keys">Question 1</a>,
  <a href="https://stackoverflow.com/questions/44788860/decrypting-multiple-env-variables-in-aws-lambda">Question 2</a></p>
  
  <p>I have been able to successfully decrypt a single environment variable through their code sample, however, when I try to apply a cleaner approach through the use of promises (methods outlined in the questions above), I get this error when testing the lambda function in the console:</p>
  
  <blockquote>
    <p>TypeError: First argument must be a string, Buffer, ArrayBuffer,
    Array, or array-like object.</p>
  </blockquote>
  
  <p>I was wondering if anyone has had this issue before and how I could go about resolving it?</p>
  
  <p>Edit:
  I've added some samples from my code below</p>
  
  <pre><code>const AWS = require('aws-sdk');
  const mysql = require('mysql');
  let connection;
  
  const encrypted = {
      username: process.env.username,
      password: process.env.password,
      database: process.env.database,
      host: process.env.host
  };
  
  let decrypted = {};
  
  const encryptedEnvVars = [process.env.username, process.env.password, process.env.database, process.env.host ];
  
  
  exports.handler = (event, context, callback) =&gt; {
      if (isEnvVarsDecrypted()) {
          processEvent(event, context);
      } else {  
          Promise.all(encryptedEnvVars.map(decryptKMS))
          .then(decryptEnvVars)
          .catch(console.log);
      }
  };
  
  function decryptKMS(key) {
      return new Promise((resolve, reject) =&gt; {
        const kms = new AWS.KMS()
  
        kms.decrypt({ CiphertextBlob: new Buffer(key, 'base64') }, (err, data) =&gt; {
          if(err) { reject(err); }
          else { resolve(data.Plaintext.toString('ascii')); }
        });
      });
    }
  
  var decryptEnvVars = data =&gt;  {
      return new Promise((resolve, reject) =&gt; {
          console.log(data);
          decrypted.username = data[0].Plaintext.toString('ascii');
          decrypted.password = data[1].Plaintext.toString('ascii');
          decrypted.database = data[2].Plaintext.toString('ascii');
          decrypted.host = data[3].Plaintext.toString('ascii');
          resolve();
      });
  };
  
  var isEnvVarsDecrypted = () =&gt; {
      return decrypted.username &amp;&amp; decrypted.password &amp;&amp; decrypted.database &amp;&amp; decrypted.host;
  }
  </code></pre>