skip to Main Content

I am trying to run a simple Java program in aws, but I am getting the error from the title.

A little context:

This is my pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>processador-de-dados</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <name>Archetype - processador-de-dados</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>1.12.565</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-core</artifactId>
      <version>1.12.565</version>
    </dependency>
  </dependencies>
</project>

And this is the class I want to run. Note that almost everything is commented out, because I was removing comments to find where the lambda is breaking

package amazonenv;

import com.amazonaws.auth.BasicAWSCredentials;
import processador.ProcessadorDeDados;

import java.util.List;

public class Main {

    public String executeLambda() {

//         Utilizar aqui seus valores de chave de acesso, nome do bucket e região da sua conta AWS
        String accessKey = "hiddenValue";
        String secretKey = "hiddenValue";
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

//        S3Downloader downloader = new S3Downloader();
//        List<String> linhasCsv = downloader.downloadFileFromS3(credentials);

//        if (linhasCsv != null) {

//            ProcessadorDeDados processador = new ProcessadorDeDados();
//            processador.processaDados(linhasCsv);
//
//            S3Uploader uploader = new S3Uploader();
//            uploader.upload(credentials);

            return "Executed";
//        }
//        return "Not executed";
    }
}

Note that by creating an instance of BasicAwsCredentials the method breaks.

Lastly, here is the error log from aws

{
  "errorMessage": "com/amazonaws/auth/BasicAWSCredentials",
  "errorType": "java.lang.NoClassDefFoundError",
  "stackTrace": [
    "amazonenv.Main.executeLambda(Main.java:15)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/java.lang.reflect.Method.invoke(Unknown Source)"
  ],
  "cause": {
    "errorMessage": "com.amazonaws.auth.BasicAWSCredentials",
    "errorType": "java.lang.ClassNotFoundException",
    "stackTrace": [
      "java.base/java.net.URLClassLoader.findClass(Unknown Source)",
      "java.base/java.lang.ClassLoader.loadClass(Unknown Source)",
      "java.base/java.lang.ClassLoader.loadClass(Unknown Source)",
      "amazonenv.Main.executeLambda(Main.java:15)",
      "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
      "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)",
      "java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)",
      "java.base/java.lang.reflect.Method.invoke(Unknown Source)"
    ]
  }
}

If I run this method locally it works. Also, if I call the method with only the return "Executed", the Lambda also works

Any help is appreciated.

2

Answers


  1. May I suggest not storing aws credentials in code?
    Instead, create an AWS role which has the minimum necessary permissions (S3ReadOnly) to access your S3 bucket. You can do that in the AWS Lambda console under Permissions.
    You can use the AWS boto3 java sdk to get your bucket content in your Lambda code once your Lambda has the S3 permissions.

    Login or Signup to reply.
  2. I recreated your code in a new maven project on IntelliJ. I added the PSVM method, and ran the "executeLambda" from there, but I did not encounter any issues.

    I would suggest rebuilding your project, or reimporting your pom.xml file.

    Furtheremore, the other person here is very correct; it’s not good practice to hard code in credentials.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search