riboseinc / terraform-aws-s3-cloudfront-website

Terraform module for creating a static S3 website with CloudFront with an SSL certificate (e.g., from ACM)
Apache License 2.0
74 stars 40 forks source link

Support compressed uploads #31

Open ronaldtse opened 3 years ago

ronaldtse commented 3 years ago

This post gives a good introduction to Lambda-based zip file extraction, based on seek (so you don't need to expand the whole archive at once, since a Lambda only has 500MB disk space):

https://alexwlchan.net/2019/02/working-with-large-s3-objects/

The idea here builds on top of #30 - we have a bucket with this structure:

|--- uploads
|--- 20210201000000
       |---  .done
       |--- index.html
       |--- foo
             |--- bar.html
|--- 20210301000000
       |---  .done
       |--- index.html
       |--- foo
             |--- bar.html

This work involves:

Suppose we wish to upload a 20210401000000.zip.

The process goes:

  1. Upload 20210401000000.zip to /uploads/
  2. Once 20210401000000.zip is uploaded, the Lambda zip extraction function is triggered, and it extracts 20210401000000.zip into a local disk directory. Then, it uploads the content to S3 with correct MIME types.
  3. When the extraction is done, the Lambda function uploads the /{name}/.done file to mark that upload is complete.

Then I am not sure who should do the CloudFront invalidation. There are two choices:

  1. The user to do the invalidation. The user needs to monitor (via polling, e.g. aws s3 ls) that the archive extraction and upload from Lambda is complete (via monitoring the presence of the /{name}/.done file). This has the benefit of ensuring that the GHA build flow only succeeds when the deploy actually succeeds.
  2. Some other Lambda function will perform the CloudFront invalidation when it detects that /{name}/.done is created. But this way the GHA build flow won't know that deployment has failed, because the GHA flow would already succeed when the initial zip archive upload is complete. Unless the user also monitors the deployment until the end, which by then the user can ask CloudFront to invalidate anyway.

Thoughts @phuonghuynh @strogonoff ?

ronaldtse commented 3 years ago

Ping @skalee since this is relevant to you too.

strogonoff commented 3 years ago

This is neat!

strogonoff commented 3 years ago

I’m running tests with S3 uploads to acceleration-enabled and normal S3 buckets, but no strong results yet.

ronaldtse commented 3 years ago

And when the Lambda function uploads to S3, we should use parallel processes for maximum speed (https://github.com/riboseinc/terraform-aws-s3-cloudfront-website/issues/29#issuecomment-789722054).

ronaldtse commented 3 years ago

Module creation now located here: https://github.com/riboseinc/terraform-aws-lambda-s3-archive-extract-upload/issues/1

strogonoff commented 3 years ago

And when the Lambda function uploads to S3, we should use parallel processes for maximum speed (https://github.com/riboseinc/terraform-aws-s3-cloudfront-website/issues/29#issuecomment-789722054).

Just to clarify, parallel uploads I mentioned only apply in the case where we upload individual files, not in the zipped case. As I mentioned, I believe those are mutually exclusive

ronaldtse commented 3 years ago

Yes, terraform-aws-lambda-s3-archive-extract-upload#1 Step 3 describes using concurrency when the Lambda function uploads to S3 after unzipping.

phuonghuynh commented 2 years ago

@ronaldtse should I start implement lambda / find a solution for this one?

ronaldtse commented 2 years ago

@phuonghuynh any solution is fine, as long as it works. Thanks!