0

I have an AWS Lambda function in python3.7. The way its set up im running the lambda_handler(event, context) function and passing data to a separate function that calls itself multiple times depending on what is passed into it. How do I then return data from the second function?

import json
import boto3

def lambda_handler(event, context):
    # code to get initial data
    x = second_function(data)
    print(x)
    return x



def second_function(data):
    # code to manipulate data
    if condition:
       print(newData)
       second_function(newData)
    else:
       return allData

I expected this to return allData back through the lambda_handler function, but instead returns null

And logged is

newData
newData
newData
None

I am using the second function to get data based on the last PaginationToken. Is there a better way to get paginated data rather than creating a second recursive function?

2
  • Your second_function() doesn't seem to return anything if condition is true. And if condition is false, it appears that allData is empty (None). Commented Feb 11, 2019 at 0:49
  • if condition is true the second_function calls itself and sends what it has already gotten. If the condition is false (when it reaches the final page) it is supposed to return all the data combined Commented Feb 11, 2019 at 1:07

1 Answer 1

1

One option is to use a boto3 paginator.

Alternatively, you could use a loop rather than a recursive function.

It would be something like:

response = api_call()
<do stuff with response>
while response['NextToken']:
    response=api_call(NextToken=response['NextToken'])
    <do stuff with response>

You can probably avoid having to double-up the <do stuff> bit by improving on the while statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm fairly new to python and didn't think to use a while statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.