Mario Vasilev
Mario Vasilev

Reputation: 73

TypeError: A Future or coroutine is required with AWS lambda

I am setting up a lambda function which makes asynchronous requests using asyncio and aiohttp. Even thought the code works fine while running locally, once I upload it to lambda it returns:

"errorMessage": "A Future or coroutine is required"

I've looked at already opened issues such as Boto3: A Future or coroutine is required and TypeError: A Future or coroutine is required but couldn't make it to work.

I am not sure why is returning the error message while I have a defined coroutine

base_url = "https://www.reed.co.uk/api/1.0/jobs/"

url_list = [
"38012438",
"38012437",
"38012436"]

def lambda_handler(event, context):
client = boto3.client('s3',
                      aws_access_key_id="aws_access_key_id",
                      aws_secret_access_key="aws_secret_access_key")

async def fetch(session, url):
    auth = aiohttp.BasicAuth(login='login', password='')
    async with aiohttp.ClientSession(auth=auth) as session:
        async with session.get(url) as response:
            return await response.json()

async def fetch_all(urls, loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        results = await asyncio.gather(*[fetch(session, base_url + url) for url in url_list], return_exceptions=True)
        return results

loop = asyncio.get_event_loop()
htmls = loop.run_until_complete(fetch_all(urls, loop))
#I believe this is where the coroutine is 

with open("/tmp/JOBS.json", "w") as f:
    json.dump(htmls, f)

I just want the combined content of my requests to be uploaded to a json file.

I apologise for my limited coding skills as I am new to python, lambda and etc.

Upvotes: 4

Views: 649

Answers (1)

Ruslan Ksalov
Ruslan Ksalov

Reputation: 111

Check your requirements.txt file. I got the same error when I added asyncio to the requirements.txt. It seems like AWS uses a special version of python. Asyncio is a part of python. And if you install it separately then it doesn't work in AWS Lambda.

Upvotes: 2

Related Questions