Home > Enterprise >  AWS Lambda cutting my payload in lambda invocation
AWS Lambda cutting my payload in lambda invocation

Time:01-06

I fried my brain a long day with a weird problem. I'm trying to send a .docx file document encoded in base64, initially, I thought that the AWS Api Gateway was cutting my body request, but invoking directly via awscli I check the lambda is cutting part of the body payload. Let me show you:

Here a snippet of my lambda function:

def lambda_handler(event, context):
    if event["httpMethod"] == "GET":
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from PDFTron! Please send base64 doc to use this Lambda function!')
        }

    elif event["httpMethod"] == "POST":
        try:
            output_path    = "/tmp/"
            body           = json.loads(str(event["body"])) # <--- the error is thrown here
            base64str      = body["file"]["data"]
            filename       = body["file"]["filename"]
            base64_bytes   = base64decode(base64str)
            input_filename = filename.split('.')[0]   "docx"

Can you check the full file in lambda_function.py

Ok, now I try call my function via awscli with this command line:

aws lambda invoke \
  --profile lab \
  --region us-east-1 \
  --function-name pdf_sign \
  --cli-binary-format raw-in-base64-out \
  --log-type Tail \
  --query 'LogResult' \
  --output text \
  --payload file://payload.json response.json | base64 -d

payload.json

And receive the error:

C/ZcP1zcsiKiso3qnIUFO0Bk9/LjB7EOzkNAA7GgFDYu2A7RzzmPege9ijNyW/K0LvQKiYYtd21rNKycfuvBIr8syxsO7wi2gebCTwnZmHG x/9N2jid9MWX uApnxQ19L5TCPJbetnNGoe94JNV1A5VV5seZPWJ7BMTa7WFKCvBRyBeXWiivCsFH5FY7lRQGmmC8rq6Ezzj4rP3ndEKabbyq9HBRddi8TQILtJ7wfMQQU1sQL8FgwdJFXIqvhhL9a8EHwEJC2oblN8d1U1MbLTqYEnty1Z1EQT/bRCPoNJq18okfXuc70GjC0U0P2m5l6z4riKkoS3YXgWjLLIxbCQD7nzEIGuDHeWe ADzsBybqyRyBOeBAxk0ED5XN1SITy31hv8QW ViBw2j1ExOruxU44 sS9d7ZQ9yvXqog7O0v6MhDfxHfPa1W6ULOY7y3Jgt/9XgbuOVptXclLf5GWQesSErNLTXaTWTQTxSI6FL emt3UJzivn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MS54bWxQSwUGAAAAABEAEQBdBAAAQn8GAAAA'}}}
[ERROR] Runtime.MarshalError: Unable to marshal response: Object of type KeyError is not JSON serializable
Traceback (most recent call last):END RequestId: a26b93bd-bcf2-4072-99da-29ffcc3bc350
REPORT RequestId: a26b93bd-bcf2-4072-99da-29ffcc3bc350  Duration: 11.92 ms      Billed Duration: 12 ms  Memory Size: 1024 MB    Max Memory Used: 93 MB

When I add a debug (print command) on my code I can check that the payload is cutted and in the log error conntains the the final part of my payload (eg. RX4GAGN1c3RvbVhtbC9pdGVtMS54bWxQSwUGAAAAABEAEQBdBAAAQn8GAAAA'}}}.

Reading the aws doc about lambda quotas and limits, we can see that the invocation payload limit is 6MB, very different than my payload size (232kb)

Resource Quota
Invocation payload (request and response) 6 MB each for request and response (synchronous); 256 KB (asynchronous)

Source: https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

I really think I'm doing all things, into the rules, someone here can tell me and help me, where am i going wrong or misunderstand?

I expect the payload of my request arrives correct in my python function.

CodePudding user response:

How you logged event in your Lambda function?

It seems like the problem is this line: body = json.loads(str(event["body"]))

You are using the CLI and based on the docs, the Event will be sent as a String. So you need to JSON parse the event object first.

            output_path    = "/tmp/"
            e = json.loads(event)
            body           = str(e["body"])
            base64str      = body["file"]["data"]
            filename       = body["file"]["filename"]
            base64_bytes   = base64decode(base64str)
            input_filename = filename.split('.')[0]   "docx"

  • Related