I am trying to connect to my redshift database using com.amazon.redshift.jdbc.Driver I wrote a simple class which connects to the database and executes a select query. I have created a simple gradle project which creates fat jar.
When I run it in my local machine everything works fine. But when I deployed it into AWS Lambda, it gives me below error
{
"errorMessage": "No suitable driver found for jdbc:redshift://xyz/xyz",
"errorType": "java.sql.SQLException",
"stackTrace": [
"java.sql.DriverManager.getConnection(DriverManager.java:689)",
"java.sql.DriverManager.getConnection(DriverManager.java:208)",
"test.DatabaseConnector.grab(DatabaseConnector.kt:204)",
"test.DatabaseConnector.generateHTMLReport(DatabaseConnector.kt:271)",
"test.DatabaseConnector.sendReport(DatabaseConnector.kt:606)",
"test.HelloWorldLambda.runReport(HelloWorldLambda.kt:15)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
]
}
I am not sure why the same jar works in local and not in AWS lambda. Below is the code
Class.forName(driverName)
val connection = DriverManager.getConnection(url,
Properties().apply {
setProperty("user", username)
setProperty("password", password)
})
val stmt = connection.createStatement()
val rs = stmt.executeQuery(query)
while (rs.next()) {
println(rs.getString("name"))
}
rs.close()
stmt.close()
connection.close()
Can anybody help in fixing this?
If your using Maven, make sure to use redshift-jdbc42-no-awssdk. Also, apparently there was a bug in particular 1.2.10.1009 version, look here.
The only working version seems to be "1.2.8.1005" which is the oldest available version on the Maven repository.
Another thing that you need to do is to make sure that your jar file needs to be a so called "fat jar" which would contain all of the dependencies of your application.
You can make a fat jar by using maven-assembly-plugin as the packaging life cycle:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Handler</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With