1

I have a Postgres DB on AWS that is available outside. The connection works fine when I launch my app locally.

When I launch it on AWS lambda it gives me this error:

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:454)
    at org.postgresql.Driver.connect(Driver.java:256)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at ninja.majewski.AmazonDbService.getRemoteConnection(AmazonDbService.java:84)
    at ninja.majewski.AmazonDbService.removeOldDeals(AmazonDbService.java:63)
    at ninja.majewski.ChinaStuffLambdaHandler.handleRequest(ChinaStuffLambdaHandler.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:259)
    at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
    at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
    at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:292)
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at lambdainternal.LambdaRTEn
    try.main(LambdaRTEntry.java:94)

This is the code that fails:

private Connection getRemoteConnection() throws Exception {
    Class.forName("org.postgresql.Driver");
    String dbName = System.getenv("RDS_DB_NAME");
    String userName = System.getenv("RDS_USERNAME");
    String password = System.getenv("RDS_PASSWORD");
    String hostname = System.getenv("RDS_HOSTNAME");
    String port = System.getenv("RDS_PORT");
    String jdbcUrl = "jdbc:postgresql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;
    log.info(String.format("Logging to DB: %s", jdbcUrl));
    return DriverManager.getConnection(jdbcUrl);
}

What could be the reason for this?

11
  • Is your Lambda function running inside a VPC? Commented Nov 2, 2018 at 16:42
  • No. it is not inside VPC Commented Nov 2, 2018 at 16:44
  • Is RDS_HOSTNAME set? Try to print it out, it could be null, the error shows that there is no suitable database host. Commented Nov 2, 2018 at 16:47
  • I print jdbcUrl variable and it is correct. If I copy it to IntelliJ it connects to DB correctly. All of these env variables are set Commented Nov 2, 2018 at 16:48
  • Is your RDS publicly accesible? If not, did you configured your lambda so it can access it? Commented Nov 2, 2018 at 16:51

1 Answer 1

1

In your lambda you have to configurate the Network parameter. You have to add a VPC and a Security Group.

Once this Security Group is added to your lambda, you go to your EC2 Management Console in NETWORK & SECURITY -> Security Groups. Here you select the Security Group of your RDS Instance and you Edit the Inbound parameter. You add a Type PostgreSQL with a Custom Source with the name of the Security Group you just added to your Lambda.

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

Comments

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.