Node.js Elastic Beanstalk TIMEDOUT Error

Posted on

I got an error when deploying my Node.js app into AWS Elastic Beanstalk (EB). The error said

Error: connect ETIMEDOUT at errnoException (net.js:901:11) at Object.afterConnect [as oncomplete] (net.js:892:19) -------------------- at Protocol._enqueue (/var/app/current/node_modules/mysql/lib/protocol/Protocol.js:110:26) at Protocol.handshake (/var/app/current/node_modules/mysql/lib/protocol/Protocol.js:42:41)

My Node.js app using node-mysql module to access database. You can see "mysql" in the error statement. Seeing from errors, this must be related with database. I use Amazon Relational Database Service (RDS) and my node.js app is configured to connect with it.

After exploring this error, I know why it is occurred. It is caused by security group of my EB instance which is not allowed to connect to DB instance in RDS. To overcome this problem, follow these steps:

  1. Go to app in Elastic Beanstalk 2) Choose the environment 3) Choose configuration then choose instance 4) Check its EC2 security groups

[caption id="attachment_32" align="alignnone" width="918"]eb-ec2-security-groupsEC2 Security Groups[/caption]

  1. Go to RDS menu

[caption id="attachment_33" align="alignnone" width="635"]RDS menuRDS menu[/caption]

  1. Choose DB Instance

  2. Add the security group you found at step 4 to DB Instance

[caption id="attachment_34" align="alignnone" width="1099"]Add security group to db instanceAdd security group to db instance[/caption]

  1. Click Authorize

  2. Finish

I can conclude that everytime you create new EB instance you need to add its security group to DB instance in RDS so your Node.js app can connect to it. Hope this article helps you.