【问题标题】:How to parse input from AWS event rule in lambda?如何在 lambda 中解析来自 AWS 事件规则的输入?
【发布时间】:2020-05-13 09:52:45
【问题描述】:

如果我在事件规则中使用输入......我应该如何在 lambda 中解析它?

现在我有:

  MyJobScheduledRule:
    Type: AWS::Events::Rule
    Properties:
      Description: Scheduled Rule
      ScheduleExpression: !Sub "rate(${IntervalMinutes} minutes)"
      State: "ENABLED"
      Targets:
        - Id: "MyJobLambda"
          Arn: !GetAtt MyJobLambda.Arn
          Input: "\"{\\\"key\\\":\\\"value\\\"}\"

使用以下 lambda:

public class MyJobLambda implements RequestHandler<Map<String, String>, Void>  {
  private static Logger LOGGER = LoggerFactory.getLogger(MyJobLambda.class);

  @Override
  public Void handleRequest(Map<String, String> event, Context context) {

    LOGGER.debug("MyJob got value {} from input", event.get("key"));

    return null;
  }

}

但我得到以下运行时异常:

An error occurred during JSON parsing: java.lang.RuntimeException
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('
{
    "key": "value"
}

我也尝试过使用 POJO 作为 lambda 的输入。有什么想法吗?

【问题讨论】:

  • 您可以打印出event 对象并在 CloudWatch Logs 中检查其结构
  • event.readValueAsTree().toString()
  • 你的情况是什么类型的事件?

标签: amazon-web-services events aws-lambda jobs


【解决方案1】:

使用 ScheduledEvent 对象,并解析事件的详细信息字段

import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
public class MyJobLambda implements RequestHandler<Map<String, String>, Void>  {
  private static Logger LOGGER = LoggerFactory.getLogger(MyJobLambda.class);

  @Override
  public Void handleRequest(ScheduledEvent event, Context context) {

    LOGGER.debug("MyJob got value {} from input", mapScheduledEventDetail(event.getDetail());

    return null;
  }

 private String mapScheduledEventDetail(Map<String,Object> detailObject) {
       return ParserUtil.parseObjectToJson(detailObject.get("clientId"));
    }

}

【讨论】:

  • 得到这个编译错误Class ... must either be declared abstract or implement abstract method 'handleRequest(I, Context)' in 'RequestHandler'。如何编译?
【解决方案2】:
import os
import boto3
import json

#EVENT JSON PARSER (SYED)
def lambda_handler(event, context):
print('starting a new execution')
print('## ENVIRONMENT VARIABLES')
print(os.environ)
AMI = os.environ['AMI']
print('printing os variable AMI')
print(AMI)
print('#########################')
print('PRINTING EVENT')
print(event)
#return event
print('taking message payload from event')
#messagepayload = event['Records'][0]['Sns']['Message']
#the event[] gives a simple string
#the json.loads converts that string into a json/python dictionary that you can use
#[0] denotes first part of the array
messagepayload = json.loads(event['Records'][0]['Sns']['Message'])
print(messagepayload)
#key2 represents a key in the message payload json 
#next we take value for the key named as key2
key2value = messagepayload['key2']
print(key2value)
return key2value
print('END OF EXECUTION')

【讨论】:

  • 您能否详细说明这是如何解决问题的?
猜你喜欢
  • 2019-04-14
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多