Dinesh Rawat
Dinesh Rawat

Reputation: 1019

AWS Lambda function failing to access MySQL Database from an EC2 Instance : SequelizeConnectionError connect ETIMEDOUT

I am trying to access the MySQL database installed in an EC2 instance. I have written a simple AWS Lambda function in express js. The lambda function needs to access the MySQL. I am getting the below everyone when it is trying to establish the DB connection on EC2:

{
    "errorType": "SequelizeConnectionError",
    "errorMessage": "connect ETIMEDOUT",
    "name": "SequelizeConnectionError",
    "parent": {
        "errorType": "Error",
        "errorMessage": "connect ETIMEDOUT",
        "code": "ETIMEDOUT",
        "errorno": "ETIMEDOUT",
        "syscall": "connect",
        "fatal": true,
        "stack": [
            "Error: connect ETIMEDOUT",
            "    at Connection._handleTimeoutError (/var/task/node_modules/mysql2/lib/connection.js:173:17)",
            "    at ontimeout (timers.js:436:11)",
            "    at tryOnTimeout (timers.js:300:5)",
            "    at listOnTimeout (timers.js:263:5)",
            "    at Timer.processTimers (timers.js:223:10)"
        ]
    },
    "original": {
        "errorType": "Error",
        "errorMessage": "connect ETIMEDOUT",
        "code": "ETIMEDOUT",
        "errorno": "ETIMEDOUT",
        "syscall": "connect",
        "fatal": true,
        "stack": [
            "Error: connect ETIMEDOUT",
            "    at Connection._handleTimeoutError (/var/task/node_modules/mysql2/lib/connection.js:173:17)",
            "    at ontimeout (timers.js:436:11)",
            "    at tryOnTimeout (timers.js:300:5)",
            "    at listOnTimeout (timers.js:263:5)",
            "    at Timer.processTimers (timers.js:223:10)"
        ]
    },
    "stack": [
        "SequelizeConnectionError: connect ETIMEDOUT",
        "    at Utils.Promise.tap.then.catch.err (/var/task/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:149:19)",
        "    at tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23)",
        "    at Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:512:31)",
        "    at Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:569:18)",
        "    at Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:614:10)",
        "    at Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:689:18)",
        "    at Async._drainQueue (/var/task/node_modules/bluebird/js/release/async.js:133:16)",
        "    at Async._drainQueues (/var/task/node_modules/bluebird/js/release/async.js:143:10)",
        "    at Immediate.Async.drainQueues [as _onImmediate] (/var/task/node_modules/bluebird/js/release/async.js:17:14)",
        "    at runCallback (timers.js:705:18)",
        "    at tryOnImmediate (timers.js:676:5)",
        "    at processImmediate (timers.js:658:5)"
    ]
}

The ec2 configurations are:-

EC2 Instance configuration

Inbound Rules: Inbound rules

Outbound Rules Outbound Rules*

Lambda function Lambda management console

NOTE: vpc, subnet, and security group are same in EC2 and lambda

Here is my code:- Lambda Function Code

Any help is highly appreciated!

Upvotes: 1

Views: 1483

Answers (1)

Thanh Nguyen Van
Thanh Nguyen Van

Reputation: 11812

If you want to connect from lambda function to ec2 instance.

Firstly, Ec2 instances and lambda function are being in the same VPC, and

Secondly, you have to create NAT gateway for lambda function. And subnet of EC2 instance and subnet of lambda function should be routed via the same NAT gateway.

For example :

enter image description here

enter image description here

Upvotes: 1

Related Questions