stjrc
stjrc

Reputation: 41

How to solve the AWS S3 Object Lambda function invalid token error?

I was testing Python Amazon S3 Object Lambda function and it gave an invalid token error when executing time is longer than 60s. The sample code and error messages are attached.

import boto3
import json
import struct
from botocore.session import Session
from botocore.config import Config
import time

def lambda_handler(event, context):
    print('event', event)
    object_get_context = event["getObjectContext"]
    request_route = object_get_context["outputRoute"]
    request_token = object_get_context["outputToken"]
    s3_url = object_get_context["inputS3Url"]
    
    s = Session()
    s3 = s.create_client('s3', config=Config(connect_timeout=300, read_timeout=300, retries={'max_attempts': 1}))
    
    
    print("start")
    time.sleep(60)
    print("sleep for 60s")
    
    s3.write_get_object_response(
        Body=b"123",
        RequestRoute=request_route,
        RequestToken=request_token)

    return {'status_code': 200}
    
[ERROR] ClientError: An error occurred (ValidationError) when calling the WriteGetObjectResponse operation: Invalid token

When I changed the sleep time to 50s, it executed successfully. And I've changed the both client side timeout and lambda timeout. I guess there may be some other parameters controlling this but I don't know.

Upvotes: 0

Views: 489

Answers (1)

stjrc
stjrc

Reputation: 41

It is a constraint of AWS S3 Object Lambda (not Lambda itself)

S3 Object Lambda allows up to 60 seconds to stream a complete response to its caller

Upvotes: 2

Related Questions