解析AWS Lambda响应

时间:2019-11-22 04:26:05

标签: python aws-lambda boto3 moto

我正在为lambda函数(使用moto)编写单元测试(使用pytest)。

有效负载响应不仅包括函数的响应,还包括函数的退出状态,使用的内存,执行所花费的时间等。这与(https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke)上的文档一致: For synchronous invocation, details about the function response, including errors, are included in the response body and headers.

START RequestId: xxxxxxx-xxx-xx-xxx-xxxxxx Version: $LATEST
END RequestId: xxxxxxx-xxx-xx-xxx-xxxxxx
REPORT RequestId: xxxxxxx-xxx-xx-xxx-xxxxxx  Init Duration: 119.35 ms        Duration: 3.08 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 23 MB

{"message":"Hello pytest!"}

如何仅返回(或选择){"message":"Hello pytest!"}部分?

我已经指定了'LogType'='None'。

测试功能:

def test_lambda(lambda_client):
    function_name = 'say_hi_pytest'

    create_lambda(lambda_client)
    response = lambda_client.invoke(
       FunctionName=function_name, 
       InvocationType='RequestResponse',
       LogType='None'
       )

    payload = response['Payload'].read().decode('utf-8')
    assert payload['message'] == 'Hello pytest!' # expecting this to be True

我的处理程序:

def handler(event, context):
    return {
        'message': 'Hello pytest!'
    }

0 个答案:

没有答案