从java

时间:2017-07-13 17:31:11

标签: amazon-web-services aws-lambda

我正在尝试从另一个lamda函数(java)创建lambda函数。 这里有一段代码

LambdaFunctionCreater implements RequestHandler<Object, Object> {
...
}

我的课程中有以下方法

createNewLambdaFunction (Map<String, String> lambdaEnvProperties) {
try {
    AWSLambdaClientBuilder awsLambdaClient = AWSLambdaClient.builder();
            awsLambdaClient.setRegion(RegionUtils.getRegion("us-east-1").getName());
    CreateFunctionRequest req = new CreateFunctionRequest ();

            req.withDescription("Hello World")
            .withEnvironment(new Environment ().withVariables(myProperties))
            .withFunctionName("Sample Lambda Dynamically created"))
            .withHandler("com.sample.SampleLambda::handleRequest")
            .withRole("MyRole")
            .withRuntime("java8")
            .withMemorySize(128)
            .withTimeout(600)
            .addTagsEntry("Tag1", "Value1")
            .withCode(new FunctionCode().withS3Bucket("mybucket").withS3Key("myfile.jar").withS3ObjectVersion("latestversionid"));

    CreateFunctionResult result = awsLambdaClient.build().createFunction(req);
} catch (Exception e) {
            logger.error("Failed to create ", e.fillInStackTrace());
        }
}

上述调用失败并显示消息

  

java.lang.NullPointerExceptionnull at   com.capitalone.aws.lambda.handler.LambdaFunctionCreater.createNewLambdaFunction(LambdaFunctionCreater.java:154)null at   com.capitalone.aws.lambda.handler.LambdaFunctionCreater.handleRequest(LambdaFunctionCreater.java:104)null at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)null at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)null at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)null at   java.lang.reflect.Method.invoke(Method.java:498)null at   lambdainternal.EventHandlerLoader $ PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:456)null at   lambdainternal.EventHandlerLoader $ PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:375)null at   lambdainternal.EventHandlerLoader $ 2.call(EventHandlerLoader.java:1139)null at   lambdainternal.AWSLambda.startRuntime(AWSLambda.java:278)null at   lambdainternal.AWSLambda。(AWSLambda.java:62)null at   java.lang.Class.forName0(Native Method)null at   java.lang.Class.forName(Class.java:348)null at   lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94)空

任何见解都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

由于您未发布完整代码,因此很难评估空指针异常发生的位置。

我将代码拆分为更多部分,避免直接将新对象传递到.withEnvironment(new Environment ().withVariables(myProperties))这样的单个语句中。这很难调试,因为如果这样的语句失败,你就无法检查变量值。

我还建议您设置开发环境,以便可以直接在IDE中运行和调试代码。调试器监视会立即告诉您哪个变量是NullPointerException的源。

此NullPointerException可以是配置,权限或依赖性问题。

另请参阅这些Java Lambda examples