0

I am currently trying to get into coding against AWS and I ran into an issue when trying to subscribe a user to a SNS topic.

So what I have is a lambda expression that is supposed to take in an email and send that on to SNS. I can call the expression and that is working fine, but when it tries to create the AmazonSNSClient I get a java.lang.NoClassDefFoundError: com/amazonaws/services/sns/AmazonSNSClient

The code is quite simple:

public String handleRequest(SubscriptionRequest request, Context context) {
    LambdaLogger logger = context.getLogger();
    logger.log("Subscribing to list with email:" + request.email);
    logger.log("Creating SNS client");
    AmazonSNSClient snsClient = new AmazonSNSClient(new AnonymousAWSCredentials());
    snsClient.setRegion(Region.getRegion(Regions.EU_WEST_1));
    //subscribe to an SNS topic
    SubscribeRequest subRequest = new SubscribeRequest(TOPIC_ARN, "email", request.email);
    logger.log("Sending request");
    snsClient.subscribe(subRequest);
    return "OK";
}

There are probably tons of other errors in it as well, but that is what I got so far, with the following dependencies

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sns</artifactId>
        <version>1.11.328</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>1.11.328</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.328</version>
    </dependency>
</dependencies>

I think that I have added more dependencies than I really need, but since I don't know where the issue is, I just tried adding anything that made some sort of sense.

So question is:

What am I missing, and what is causing the NoClassDefFoundError?

edit Since the constructor for the AmazonSNSClient is deprecated I changed the line to

    logger.log("Creating SNS client");
    AmazonSNS snsClient = AmazonSNSClientBuilder.defaultClient();

But I still get errors with with it not finding the AmazonSNSClientBuilder.

It is almost as if it is not adding sns dependency to the jar file, which would be odd since the lambda dependency seems to be working fine

munHunger
  • 2,572
  • 5
  • 34
  • 63

1 Answers1

0

found the solution here: AWS Lambda NoClassDefFoundError

had to make it into an uberjar with this piece of pom config

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
munHunger
  • 2,572
  • 5
  • 34
  • 63