How do I create a zip file from an existing Java project that can be uploaded to an AWS Lambda function??
I can't find any clear steps on how to do this. I want to export my Eclipse project as a zip file and then upload it to AWS Lambda. Currently, I use the AWS Toolkit in Eclipse to upload the project, but now I want to do it manually using a zip file.
What are the steps?
This is how I tried to do it. This is where I export the project as a zip:
Then, I go to AWS and choose to upload a zip file:
I choose my zip file and then test the code and this is the error I get:
{
"errorMessage": "Error loading class com.amazonaws.lambda.demo.LambdaFunctionHandler: com/amazonaws/auth/AWSCredentialsProvider",
"errorType": "java.lang.NoClassDefFoundError"
}
EDIT: The function works if I upload using the Toolkit so I know that all resources are in the folder, I just need to figure out how to use it. And this is the function's code:
public class LambdaFunctionHandler extends IOException implements RequestHandler<SNSEvent, String> {
private static AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1)
.withCredentials(DefaultAWSCredentialsProviderChain.getInstance()).build();
public LambdaFunctionHandler() {
}
@Override
public String handleRequest(SNSEvent event, Context context) {
try {
System.out.println("Hello world");
return "SUCCESS2";
} catch (Exception e) {
System.out.println("caught exception");
}
return "FAILURE2";
}
}
EDIT #2: This is my pom.xml
file:
<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.amazonaws.lambda</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.12.91</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.hierynomus/sshj -->
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.29.0</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>
CodePudding user response:
You can create a Maven project in Eclipse and then Export that as a JAR file that you can upload to the AWS Management Console. Its very easy to do so. To learn how to do this use case, see this AWS tutorial. It uses IntelliJ (just switch that for Eclipse), and follow the rest of the doc.
The example use case for this Lambda function is to detect personal protective equipment (PPE) in images located in an Amazon Simple Storage Service (Amazon S3) bucket. It steps you through all steps including deploying the Lambda function.
Data that is placed in the DynamoDB table:
Your error is saying it cannot find the cred provider. When using Lambda, you do not need to set creds in your Java code. The permissions are set in the IAM role. You need to set the policy for the IAM role so it can invoke the services. SO in this example use case, we set policies for lambda-support.
Another Update
From Eclipse, I build a project using your code (I think this PPE is pretty advanced - so we will make a new one named Getting Started with Lambda). It worked well: