serverless-esbuild icon indicating copy to clipboard operation
serverless-esbuild copied to clipboard

Unable to package and deploy from an s3 bucket

Open brandonin opened this issue 2 years ago • 2 comments

Describe the bug When adding the serverless-esbuild plugin, it always builds and does not take into account s3 buckets that are added as artifacts. It overrides any artifacts that are provided in favor of the location that esbuild outputs to.

https://github.com/serverless/serverless/issues/11414

To Reproduce

package:
  individually: true

plugins:
  - serverless-esbuild

provider:
  name: aws
  runtime: nodejs14.x
  region: us-west-2

functions:
  addData:
    handler: addData.main
    package:
      artifact: s3://some-bucket/add-data.zip
    events:
      - http:
          path: add-data/{code}
          method: post

Expected behavior It should deploy from an s3 artifact.

Versions (please complete the following information):

Mac OSX 12.2.1 (21D62) Framework Core: 3.22.0 (local) 3.22.0 (global) serverless-esbuild: 1.33.0 Plugin: 6.2.2 SDK: 4.3.2

brandonin avatar Sep 28 '22 23:09 brandonin

I'm experiencing the same issue. It appears that esbuild is trying to compile it but really it shouldn't be doing anything if an artifact is being provided. In my situation I've got a project that has several lambas and leverages esbuild, but I'm also adding another lambda to the project that's going to use an artifact that's published to S3. So for that lambda I don't want esbuild to do anything with it.

koryhutchison avatar Dec 21 '22 19:12 koryhutchison

So I've done some investigation and testing and this isn't just limited to S3. Basically if any function in the serverless config has an artifact defined, then the plugin throws an error because it can't find the handler. When really, if an artifact is defined, esbuild doesn't need to run at all. It's already zipped up. So this includes local artifacts as well as ones in S3.

I think I have an easy fix for this, but I want to know if you think it's acceptable or not. The problem stems from here because functions just picks up on all the functions in the config. (Except for those that aren't Node functions) So my suggested change would be to add a build option called excludeFunctions or something like that where I could use that to tell serverless-esbuild to just ignore those particular functions all together. So an example yaml would be like:

custom:
  esbuild:
    excludeFunctions:
      - functionAlias

And then the extra check would be to just check if the function has been excluded:

private isExcluded(functionAlias: string): boolean {
    return this.buildOptions.excludeFunctions.includes(functionAlias);
}

Let me know what you think @floydspace or @samchungy. I'm definitely open to other ideas, but this is the first idea I had. I'd like to contribute, but I'm not quite sure what your process is here.

I realize now that this option here might not work because everything in that area gets sent as an option to esbuild. And it won't like an excludeFunctions option getting passed in. But maybe there's something else that can be done?

koryhutchison avatar Dec 22 '22 17:12 koryhutchison