Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda Java, connect to MySQL RDS

I need to develop an AWS Lambda Java function to retrieve some records from RDS MySQL database.

Should I use JDBC? Should I use the standard JDBC example:

try {
    String url = "jdbc:msql://200.210.220.1:1114/Demo";
    Connection conn = DriverManager.getConnection(url,"","");
    Statement stmt = conn.createStatement();
    ResultSet rs;

    rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
    while ( rs.next() ) {
        String lastName = rs.getString("Lname");
        System.out.println(lastName);
    }
    conn.close();
} catch (Exception e) {
    System.err.println("Got an exception! ");
    System.err.println(e.getMessage());
}
like image 844
giò Avatar asked Apr 18 '16 13:04

giò


People also ask

Can Lambda connect to RDS?

Lambda can work seamlessly with RDS instances, as long as you remember the specific requirements for this particular setup. Since RDS instances are running in your VPC and Lambda by default does not have access to those resources, you'll need to configure the VPC connection when creating a Lambda function.

How do I access RDS SQL server from AWS Lambda function?

Create a Lambda function to access the ExampleDB database, create a table (Employee), add a few records, and retrieve the records from the table. Invoke the Lambda function and verify the query results. This is how you verify that your Lambda function was able to access the RDS MySQL instance in the VPC.

How do I connect to AWS RDS MySQL database?

Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/ . In the navigation pane, choose Databases to display a list of your DB instances. Choose the name of the MySQL DB instance to display its details. On the Connectivity & security tab, copy the endpoint.

How do I link my Amazon RDS to Java?

First, open the security group to your RDS DB instance to allow traffic from your computer. Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list.


2 Answers

Step 1:

  1. login IAM console
  2. roles -> create new roles
  3. role name :lambda-vpc-execution-role
  4. AWS service roles ->

    a) Select aws lambda

    b) Attach policy "AWSLambdaFullAccess"

Step 2:

  1. Get code from https://github.com/vinayselvaraj/lambda-jdbc-sample (note this is maven project)
  2. Right click on project select Run as --->5.maven build...

    for goal provide name package shade:shade

  3. Go to project folder and target/lamda-0.0.1-SNAPSHOT-shaded.jar

Step 3:

  1. Login to lambda console(skip blueprint)
  2. Create new lambda
  3. name: time-test

    a) runtime-java

    b) upload .zip(.jar) file (target/lamda-0.0.1-SNAPSHOT-shaded.jar)

  4. Provide package.class-name::myhandler -> Handler

  5. Role -> lambda-vpc-exceution-role

  6. vpc provide rds-vpc details (this should work in same vpc group)

  7. Create function

In the Action drop down list select configure test event result will shown down like this "Execution result: succeeded (logs)"

like image 152
Ravikumar Mangipudi Avatar answered Sep 19 '22 22:09

Ravikumar Mangipudi


Yes, you need to use standard JDBC code in your lambda function class. The code you provided looks okay. There are a few more things you need to do when accessing RDS or any other RDBMS through a Lamda function -

  1. Create a jar or a zip file for your Lambda function
  2. Your zip file needs to have a lib folder in which your JDBC driver file goes. The Lambda function doc says this is one of the two standard ways, but it didn't work for me.
  3. You can create a jar file in which the driver classes are put in. This works. The best way to do it is through the Maven Shade plugin, which extracts the JDBC drivers and packs the classes in one single jar file.
  4. Setup the handler function and specify it at the time of Lambda deployment
  5. Define execution role and VPC as needed.
  6. Upload and publish your jar or zip file.

You can test the Lambda function through the console, and see the actual output in the CloudWatch logs.

like image 42
unmeshk Avatar answered Sep 20 '22 22:09

unmeshk