Python downloading/zipping files from S3: ECS Fargate hits storage limit

Viewed 1712

I have an ECS Fargate task that downloads about 2 million CSVs that are in an S3 bucket, compresses them into a single zip archive and then saves that archive to S3. The CSVs are ~40kb. My code that runs in the Docker container is below.

I got an error though, "OSError: [Errno 28] No space left on device: '/app/data/temp/myfile.csv'"

The Fargate docs say: "For Amazon ECS on Fargate tasks using platform version 1.4.0 or later, each task receives 20 GB of ephemeral storage". (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-task-storage.html)

This makes sense that I ran into a disk error, because 40kb * 2000000 files = 80000000kb = 80Gb

I'm left with the problem of how to download these files from S3 and zip them. Is there a way to address this in parallel by generating several zip files from parallel containers? (Or a better approach entirely for generating a zip of this S3 bucket).

I can download them one by one by doing this:

aws s3 sync s3://mybucketname .

But I have been having trouble zipping them up into one file locally once they are all downloaded to my machine, my machine crashes (hence doing this on AWS).

thanks

import boto3
import os
from botocore.exceptions import ClientError
import shutil
from configs import *

s3_resource = boto3.resource('s3')
s3_client = boto3.client('s3')

source_bucket = s3_resource.Bucket(bucket)

# download CSVs
for s3_object in source_bucket.objects.all():
    path, filename = os.path.split(s3_object.key)
    source_bucket.download_file(s3_object.key, f"temp/{filename}")  # OSError: [Errno 28] No space left on device: '/app/data/temp/myfile.csv'

# archive
shutil.make_archive(f"temp/archive", 'zip', tmp_dir)

# save to s3
s3_client.upload_file(f"temp/archive.zip", bucket, "archive.zip")
2 Answers

Since recently (April 2020), ECS and Fargate have build in support for EFS:

ECS tasks using EFS will automatically mount the file systems specified by the customer in the task definition and make them available to the containers in the task across all availability zones in the region. This enables persistent, shared storage to be defined and used at the task and container level in ECS.

Therefore, the use of EFS could be a possible way which could be used to address your storage shortage.

Related