title: Connect to MySQL database from Lambda function (Node)
body: <p>I have been unable to connect to MySQL database using Node from Lambda function. The error I receive is <code>Task timed out after 4.00 seconds</code>.</p>
  
  <p><strong>Does anyone have any solutions?</strong></p>
  
  <p>Here is an overview of my state:</p>
  
  <ol>
  <li>The AWS RDS database is a MySQL database. It is not confined to the VPC (I am able to connect using host/user/password from MySQLWorkbench).</li>
  <li>The execution role of my Lambda function is set to have Lambda as a trusted entity and given AdministratorAccess.</li>
  <li>On my local machine, I installed the mysql module, zipped my index.js and node_modules folder, and uploaded to my Lambda function.</li>
  <li>I have tried putting the createConnection and connect function inside the handler. I have tried putting my query inside the callback function of the connection function. I have tried increasing the timeout time to 10 seconds.</li>
  <li><p>My code:</p>
  
  <pre><code>var mysql = require('mysql');
  
  var connection = mysql.createConnection({
      host     : 'amazon-string.rds.amazonaws.com',
      user     : 'myusername',
      password : 'mypassword'
  });
  
  connection.connect();
  
  exports.handler = (event, context, callback) =&gt; {
  
      connection.query("SELECT * FROM table", function(err, rows, fields) {
          console.log("rows: " + rows);
          callback(null);
      });
  
  };
  </code></pre></li>
  </ol>