How to download 6Gb - 500Gb files using python within lambda

Viewed 293

On an hourly basis I have to download files ranging from 6-10Gb and on a daily basis I have to download a file that ranges from 25-45Gb and monthly the file ranges from 400-500Gb. Currently this is all being done using perl and it works great but now we are moving to AWS and Lambda and with Lambda's 15 minute max process time my code times out. If I happen to have a files that is less than 5.5Gb the lambda python function I have written works just fine. So my hope is that someone has had to tackle this sort of problem and can help me out. Here is a stripped down version of the function as it sits right now.

import boto3
from botocore.exceptions import ClientError
import datetime
from datetime import datetime, timedelta
import dateutil.tz
import urllib3
import time

def lambda_handler(event, context):
    http = urllib3.PoolManager()
    s3 = boto3.client('s3') 
    s3object = boto3.resource('s3')

    bucketName = "test-bucket"
    checkDir = "check"

    fileURL = 'https://export.dnsdb.info/dnsdb-export/mtbl/dns.20200930.0700.H.mtbl'
    # Check to see if the file exists in S3 bucket
    fileExists = True
    
    try:
        s3.head_object(Bucket=bucketName, Key=checkDir + '/' + file)
    except ClientError as e:
        fileExists = False

    # If the file doesn't exist, download it
    if not fileExists:
        http = urllib3.PoolManager()
        hdr={'X-API-Key' : 'api-key'}
        response = http.request('GET', fileURL, preload_content=False, headers=hdr)
        s3.upload_fileobj(response, bucketName, checkDir + '/' + file)
        response = s3.head_object(Bucket=bucketName, Key=checkDir + '/' + file)
2 Answers

AWS Lambda can only be used for 15 minutes per execution. This means if your download takes longer than that, you will get a timeout like you describe. Essentially you're limited by your download speed, but getting a 500 GB file in under 15 minutes means downloading with >560 MB/s which is unrealistic in most scenarios. Perhaps consider using an EC2 instead of Lambda.

Here are a few ideas:

Related