title: How to decrypt AWS Lambda Environment Variables
body: <p>I'm using the example code provided on the Lambda console when I 'encrypt' the environment variables.</p>
  
  <p>I've created a key, and given the Role "kms:Decrypt" permission.</p>
  
  <p>My function times out when trying to "decrypt" the variable, but runs fine when not encrypted. The logs do not provide any errors.</p>
  
  <p>Here is the code used to decrypt:</p>
  
  <pre><code>private String decryptKey(String keyName) {
      byte[] encryptedKey = Base64.decode(keyName);
      AWSKMS client = AWSKMSClientBuilder.defaultClient();
      DecryptRequest request = new DecryptRequest()
       .withCiphertextBlob(ByteBuffer.wrap(encryptedKey));
      ByteBuffer plainTextKey = client.decrypt(request).getPlaintext();
      return new String(plainTextKey.array(), Charset.forName("UTF-8"));
  }
  </code></pre>
  
  <p>And it's called like this...</p>
  
  <pre><code>return decryptKey(System.getenv(variableName));
  </code></pre>
  
  <p>I took this code as is, assuming that, as it runs from within Lambda, the 'defaultClient' knows the region, account, etc. </p>
  
  <p>Edit:</p>
  
  <p>These are the log lines:</p>
  
  <blockquote>
    <p>START RequestId: 92419f62-fa84-11e6-876d-99aa85e9b481 Version: $LATEST
    END RequestId: 92419f62-fa84-11e6-876d-99aa85e9b481
    REPORT RequestId: 92419f62-fa84-11e6-876d-99aa85e9b481    Duration: 15001.41 ms > Billed Duration: 15000 ms   Memory Size: 512 MB Max Memory Used: 64 MB<br>
    2017-02-24T11:30:13.908Z 92419f62-fa84-11e6-876d-99aa85e9b481 Task timed out 
    after 15.00 seconds</p>
  </blockquote>
  
  <p>If I run without EncryptionHelpers, but still try and unencrypt the variables I get this, which is as expected:</p>
  
  <blockquote>
    <p>{
     "errorMessage": "Input is expected to be encoded in multiple of 4 bytes but found: 13",
     "errorType": "java.lang.IllegalArgumentException",
     "stackTrace": [
       "com.amazonaws.util.Base64Codec.decode(Base64Codec.java:198)",
       "com.amazonaws.util.Base64.decode(Base64.java:89)",
       "scripts.Environment.decryptKey(Environment.java:56)",
       "scripts.Environment.getEnvVariable(Environment.java:38)",
       "scripts.Environment.(Environment.java:30)",
       "scripts.CreateNewDatabase.createNewConfigDatabase(CreateNewDatabase.java:33)",
       "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
       "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
       "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
       "java.lang.reflect.Method.invoke(Method.java:498)"
     ]
    }</p>
  </blockquote>