How to invoke the AWS lambda function / handler from Java code

前端 未结 5 1548
耶瑟儿~
耶瑟儿~ 2021-01-19 18:40

I am new to AWS lambda I have created a lambda function with handler

example.Orders::orderHandler

And this is the custom handler, now I wa

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 19:02

    Use this LATEST code to invoke a Lambda Function Synchronously:

        final String AWS_ACCESS_KEY_ID = "xx";
        final String AWS_SECRET_ACCESS_KEY = "xx";
    
        AWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
    
        // ARN
        String functionName = "XXXX";
    
        //This will convert object to JSON String
        String inputJSON = new Gson().toJson(userActivity);
    
        InvokeRequest lmbRequest = new InvokeRequest()
                .withFunctionName(functionName)
                .withPayload(inputJSON);
    
        lmbRequest.setInvocationType(InvocationType.RequestResponse);
    
        AWSLambda lambda = AWSLambdaClientBuilder.standard()
                .withRegion(Regions.US_EAST_1)
                .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
    
        InvokeResult lmbResult = lambda.invoke(lmbRequest);
    
        String resultJSON = new String(lmbResult.getPayload().array(), Charset.forName("UTF-8"));
    
        System.out.println(resultJSON);
    

    Use these dependencies to avoid any errors:

        
            org.codehaus.janino
            janino
            2.6.1
        
    
        //Required by BeanstalkDeploy.groovy at runtime
        
            org.apache.httpcomponents
            httpclient
            4.5.2
            runtime
        
    
        
            com.amazonaws
            aws-java-sdk-lambda
            1.11.207
        
    

提交回复
热议问题