Amazon SQS调用lambda函数

时间:2018-10-23 15:13:31

标签: java aws-lambda amazon-sqs

我正在尝试通过Amazon SGS触发器来触发功能。触发器工作正常,但是消息未传递到my函数中。

这是我的lambda函数

import java.text.SimpleDateFormat;
import java.util.Calendar;

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

public class x implements RequestHandler<RequestClass, ResponseClass> {
    private LambdaLogger logger;

    public void log(String message) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        logger.log(sdf.format(cal.getTime()) + "  " + message+"\n");
    }

    public ResponseClass handleRequest(RequestClass request, Context context) {
        this.logger = context.getLogger();
        log("Request " + request);
        if (request == null || (request.getFilename() == null && request.getRecords() == null)) {
            log("No file was passed in");
            throw new RuntimeException("No file was passed in");
        }


        return new ResponseClass(null);
    }
}

请求类为https://pastebin.com/Q1G6bnrA

当我看到日志时,记录始终为空。

2 个答案:

答案 0 :(得分:0)

您是否照顾过Lambda的执行角色权限?

来自here

执行角色权限

Lambda需要以下权限才能管理Amazon SQS队列中的消息。将它们添加到函数的执行角色中。

  • sqs:ReceiveMessage
  • sqs:DeleteMessage
  • sqs:GetQueueAttributes

答案 1 :(得分:0)

以下代码对我来说很好:

@Repository
public interface ImportMovieDAO extends JpaRepository<ImportMovie, Long> {
  @NotNull
  @Override
  @EntityGraph(value = "graph.ImportMovie.videoPaths")
  List<ImportMovie> findAll();
}

也许尝试使用package au.com.redbarn.aws.lambda2lambda_via_sqs; import java.util.List; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.SQSEvent; import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage; import lombok.extern.log4j.Log4j2; @Log4j2 public class SQSConsumerLambda implements RequestHandler<SQSEvent, String> { @Override public String handleRequest(SQSEvent input, Context context) { log.info("message received"); List<SQSMessage> records = input.getRecords(); for (SQSMessage record : records) { log.info(record.getBody()); } return "Ok"; } } 而不是您自己的SQSEvent