AWS Lambda Java,连接到 MySQL RDS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36695481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:46:46  来源:igfitidea点击:

AWS Lambda Java, connect to MySQL RDS

javaamazon-web-servicesaws-lambda

提问by giò

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

我需要开发一个 AWS Lambda Java 函数来从 RDS MySQL 数据库中检索一些记录。

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

我应该使用JDBC吗?我应该使用标准的 JDBC 示例:

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());
}

回答by Ravikumar Mangipudi

Step 1:

步骤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"

  1. 登录 IAM 控制台
  2. 角色 -> 创建新角色
  3. 角色名称:lambda-vpc-execution-role
  4. AWS 服务角色 ->

    a) 选择 aws lambda

    b) 附加策略“AWSLambdaFullAccess”

Step 2:

第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

  1. https://github.com/vinayselvaraj/lambda-jdbc-sample获取代码(注意这是 maven 项目)
  2. 右键单击项目选择 Run as --->5.maven build...

    为目标提供名称 package shade:shade

  3. 转到项目文件夹和 target/lamda-0.0.1-SNAPSHOT-shaded.jar

Step 3:

第 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

  1. 登录 lambda 控制台(跳过蓝图)
  2. 创建新的 lambda
  3. 名称:时间测试

    a) 运行时-java

    b) 上传 .zip(.jar) 文件 (target/lamda-0.0.1-SNAPSHOT-shaded.jar)

  4. 提供package.class-name::myhandler-> 处理程序

  5. 角色-> lambda-vpc-exceution-role

  6. vpc 提供 rds-vpc 详细信息(这应该在同一个 vpc 组中工作)

  7. 创建函数

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

在操作下拉列表中选择配置测试事件结果将显示如下“执行结果:成功(日志)”

回答by unmeshk

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 -

是的,您需要在 lambda 函数类中使用标准的 JDBC 代码。您提供的代码看起来没问题。通过 Lamda 函数访问 RDS 或任何其他 RDBMS 时,您还需要做一些事情 -

  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.
  1. 为您的 Lambda 函数创建 jar 或 zip 文件
  2. 您的 zip 文件需要有一个 lib 文件夹,您的 JDBC 驱动程序文件在其中。Lambda 函数文档说这是两种标准方式之一,但它对我不起作用。
  3. 您可以创建一个 jar 文件,在其中放入驱动程序类。这是可行的。最好的方法是通过 Maven Shade 插件,它提取 JDBC 驱动程序并将类打包到一个单独的 jar 文件中。
  4. 设置处理函数并在 Lambda 部署时指定它
  5. 根据需要定义执行角色和 VPC。
  6. 上传并发布您的 jar 或 zip 文件。

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

您可以通过控制台测试 Lambda 函数,并在 CloudWatch 日志中查看实际输出。

回答by Alejandro Garcia

You could use this kinda implementation:

你可以使用这种实现:

public static DataSource getDataSource(){

    Utils._logger.log("Get data source");
    MysqlDataSource mysqlDs = null;
    try{
        mysqlDs = new MysqlDataSource();
        mysqlDs.setURL('jdbc:msql://'+'url');
        mysqlDs.setUser('user');
        mysqlDs.setPassword('pwd');
        Utils._logger.log("Object "+mysqlDs.getUrl()+" "+mysqlDs.getUser()+" ");
        return mysqlDs;
    }
    catch(Exception e) {
        Utils._logger.log("No se pudo abrir el archivo de properties");
        e.printStackTrace();
    }
    return mysqlDs;

}