0

I am currently trying to connect to an amazon RDS from a lambda function. The problem I am facing is that I do not get an error but neither I manage to get a connection, so I am not sure how to proceed. I do not have a timeout, or an error with the parameters, but the object "con" is always null.

To do this I am doing the following:

Connection con = null;
String jdbcUrl2 = "jdbc:mysql://" + "connectionValueExample-zone.rds.amazonaws.com" + ":" + "3306" + "/" + "botdb" + "?user=" + "bot" + "&password=" + "PWvalueExample";
con = DriverManager.getConnection(jdbcUrl2);

After every step I check with prints() how it's going. After I try to stablish the connection I try to catch it, because while I am not getting exceptions right now, the object con never stops being null, so I do not stablish a connection.

catch (SQLException e) { 
    e.toString();
    log.warn(e.toString());
} catch (Exception e) {
    System.out.println("excepcion no capturada ");
    e.toString();               
}

The second catch is just in case I was missing something, originally it was not there.

All in all, the code looks something like:

@Override
public String handleRequest(Object input, Context context) {
    context.getLogger().log("Input: " + input);
    Connection con = null;
    try {
        String jdbcUrl2 = "jdbc:mysql://" + "value-zone.rds.amazonaws.com" + ":" + "3306" + "/" + "botdb" + "?user=" + "bot" + "&password=" + "PW";
        con = DriverManager.getConnection(jdbcUrl2);
    } catch (SQLException e) { 
        e.toString();
        log.warn(e.toString());
    } catch (Exception e) {
        System.out.println("excepcion no capturada ");
        e.toString();               
    }
    String status = null;
    if (con != null) {
        status = "conexion establecida";
        System.out.println("connection stablished");
    } else status = "connection failed";
    return status; 
}

Edit:

I am now capturing slightly better the exceptions and get the following message:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

At this point I am unsure if the Strin jdbcURL is not properly formed OR why is the connection failing.

alexrnov
  • 2,346
  • 3
  • 18
  • 34
monkey intern
  • 705
  • 3
  • 14
  • 34
  • 1
    How are you testing your code. – LMC Feb 27 '18 at 15:27
  • My bad, I should have said so. To run the code, once I configured the eclipse plugin with my keys, with AWS you can create a lambda function and then, you can give values to your variables (the final goal is to use system.getenv() ). Once that set up is done, I go to my code, right click, select "AWS lambda", then "run function on lambda" and mark "show the logs". So, I test it by running the lambda function. – monkey intern Feb 27 '18 at 15:31
  • 1
    Does that work with a simple example? If yes, try to debug your code. – LMC Feb 27 '18 at 15:57
  • I am not sure I understand your question, but since this is running in an amazon server, to debug it is quite tricky for someone with as little experience as myself. I started reading about AWS SAM and it seemed like its own can of worms to open. Basically, I do not know of a way to debug locally. @LuisMuñoz – monkey intern Feb 27 '18 at 16:00
  • 1
    ok, I suggest to write a simple 'hello world' to verify your testing setup is working. That way you will make sure the problem is in your code and not elsewhere. – LMC Feb 27 '18 at 16:15
  • I have been able to succesfully launch small code like hello world with lambda before. The problem arises when I try to connect, with lambda, to a RDS. Right now I am getting an error I was not capturing before, I am going to update the post to reflect so. – monkey intern Feb 27 '18 at 16:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165913/discussion-between-luis-munoz-and-monkey-intern). – LMC Feb 27 '18 at 16:17
  • If you want to access RDS from an AWS Lambda, you need to configure the correct VPC(s) for your lambda, see https://docs.aws.amazon.com/lambda/latest/dg/vpc.html : _"By default, resources within a VPC are not accessible from within a Lambda function. "_ (posting as a comment as I have never done this myself, I just read it today) – Mark Rotteveel Feb 27 '18 at 16:20
  • Thank you for all your advice!! The VPC has been previously set up by my architect. Also, another mistake I was doing was not configuring the lambda function properly. Now it is supposedly correct, with the right VPC configured. – monkey intern Feb 27 '18 at 16:23
  • Np, Glad you solved it. – LMC Feb 27 '18 at 17:44
  • I mean, it still does not connect, but at least I know what type of error it's throwing (jdbc4.CommunicationsException). Now, how to solve that... – monkey intern Feb 28 '18 at 07:42
  • 1
    Look at https://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql (and linked questions) – Mark Rotteveel Feb 28 '18 at 09:53

0 Answers0