AWS Lambda:如何从简单的java类调用lambda函数 [英] AWS Lambda : How to call lambda function from simple java class

查看:451
本文介绍了AWS Lambda:如何从简单的java类调用lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的Lambda函数,并且上传到AWS Lambda.

I have created simple Lambda function and upload this to 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;
    }

}

}

我想使用Java类从其他项目中调用此Lambda函数.我正在使用aws-java-sdk-lambda-1.10.22调用该函数.但是我无法做到这一点.

I want to invoke this Lambda function from some other project using java class. I am using aws-java-sdk-lambda-1.10.22 to call the function. But I am not able to succeed in that.

这是我的InvokeLambda类,它是一个单独的项目.

Here is my InvokeLambda class which is a separate project.

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);
    }
}

如何使用Java调用lambda函数?

How to call the lambda function using java?

推荐答案

给出注释中的信息,调用该函数的客户端代码就可以了.问题似乎出在功能本身的配置上.具体来说,AWS Lambda无法找到您指定的处理程序(com.aws.HelloLambda::handleRequest),因为它与处理程序类(Hello)的名称和包以及该类中方法的名称(handleRequest).

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控制台更新功能处理程序名称.选择您的函数,然后选择配置"选项卡,然后选择处理程序"属性.

You can update the function handler name through the AWS Console. Choose your function, then the configuration tab and then the Handler property.

您可能希望将其从com.aws.HelloLambda::handleRequest更改为Hello::handleRequest.

You probably want to change it from com.aws.HelloLambda::handleRequest to Hello::handleRequest.

在通过Java客户端测试该功能之前,可以直接通过控制台对其进行测试,这将帮助您确保正确配置该功能.

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.

这篇关于AWS Lambda:如何从简单的java类调用lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆