Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda handlers in Java, the package is unavailable

I'm trying to implement an AWS Lambda handler in Java. I use this dependency in my pom.xml:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-lambda</artifactId>
    <version>1.11.487</version>
</dependency>

In my Java code:

import com.amazonaws.services.lambda.runtimeContext;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEven;

The IDE is complaining that there is no com.amazonaws.services.lambda package (and sub-packages). Tried the project re-import - no difference.

The full class names are taken from the AWS documentation here: https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-handler-types.html

I suspect that I use some wrong dependency. What Maven dependency is actually needed here?

like image 762
user3791111 Avatar asked May 13 '26 05:05

user3791111


2 Answers

The Java Lambda stuff is a bit confusing. There is one library that you need to write a Lambda and an optional library to handle Amazon based events.

These libraries are somewhat documented on this page.

To use the aws-lambda-java-core library in maven you'll want to use:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-core</artifactId>
    <version>1.2.0</version>
</dependency>

as your Maven dependency.

To use aws-lambda-java-events (the Lambda events library) use the dependency

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>2.2.5</version>
</dependency>

in your pom.xml.

In your example com.amazonaws.services.lambda.runtime.Context and com.amazonaws.services.lambda.runtime.RequestHandler come from the aws-lambda-java-core library. Your post looks like you have a copy/paste issue in the first import.

But com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent and com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent (again, your post has a typo in it) come from aws-lambda-java-events. This library simplifies dealing with AWS events. Your code looks like it wants to handle API Gateway Proxy events.

Be warned, however, that by pulling in the aws-lambda-java-events library you pull in a huge number of AWS libraries. For example, even if your Lambda only wants API Gateway events you'll still pull in a dependency on the S3 library as the events library also handles S3 events. So your Lambda deployment package will be significantly bigger than if you don't pull that in.

As an alternative to the events library you can use something like JsonPath to pull what you want out of the event. Your Lambda would be something like:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.jayway.jsonpath.JsonPath;

public class YourLambdaHandler implements RequestStreamHandler {
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
        String accountId = JsonPath.read(inputStream, "$.requestContext.accountId");

Which would read the accountId from the API Gateway event.

There is no question that this way is a bit more work but if you're only pulling a few items out of the event then it may be a lighter weight way to go.

EDIT

It looks like you have a build / IDE issue. So, the smallest amount of code I can show is first a working pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yourpackage.handler</groupId>
    <artifactId>apigateway</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <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.2.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

And a Lambda handler:

package com.yourpackage.handler;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

@SuppressWarnings("unused")
public class DemoHandler implements RequestHandler<APIGatewayProxyResponseEvent, String> {
    public String handleRequest(APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent, Context context) {
        return "hello";
    }
}

Can you start with a new environment and try this out?

like image 179
stdunbar Avatar answered May 14 '26 19:05

stdunbar


The following dependencies worked for me

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>2.2.6</version>
    </dependency>
</dependencies>
like image 24
Mohammed Siddiq Avatar answered May 14 '26 19:05

Mohammed Siddiq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!