Home > Net >  AWS Lambda integrate with AWS RDS using Java but facing Error loading class software/amazon/awssdk/a
AWS Lambda integrate with AWS RDS using Java but facing Error loading class software/amazon/awssdk/a

Time:11-17

This is my code which uploaded in aws lambda using java. I have made a jar using mvn clean package and uploaded to lambda function service but getting error java.lang.NoClassDefFoundError: software/amazon/awssdk/auth/credentials/AwsCredentialsProvider

Please help me out.


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

CodePudding user response:

AwsCredentialsProvider exists in https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.12.110

If you do not use Maven and Dependencies, then download the jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk/1.12.110/aws-java-sdk-1.12.110.jar

You need to either include this in your libs in your packaged jar or it must be added to your classpath like java -cp

Take your generated jar, rename to .zip and unzip it, and see the contents. Does it have this jar in it?

CodePudding user response:

I have made a jar using mvn clean package

This just packages up your classes; it does not include dependencies.

Instead, create an "uberjar" using the Maven shade plugin. This is described in the AWS documentation: https://docs.aws.amazon.com/lambda/latest/dg/java-package.html

  • Related