ARINDAM BANERJEE
ARINDAM BANERJEE

Reputation: 689

AWS Lambda in Java unable to connect to Oracle RDS using connection wallet

I am trying to connect to AWS Oracle RDS from Lambda written in java. Binary of connection wallet used in order to make the connection.

When I use it from SQLDeveloper, it works fine. But same does not happen when I use this java code.

In the SQLDeveloper I am using custom jdbc url which is: dbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCPS)(HOST =myhost100.mydomain.fr) (PORT = 2499)))(CONNECT_DATA = (SID = MYSID01))(SECURITY = (SSL_SERVER_CERT_DN = "C=US,ST=Washington,L=Seattle,O=Amazon.com,OU=RDS,CN=mydb100.xyzabc.eu-west-3.rds.amazonaws.com")))

Hence same is used in the code as well.

I have added the binary in the jar as well.

Here is the java code and pom.xml.

It is unable to connect to RDS and eventually times out.

The RDS is in private VPC, which are in line with lambda that means VPC, Subnet and Security groups are aligned.

Oracle version: 19.0.0.0.ru-2021-04.rur-2021-04.r1

Java:

package fr.globalhealthcheck;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;


public class MeteoHandler  implements RequestHandler<Map<String,String>, String>{

    public String handleRequest(Map<String,String> event, Context context)
    {
        LambdaLogger logger = context.getLogger();

        String response = new String("200 OK");
            
        testConnection();

        return response;
    }

    public void testConnection()
    {
        try      
        {
            System.out.println("Before connect");
            System.setProperty("oracle.net.SSL_SERVER_DN_MATCH", "ON");
            System.setProperty("oracle.net.SSL_CYFER_SUITES", "(SSL_RSA_WITH_AES_256_CBC_SHA)");
            System.setProperty("oracle.net.ssl_version1", "1.0");
            System.setProperty("oracle.net.SSL_CLIENT_AUTHENTICATION", "FALSE");
            System.setProperty("oracle.net.wallet_location", "/var/task/cwallet.sso");
            Connection conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCPS)(HOST =myhost100.mydomain.fr) (PORT = 2499)))(CONNECT_DATA = (SID = MYSID01))(SECURITY = (SSL_SERVER_CERT_DN = \"C=US,ST=Washington,L=Seattle,O=Amazon.com,OU=RDS,CN=mydb100.xyzabc.eu-west-3.rds.amazonaws.com\")))", 
                    "myuser", "mypassword");
            System.out.println("After connect");
            if (conn != null) {
                System.out.println("Connected to the database!");
            } else {
                System.out.println("Failed to make connection!");
            }

        } catch (SQLException e) {
            System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

Maven:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fr.globalhealthcheck</groupId>
    <artifactId>global-healthcheck</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>global-healthcheck</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> 
            <version>2.8.6</version> </dependency> -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-log4j2</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc11</artifactId>
            <version>21.1.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/lib/binary</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        fr.globalhealthcheck.MeteoHandler
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>           
        </plugins>
    </build>
</project>

cwallet.sso is connection wallet which is added in the binary folder, and referred in the maven.

So I have printed it to verify the path in the lambda as well which is /var/task/cwallet.sso

Upvotes: 0

Views: 654

Answers (1)

Nirmala
Nirmala

Reputation: 1338

One of the properties that you are using is incorrect. oracle.net.SSL_CYFER_SUITES --> oracle.net.SSL_CIPHER_SUITES

What is the JDBC driver version that you are using? Also, you can check out this blog for 12.2 and lower. Otherwise, refer to this page.

Upvotes: 0

Related Questions