2

I want to use AWS lambda to compute the address of the lat/long we receive from a device. Every time I receive lat/long I want to invoke AWS lambda to compute address by calling API and then save address in MYSQL database(not using Amazon here). Can I connect my database server(someIP) with AWS lambda?

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • 2
    Your specific question is to connect an AWS Lambda function to a MySQL database? (please also specify the language of the function). If this is the case, isn't [this](http://stackoverflow.com/q/31809890/1535071) helpful for you? – imTachu Feb 07 '17 at 10:48
  • Are you saying that your MySQL database is *not* inside AWS? Is it accessible from the Internet? – Michael - sqlbot Feb 07 '17 at 13:24
  • @Michael-sqlbot Yes that's correct. – Vivek Sadh Feb 07 '17 at 14:00

4 Answers4

0

Yes you can.

Previously it was not supported but nowadays you can have a sql connection in your lambda function. Refer to this guide.

Keep in mind however that you have to increase your lambda function execution time, since opening a connection inside a lambda function takes some time

gkatzioura
  • 2,655
  • 2
  • 26
  • 39
0

Yes, that doable. Here an example for Node.js:

var mysql = require('mysql');
this.getDBConnection = function (infoDict) {
    var connection = mysql.createConnection({
        host: infoDict.DB_HOST,
        user: infoDict.DB_USER,
        password: infoDict.DB_PASSWORD,
        database: infoDict.DB_NAME
     });
    return connection;
},
this.closeDBConnection = function (connection) {
    connection.end(function (r) {
        if (r) {
            connection.destroy();
        }
    });
}

Two things to check:

  • Need to make sure that you enabled connections outbound to the IP where your MySQL database is hosted.
  • Your MySQL accepts remote connections.
Franklin
  • 881
  • 1
  • 8
  • 28
0

To add on to Franklins answer:

  1. Run the Lambda inside a VPC for improved security. If the DB is running inside the VPC then you can create a restricted SecurityGroup to access the EC2. If outside AWS then you can create a route via a NAT Gateway with a known Elastic IP, which allows you to limit access once again.
  2. Make sure that you close the connection to the DB otherwise the Lambda may not exit and you will get timeout errors.
Geoff
  • 1,237
  • 1
  • 10
  • 15
0

Yes. You can connect to a database outside Amazon using an AWS Lambda function. I would not do it though.

I don't know how many devices and how frequently they are sending lat/long to your service but, as AWS Lambda will scale and run in potentially in parallel you can put your database out of service easily by making a new connection each time a device sends it's location.

I would use the Lambda function to call the API, get the result and put on an Amazon SQS Queue. Another component would be responsible for processing each result on the queue and putting the information in the database.

This way you would not create one connection for each lat/long received and would have control of how many connections you want to keep open.