Whenever querying data from MySQL database it gives an error aws lambda function timed out

0 votes

In my node.js file the code is as follows:

const dbConfig = require('./config/dbConfig')
const mysql = require('mysql')

var con = mysql.createConnection({
  host: dbConfig.host,
  user: dbConfig.username,
  password: dbConfig.password,
  database: dbConfig.database
})

function readMessages (event, context, callback) {
  console.log('function triggered')
  con.connect((err) => {
    if (err) {
      console.error(err)
      callback(err)
    } else {
      console.log('Connected!')
      con.query('SELECT * FROM Messages WHERE isDeleted = 0;', (err, result, fields) => {
        if (err) {
          console.error(err)
          callback(err)
        } else {
          console.log(result)
          con.end()
          callback(null, result)
        }
      })
    }
  })
}

exports.handler = readMessages

This code positively retrieves data from the MySQL database and displays it on the screen whenever I am running it on my local machine.

However, I got Task timed out after 7.01 seconds error when it is run on aws-lambda.
But, whenever I try to run it on AWS-Lambda, it states an error that the Task got timed out after 7.01

The code and its dependencies are packaged in a file named app.zip, then were uploaded into aws-lambda.

app.zip
├── config
│   └── dbConfig.js
├── index.js
└── node_modules

The only log message printed by the function I have created is function triggered, I cant really find other log messages being generated by my function in the cloud watch log.

Why does the function timed out on aws-lambda?

Jun 6, 2018 in AWS by Cloud gunner
• 4,670 points
2,196 views

1 answer to this question.

0 votes
I am damn sure that it is a permissions issue, whenever you run it on local it is of course going to grab credentials from your local machine/environment & whenever you run this in lambda, you need to assign a role to the lambda that has the permissions it needs to access the mysql database.

Also, don't forget to make MySQL Database accessible to Lambda which you're not trying to access a mysql database that is local to your machine from the lambda function.
answered Jun 6, 2018 by Flying geek
• 3,280 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer

AWS Cloudtrail API for Go SDK showing an error mesage.

You  must add S3 Policy in your ...READ MORE

answered Jun 27, 2018 in AWS by Flying geek
• 3,280 points
1,306 views
+1 vote
3 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP