studds / nx-aws

AWS plugins for nx
36 stars 10 forks source link

s3:deploy options description #116

Open etylsarin opened 1 year ago

etylsarin commented 1 year ago

Hi @studds,

first of all, thank you for creation of these useful builders. I have a question specific to @nx-aws/s3:deploy: What are the possible options? can you please improve the documentation a bit so it's clear how to use it to deploy static files to S3? And can you think of how to improve the setup to zip it before uploading to s3?

Thanks in advance, Filip

studds commented 1 year ago

Hey @etylsarin glad you're finding it useful!

I've added some docs in #117, let me know if there's anything missing or requires clarification.

By "zip it before uploading to s3", I assume you mean compressing static files to save transfer time on the client? Because I always use CloudFront, and CloudFront supports compression, this is not something I've needed. (I also use CloudFront to "proxy" api requests, so that most of my apps don't need any environment config, and so that I don't need to worry about CORS most of the time - the app and api have a single origin, from the POV of the client.)

Are you wanting to use a "naked" s3 bucket, and so needing to compress the files before uploading? If so, I'd be open to a PR, and would be happy to discuss the best approach and identify how it can be integrated.

etylsarin commented 1 year ago

Hi @studds,

thanks for the docs, that's exactly what I needed.

Yes, I meant some compression before the upload and yes, I want to copy to "naked" S3 bucket, without the CloudFront. I like the concept separation of concerns, so I'm not saying that compression step should be baked in, but it would be nice to have some preTargets and postTargets to be able to execute some job before/after the task on the same file set.

What do you think?

Thank you, Filip

studds commented 1 year ago

Great idea @etylsarin, will be much better to separate it out.

Have you looked at dependsOn to handle the pre/post targets?

If you have a configuration like:

 {
     ...
     "app": {
         "root": "apps/app",
         "sourceRoot": "apps/app/src",
         "projectType": "application",
         "schematics": {},
         "architect": {
             ...
            "build": {
                "builder": "nx:superbuilder",
                "outputs": ["dist/apps/app"],
                "options": {
                    ...
                }
            },
            "compress": {
                "builder": "nx:run-commands",
                "outputs": ["dist/apps/app"],
                "dependsOn": ["^build"],
                "options": {
                    "parallel": false,
                    "commands": [
                        "echo \"Compressing files....\""
                }
            },
            "deploy": {
                 "builder": "@nx-aws/s3:deploy",
                 "dependsOn": ["^compress"],
                 "options": {
                     "destPrefix": "public",
                     "bucket": {
                         "targetName": "api:deploy",
                         "outputName": "WebBucket"
                     },
                     "distribution": {
                         "targetName": "api:deploy",
                         "outputName": "DistributionId"
                     },
                     "config": {
                         "configFileName": "config.json",
                         "importStackOutputs": {}
                     }
                 },
                 "configurations": {
                     "production": {
                         "stackSuffix": "prod"
                     }
                 }
             }
         }
     }
 }

... this will cause nx to run compress whenever you run deploy, and build whenever you run compress. Specifying the outputs will ensure that the outputs are cached, and so the targets will only get re-run when inputs change. (Forgive me if I'm telling you what you already know.)

I think then the missing piece would be to enable specifying the content-encoding in the resources option of @nx-aws/s3:deploy.

How's that sound?

etylsarin commented 1 year ago

yup, I know about dependsOn. I just wasn't sure, how to pass files from one task to another. Your suggestion with outputs is the missing piece to my puzzle. Thank you very much!