1

I have an index.js file which contains code like the following:

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

The code correctly gets data from the mysql database and displays them on the screen when I run it on my local machine.

However, I got Task timed out after 7.01 seconds error when it is run on aws-lambda.

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

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

The only log message being printed by my function is function triggered. I cannot find other log messages generated by my function in the cloud watch log.

Why does the function timed out on aws-lambda?

1 Answer 1

3

If I had to guess it is a permissions issue, when you run locally it is going to grab credentials from your local machine/environment - when 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, make sure that the mysql database is accessible to the lamba - i.e. your not trying to access a mysql database that is local to your machine from the lambda function (I was assuming you were using rds).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Your guess is right. I didn't properly set the IAM role for the lambda function.
By the way, do you know how to configure the inbound rule of my RDS's security group so that it can be accessed by the lambda function? I don't want RDS to be accessible from any where.
@Brian You just need to allow inbound traffic to your RDS instance from your Lambda's security group only. i.e., specify a security group instead of an IP address for the inbound rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.