这很简单。这段代码以jar和lambda的形式工作(maven项目):
package test2;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class test implements RequestHandler<Map<String,String>, String>{
@Override
public String handleRequest(Map<String,String> event, Context context)
{
String prueba = "Prueba";
return prueba;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Testing def");
}
}
带有依赖项的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test2</groupId>
<artifactId>test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.2</version>
</dependency>
</dependencies>
</project>
我将它导出为一个可运行的jar,并带有它的运行配置:export; runnable jar file; runable configuration and package required libraries
然后我去localstack:
aws--endpoint-url = http://localhost:4566 lambda函数--function-name Test3--runtime java17--handler test2.test::javaRequest--role arn:aws:iam::0000000000:role/RolLambda--zip-file fileb://./test.jar
aws--endpoint-url = http://localhost:4566 lambda invoke--function-name Test3 output.json
输出:It works!
现在是不能作为lambda工作的代码(也是maven项目)。我只添加了一个KinesisClient对象:
package main;
import java.net.URI;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kinesis.KinesisClient;
public class Test implements RequestHandler<Map<String,String>, String>{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Testing def");
}
@Override
public String handleRequest(Map<String,String> event, Context context)
{
String Error = "kinesis_Error";
String prueba = "Prueba";
try {
KinesisClient kinesis_client = KinesisClient.builder().region(Region.US_EAST_1).endpointOverride(URI.create("http://127.0.0.1:4566")).build();
} catch (Exception e) {
return Error;
}
return prueba;
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>definitive_test</groupId>
<artifactId>definitive_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>kinesis</artifactId>
<version>2.20.135</version>
</dependency>
</dependencies>
</project>
我做同样的步骤:- 使用properly configuration set运行jar-创建lambda并执行:
aws--endpoint-url = http://localhost:4566 lambda函数--function-name Test3--runtime java17--handler main. Test::JavaScript请求--role arn:aws:iam::0000000000:role/RolLambda--zip-file fileb://./test.jar
但现在我得到了未处理的:
aws--endpoint-url = http://localhost:4566 lambda invoke--function-name Test3 output.json
{
"StatusCode": 200,
"FunctionError": "Unhandled",
"ExecutedVersion": "$LATEST"
}
输出:
{"errorMessage ":"software/amazon/awssdk/services/kinesis/KinesisClient","errorType":" java 。lang。NoClassDefFoundError "," stackTrace ":["主。测试。请检查请求(Test.java:25)"," java. base/jdk.internal.reflect.NativeMethodSystemorImpl.invoke0(Native Method)"," java. base/jdk.内部的反思。NativeMethods工具实现invoke(Unknown Source)"," java. base/jdk.内部的反思。DelegatingMethods委托或实施invoke(Unknown Source)"," java. base/java. lang。反思。方法。invoke(Unknown Source)"]," cause ":{"errorMessage":"软件。亚马逊。awssdk。服务。运动KinesisClient "," 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)"," main.测试。请检查请求(Test.java:25)"," java. base/jdk.internal.reflect.NativeMethodSystemorImpl.invoke0(Native Method)"," java. base/jdk.内部的反思。NativeMethods工具实现invoke(Unknown Source)"," java. base/jdk.内部的反思。DelegatingMethods委托或实施invoke(Unknown Source)"," java. base/java. lang。反思。方法。调用(未知源)"]}}
我不知道为什么会这样我已经检查了角色权限。我认为这是好的(它的arn arn:aws:iam::0000000000:role/RolLambda)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucket1/*"
},
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::bucket1/*"
},
{
"Effect": "Allow",
"Action": [
"kinesis:PutRecord",
"kinesis:PutRecords",
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:ListStreams"
],
"Resource": "*"
}
]
}
有什么建议吗?谢谢你的时间
2条答案
按热度按时间juzqafwq1#
最后一个问题是jar编译。下面是一个basic pom.xml的构建,它将jar依赖项放入final jar:
您可以在dependenciesflag中放置任何您需要的dependency。你需要在mainclassflag中输入你的包和主类(在我的例子中是productor.Productor)。属性设置java版本和编码。现在使用@smac2020命令mvn package,您可以正确地创建JavaScript文件。现在我在一个更复杂的代码中面临一个aws凭证问题,但那是另一个历史:
7nbnzgx92#
我将测试这段代码。但是,我会做一些改变。首先,为了确保所有的依赖关系都包含在你的FAT文件中,我将使用以下命令创建一个FAT文件:
mvn包
然后,为了测试它,我将上传到Lambda AWS控制台,并使用AWS控制台直接调用Lambda函数。
顺便说一句-你的问题是这样的:
errorMessage”:“software/amazon/awssdk/services/kinesis/KinesisClient”,“errorType”:“java.lang.NoClassDefFoundError”,“stackTrace”:
找不到Kinesisclient。这意味着您没有正确地创建文件夹。
更新
正如所承诺的-这是结果。当使用mvn包构建并通过www.example.com上的AWS Lambda控制台部署时,代码可以正常工作https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions
Java代码。
我使用AWS Lambda控制台部署了它:
我测试了它,它工作得很好:
以下是日志中的输出:
最后是我的POM(可能有一些额外的依赖项):