1

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!

4
  • currently, can you access using ssh to ec2 instance ? Commented Nov 19, 2019 at 10:35
  • Yes I can access it using SSH Commented Nov 19, 2019 at 10:51
  • that mean ec2 instance go to the internet via internet GW not via NAT Gateway Commented Nov 19, 2019 at 10:58
  • What do you mean? I don't understand!! Commented Nov 19, 2019 at 11:18

1 Answer 1

1

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

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

4 Comments

I am following the first point. Let me try the second point. Can you please direct me to some article?
Do I still need to setup NAT Gateway for RDS also?
Add RDS into subnet which route via NAT

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.