Create a zip file on S3 from files on S3 using Lambda Node
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon S3 is a good place to store individual objects, but users often want a single downloadable archive. A common pattern is to let an AWS Lambda function read several S3 objects, stream them into a ZIP archive, and write the archive back to S3 without provisioning a server.
When Lambda Is a Good Fit
This approach works well when the archive is created on demand, the input files are already in S3, and the total runtime fits Lambda limits. The key design choice is to stream data instead of downloading everything into memory first.
If you load every object into a buffer, memory usage grows with the total archive size. A better pattern is:
- Read each source object as a stream.
- Append each stream to a ZIP writer.
- Pipe the ZIP output directly to an S3 upload stream.
That keeps memory usage predictable and avoids filling the /tmp directory unless you explicitly need it.
Streaming ZIP Data Back to S3
In modern Node.js Lambda functions, the AWS SDK for JavaScript v3 is the usual choice. For large output, @aws-sdk/lib-storage is useful because it performs multipart upload while data is still being produced.
The example below assumes the event contains a list of source keys and one destination key. It reads from one bucket and writes the ZIP file to another location in S3.
Trigger and Event Design
Many teams trigger this workflow from API Gateway, SQS, or Step Functions rather than directly from an S3 upload event. That makes it easier to pass an explicit list of keys and avoids accidental recursion.
If you do use an S3 trigger, keep the output ZIP in a different prefix or bucket. Otherwise the newly written ZIP file may trigger the same Lambda again.
A minimal test payload looks like this:
Permissions and Packaging
The Lambda execution role needs permission to read source objects and write the archive. In practice that means s3:GetObject on the input prefix and s3:PutObject on the output prefix. If the function is listing keys itself, it also needs s3:ListBucket.
You also need to package archiver with the function or include it through your deployment process. For large dependency trees, keeping the deployment bundle small still matters because cold starts are affected by package size.
Common Pitfalls
The most common failure is buffering too much data. Using await response.Body.transformToByteArray() for every file works for tiny archives but breaks down quickly.
Another issue is forgetting that Lambda has time and memory limits. If users may request very large archives, step back and ask whether Lambda is still the right compute target. In some systems, a container task or batch job is the safer choice.
A third problem is duplicate file names inside the ZIP. If two keys end with the same base name, one entry can overwrite the other in the archive. Use a stable naming scheme, such as preserving part of the prefix, when collisions are possible.
Summary
- Stream S3 objects into the ZIP instead of loading them all into memory.
- Use
archiverplus a streaming S3 upload for efficient archive creation. - Keep output in a separate prefix or bucket to avoid recursive triggers.
- Grant only the S3 permissions the function actually needs.
- Reevaluate Lambda if archive size or runtime becomes unpredictable.
Related reading
- Create AWS Athena view programmatically
- create folder inside S3 bucket using Cloudformation
- Create kubernetes nginx ingress without GCP load-balancer
- Create multiple targets using external secret operator AWS
- Create array of regex matches
- Creating a div element in jQuery
- Create table with Global secondary index using DynamoMapper and class Annotation
- Creating a lambda function in AWS from zip file

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.