AWS Lambda 使用 Java 与 AWS RDS 集成,但在加载类软件/amazon/awssdk/auth/credentials/AwsCredentialsProvider 时遇到错误
Posted
技术标签:
【中文标题】AWS Lambda 使用 Java 与 AWS RDS 集成,但在加载类软件/amazon/awssdk/auth/credentials/AwsCredentialsProvider 时遇到错误【英文标题】:AWS Lambda integrate with AWS RDS using Java but facing Error loading class software/amazon/awssdk/auth/credentials/AwsCredentialsProvider 【发布时间】:2021-12-27 05:51:06 【问题描述】:这是我使用 java 在 aws lambda 中上传的代码。 我使用 mvn clean package 制作了一个 jar 并上传到 lambda 函数服务,但出现错误 java.lang.NoClassDefFoundError: software/amazon/awssdk/auth/credentials/AwsCredentialsProvider
请帮帮我。
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rdsdata.RdsDataClient;
import software.amazon.awssdk.services.rdsdata.model.ExecuteStatementRequest;
import software.amazon.awssdk.services.rdsdata.model.ExecuteStatementResponse;
import java.util.Collections;
public class LambdaJavaAPI implements RequestHandler<APIGatewayProxyRequestEvent, Object>
public static final String AURORA_CLUSTER_ARN_ENV = "cluster key";
public static final String DATABASE_NAME_ENV = "mylab";
public static final String AURORA_SECRET_ARN_ENV = "security manager key";
/**
* @return an instance of RdsClient
*/
public static RdsDataClient rdsClient()
Region region = Region.US_EAST_1;
return RdsDataClient.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(region)
.httpClientBuilder(UrlConnectionHttpClient.builder()).build();
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context)
String responseBody = "Test Event";
final RdsDataClient rdsDataClient = new LambdaJavaAPI().rdsClient();
ExecuteStatementRequest request = ExecuteStatementRequest.builder()
.database(DATABASE_NAME_ENV)
.resourceArn(AURORA_CLUSTER_ARN_ENV)
.secretArn(AURORA_SECRET_ARN_ENV)
.sql(String.format("select name from users",DATABASE_NAME_ENV))
.build();
ExecuteStatementResponse executeStatementResponse = rdsDataClient.executeStatement(request);
if (executeStatementResponse.hasRecords())
return new APIGatewayProxyResponseEvent().withStatusCode(200)
.withIsBase64Encoded(Boolean.FALSE)
.withHeaders(Collections.emptyMap())
.withBody(responseBody);
【问题讨论】:
【参考方案1】:AwsCredentialsProvider
存在于https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.12.110
如果你不使用 Maven 和 Dependencies,则下载 jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk/1.12.110/aws-java-sdk-1.12.110.jar
您需要将其包含在您打包的 jar
的库中,或者必须将其添加到您的类路径中,例如 java -cp
获取生成的 jar,重命名为 .zip 并解压缩,然后查看内容。里面有这个罐子吗?
【讨论】:
@***.com/users/7064617/sma - 我已经在 pom.xml 中添加了依赖项。 请打开你的罐子看看里面的内容【参考方案2】:我用 mvn clean package 做了一个 jar
这只是打包你的类;它不包括依赖项。
相反,使用 Maven shade
插件创建一个“uberjar”。这在 AWS 文档中有所描述:https://docs.aws.amazon.com/lambda/latest/dg/java-package.html
【讨论】:
以上是关于AWS Lambda 使用 Java 与 AWS RDS 集成,但在加载类软件/amazon/awssdk/auth/credentials/AwsCredentialsProvider 时遇到错误的主要内容,如果未能解决你的问题,请参考以下文章
如何通过忽略未使用的资源从 aws cli 创建 aws java lambda 函数?
使用 AWS SDK for Java 调用 AWS Lambda 函数时如何检索 context.done() 消息?