Johann Monnerie
Johann Monnerie

Reputation: 91

Put-object on private Amazon S3 from a Lambda function leads to timeout

I'm pretty new to AWS Lambda functions.

OBJECTIVE:

I'm trying to get a .xlsx file from a website and put it on a private Amazon S3 bucket.

PROBLEM:

The following code leads to a timeout when running the put_object function and I don't know how doing now ... What am I doing wrong? I'm so close...
This code works on our backend to write to a file.

CODE:

import json
import boto3
from botocore.vendored import requests
import json

s3 = boto3.client('s3')

def lambda_handler(event, context):
    url = "https://some_url"
    path = "first_folder_in_my_private_bucket/subfolder/subsubfolder/subsubsubfolder/"
    key = f"{path}test.xlsx"
    headers = {
        "User-Agent": "Mozilla/5.0",
        "Connexion": "keep-alive",
        "Accept-Encoding": "gzip, deflate, br"}
    body = {
        "login": "our_website_login",
        "password": "our_website_password"}
    login = requests.post(url=f"{url}index.php", data=body)

    if login.status_code == 200:
        response = requests.get(
            url=f"{url}export_excel.php",
            allow_redirects=True,
            headers=headers,
            cookies=login.cookies)

        # Here response.content have the good payload, and we reach this point
        s3.put_object(Bucket="maprochaineauto-s3", Key=key, Body=response.content)

        # We never reach this point and the error response is next this code sample
        return {"response":"Worked"}
    return {"response":"Not worked"}

Error (I changed all ids by blablaID):

Test Event Name
test

Response
{
  "errorMessage": "blablatimestamp blablacode Task timed out after 3.00 seconds"
}

Function Logs
START RequestId: blablaId Version: $LATEST
/opt/python/botocore/vendored/requests/api.py:65: DeprecationWarning: You are using the post() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.post() function instead.
  warnings.warn(
/opt/python/botocore/vendored/requests/api.py:65: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.get() function instead.
  warnings.warn(
END RequestId: Blabla ID
REPORT RequestId: BlablaID  Duration: 3003.50 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 75 MB  Init Duration: 571.68 ms    
2021-06-14T08:41:22.413Z BlablaID Task timed out after 3.00 seconds

Request ID
BlablaID

CONFIG:

I installed a layer to normally prevent the requests warnings but it's not really working, as you see.
I don't have permissions to see role permissions on the lambda, but "normally" it's a god-like lambda function with a lot of permissions.

Upvotes: 1

Views: 755

Answers (1)

Marcin
Marcin

Reputation: 238507

Based on the comments.

The issue was caused by a default lambda timeout of 3 seconds. Increasing the timeout in AWS console was the solution to the problem reported.

Upvotes: 1

Related Questions