简体   繁体   中英

Boto3, AWS Lambda - Results compound each call

When I call my Lambda function, the results seem to compound on each other. Every response I get is everything from the beginning of the instantiation of the function, plus the new value.

Is there a way to flush this, or at least a way to figure out what the new return are? I'm not sure if this is intended behavior or not, or if it is intended how to filter it correctly (or I'm doing something wildly wrong).


ACCESS_KEY = 'xxxxx'
SECRET_KEY = 'xxxxx'

lambda_client = boto3.client('lambda', 
                              region_name='xxxxx',
                              aws_access_key_id=ACCESS_KEY,
                              aws_secret_access_key=SECRET_KEY,)

test_event = {'firstname' : 'mark'}

def invoke_lambda(test_event):
    r = lambda_client.invoke( 
        FunctionName='Python3MySQLEndpoint',
        InvocationType='RequestResponse',
        Payload=json.dumps(test_event)
    )

    p = r['Payload']
    return p

data = invoke_lambda(test_event)
print data

results in (7 runs):

{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'xxxxx@gmail.com', u'firstname': u'mark'}, u'employee_id': 1}
{u'employee_info': {u'lastname': u'xxxxx', u'email': u'asdlkfjasldkfjlasdk@laskjfdlkdas.com', u'firstname': u'mark'}, u'employee_id': 3}

My intention would be for it only to return the two records that fit the hardcoded query.

It had nothing to do with the code in this call. I had to remove the list from outside the handler function (global) into the handler function.

from:

# array to store values to be returned
records = []

# executes upon API event
def handler(event, context):
    ...
    records.append(record)

In the above, the function continues to append to a growing list.

to:

# executes upon API event
def handler(event, context):
    # array to store values to be returned
    records = []
    ...
    records.append(record)

The list is wiped and restarted every time the function is called.

Seems the Lambda function was holding everything. I hope this helps someone. It took me trying to write this rubber ducky question to figure it out, but it was some time I'd rather not have spent :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM