简体   繁体   English

AWS Lambda:如何从简单的java类调用lambda函数

[英]AWS Lambda : How to call lambda function from simple java class

I have created simple Lambda function and upload this to AWS Lambda. 我创建了简单的Lambda函数并将其上传到AWS Lambda。

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

public class Hello implements RequestHandler<String, String> {

    @Override
    public String handleRequest(String input, Context context) {
         String output = "Bonjour, " + input + "!";
         return output;
    }

}

}

I want to invoke this Lambda function from some other project using java class. 我想使用java类从其他项目调用此Lambda函数。 I am using aws-java-sdk-lambda-1.10.22 to call the function. 我正在使用aws-java-sdk-lambda-1.10.22来调用该函数。 But I am not able to succeed in that. 但我无法成功。

Here is my InvokeLambda class which is a separate project. 这是我的InvokeLambda类,它是一个单独的项目。

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.InvokeRequest;

public class InvokeLambda {
    private static final Log logger = LogFactory.getLog(InvokeLambda.class);
    private static final String awsAccessKeyId = "XXXXXX";
    private static final String awsSecretAccessKey = "YYYY";
    private static final String regionName = "us-west-2";
    private static final String functionName = "Hello";
    private static Region region;
    private static AWSCredentials credentials;
    private static AWSLambdaClient lambdaClient;

    /**
     * The entry point into the AWS lambda function.
     */
    public static void main(String... args) {
        credentials = new BasicAWSCredentials(awsAccessKeyId,
                awsSecretAccessKey);

        lambdaClient = (credentials == null) ? new AWSLambdaClient()
                : new AWSLambdaClient(credentials);
        //lambdaClient.configureRegion(Regions.US_WEST_2);
        region = Region.getRegion(Regions.fromName(regionName));
        lambdaClient.setRegion(region);

        try {
            InvokeRequest invokeRequest = new InvokeRequest();
            invokeRequest.setFunctionName(functionName);
            invokeRequest.setPayload("\" AWS Lambda\"");
            System.out.println(byteBufferToString(
                    lambdaClient.invoke(invokeRequest).getPayload(),
                    Charset.forName("UTF-8")));
        } catch (Exception e) {
            logger.error(e.getMessage());
            // System.out.println(e.getMessage());

        }
    }

    public static String byteBufferToString(ByteBuffer buffer, Charset charset) {
        byte[] bytes;
        if (buffer.hasArray()) {
            bytes = buffer.array();
        } else {
            bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
        }
        return new String(bytes, charset);
    }
}

How to call the lambda function using java? 如何使用java调用lambda函数?

Given the information in your comment, your client code to invoke the function is fine. 根据您的评论中的信息,您调用该函数的客户端代码很好。 The problem appears to be with the configuration of the function itself. 问题似乎与功能本身的配置有关。 Specifically, AWS Lambda is not able to find the handler you've specified ( com.aws.HelloLambda::handleRequest ) because that doesn't match the name and package of your handler class ( Hello ) and the name of your method in that class ( handleRequest ). 具体来说,AWS Lambda无法找到您指定的处理程序( com.aws.HelloLambda::handleRequest ),因为它与您的处理程序类( Hello )的名称和包以及您的方法名称不匹配class( handleRequest )。

You can update the function handler name through the AWS Console. 您可以通过AWS控制台更新功能处理程序名称。 Choose your function, then the configuration tab and then the Handler property. 选择您的函数,然后选择配置选项卡,然后选择Handler属性。

You probably want to change it from com.aws.HelloLambda::handleRequest to Hello::handleRequest . 您可能希望将其从com.aws.HelloLambda::handleRequest更改为Hello::handleRequest

如何在控制台中更新Handler名称的示例

Before testing the function from your Java client, you could test it directly through the console, this will help you ensure the function is configured correctly. 在从Java客户端测试函数之前,您可以直接通过控制台对其进行测试,这将有助于确保正确配置该函数。

Another way to invoke lambda from java code is to use LambdaInvokerFactory and I found this approach cleaner. 从java代码调用lambda的另一种方法是使用LambdaInvokerFactory ,我发现这种方法更清晰。 You need to do the following: 您需要执行以下操作:

  1. Define interface representing your function and annotate method with @LambdaFunction 使用@LambdaFunction定义表示函数的接口和注释方法
  2. Create implementation of the above interface using LambdaInvokerFactory 使用LambdaInvokerFactory创建上述接口的LambdaInvokerFactory
  3. Invoke lambda function using just created proxy object (interface implementation) 使用刚创建的代理对象调用lambda函数(接口实现)

    More detailed example can be found here . 更详细的例子可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JAVA 调用 AWS lambda 函数? - How to call AWS lambda function from JAVA? 如何在 ZD52387880E1EA223817A 中的另一个 aws lambda 调用 aws lambda function - How to invoke an aws lambda function from another aws lambda in Java? 当两个函数位于同一账户、同一区域时,如何从另一个 AWS Java Lambda 函数调用 aws java lambda 函数 - How to call an aws java lambda function from another AWS Java Lambda function when both are in same account, same region 如何在 Java 中的 Lambda 函数内部调用父类中的方法? - How to call a method in parent class inside of Lambda function in Java? 如何同时调用相同的AWS Lambda function? - How to call same AWS Lambda function concurrently? 如何从Java代码调用AWS lambda函数/处理程序 - How to invoke the AWS lambda function / handler from Java code 如何将Lambda转换为简单的Java函数 - How to convert Lambda into simple Java function AWS Lambda:如何使用java从Lambda函数访问S3存储桶 - AWS Lambda : How to access S3 bucket from Lambda function using java Aws Lambda:如何在 Java 中实现的 lambda 函数中从 Api 网关获取查询参数 - Aws Lambda: How to get query params from Api Gateway in lambda function implemented in Java 尝试从 aws lambda 处理程序函数调用 SDN 时出现 java.lang.NullPointerException - java.lang.NullPointerException when trying to call SDN from aws lambda handler function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM