Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

This is my node.js code:

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 is working fine as it is retrieving data from the database and displaying on the screen when I run it locally.

I am receiving an error if I run it in Lambda, which shows 7.01 seconds. Shows that the task has timed out.

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?

1 Answer

0 votes
by (44.4k points)

I will put in some pointers for you to remember while doing this, mostly those points will answer your question:

  1. Make your RDS instance publicly accessible else the Lambda function will not be able to establish connection. This might be the biggest reason why it is not working.

  2. Try debugging the code with multiple console.log statements and find out where the error is.

  3. If RDS is publicly accessible and connection is been made but it is timing out; then try a sample code to connect to the database.

  4. Also, add the context.succeed(“Some message”) con.end() in the node.js code

As you are new to AWS, I would suggest you to take a look at this informative AWS Tutorial and also if you are looking to become an expert in the same, check out the AWS Training page.

 

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...