我有一个AWS Python lambda函数,我想在其中调用另一个lambda函数。 我有一个有效的硬编码有效负载,我尝试对其进行重现以使其更适合我的用例。因此,使用相同的数据定义payload_dict。
我还附上了受人尊敬的用法的印刷品作为内联注释,以防万一希望使它成为一个更好的问题。
简而言之,问题是:
如何使json.dumps(payload_dict).encode()
的行为与lambda_client.invoke中的硬编码版本相同?
在Python 3.6上运行
from datetime import datetime, timedelta
import boto3
import sys
import json
import ast
def lambda_handler(event, context):
startTime = datetime.today() - timedelta(days=0)
endTime = datetime.today() - timedelta(days=1)
payload_dict = {}
payload_dict['instanceID'] = "i-id"
payload_dict['StartTime'] = startTime.isoformat(' ', 'seconds')
payload_dict['EndTime'] = endTime.isoformat(' ', 'seconds')
print("PAYLOAD: ",json.dumps(payload_dict).encode()) # b'{"instanceID": "i-id", "StartTime": "2018-08-03 19:11:32", "EndTime": "2018-08-02 19:11:32"}'
#actually working payload
payload=b"""{
"instanceID": "i-id",
"StartTime": "2018-08-01 00:00:00",
"EndTime": "2018-08-02 00:00:00"
}"""
#b'{"instanceID": "i-id","StartTime": "2018-08-01 00:00:00", "EndTime": "2018-08-02 00:00:00"}'
调用另一个lambda:
invoke_response = lambda_client.invoke(FunctionName="daily_average_cpu_utilisation_lambda",
InvocationType='RequestResponse',
LogType='Tail',
Payload = payload
)
在调用时从lambda_client.invoke
获取json.dumps(payload_dict).encode()
作为有效载荷的信息时发生的错误
percentage_usage = invoke_response['Payload'].read().decode('utf8').replace("'", '"'
)
:
Response:
{
"errorMessage": "Expecting ',' delimiter: line 1 column 289 (char 288)",
"errorType": "JSONDecodeError",
"stackTrace": [
[
"/var/task/lambda_function.py",
46,
"lambda_handler",
"percentage_dict = json.loads(percentage_usage)"
],
[
"/var/lang/lib/python3.6/json/__init__.py",
354,
"loads",
"return _default_decoder.decode(s)"
],
[
"/var/lang/lib/python3.6/json/decoder.py",
339,
"decode",
"obj, end = self.raw_decode(s, idx=_w(s, 0).end())"
],
[
"/var/lang/lib/python3.6/json/decoder.py",
355,
"raw_decode",
"obj, end = self.scan_once(s, idx)"
]
]
}
希望这是一个很好的问题,并且非常感谢您的帮助,至少直到现在,我还是看不到或调试过,如何看似相同的负载如何如此不同地解释,也很可能是我错过的显而易见的东西