如果我在事件规则中使用输入..我该如何在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的输入。有什么想法吗?
发布于 2021-05-08 02:42:40
使用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"));
}
}
发布于 2021-03-12 22:02:15
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')
https://stackoverflow.com/questions/61771458
复制相似问题