aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Support BuildProperties/BuildMethod config for multiple functions

Open iRoachie opened this issue 3 years ago • 9 comments

Describe your idea/feature/enhancement

Hey folks, super pumped about the new typescript + esbuild feature. One annoying thing I noticed is that for every function resource, I have to specify a metadata field with the esbuild options. You can imagine how repetitive this can get with multiple functions.

e.g.

Metadata: # Manage esbuild properties
  BuildMethod: esbuild
  BuildProperties:
    Minify: true
    Target: 'es2020'
    Sourcemap: true
    EntryPoints:
      - index.ts

Proposal

Would be awesome if we could somehow add this to the Globals section of the sam template. e.g If I wanted to use esbuild for all my functions.

The same would be great to have for LayerVersion.

iRoachie avatar Mar 06 '22 14:03 iRoachie

@iRoachie Thanks for the feature request. Globals happens as apart of the SAM Transform, so I don't think we want to support this locally or in SAM.

However, I do see a need for something like this and what I think you are asking for is to be able to set build args for all your buildable resources similar to how you could do this by using esbuild or other tooling (either in the command line or rc files.). I am not sure the best way to support this though but I do think it is a good improvement.

If the intention is correct above, I will flag this for our PM to review.

jfuss avatar Mar 07 '22 17:03 jfuss

I wasn't sure how it could be done, but yes the intent is correct.

iRoachie avatar Mar 07 '22 18:03 iRoachie

Agree, the commonality is needed to be placed in the Globals > Function section, with the exception for EntryPoints. I also believe EntryPoints should THEN be more easily configured inside the Resources portion, because Metadata > BuildProperties > EntryPoints > app.ts is just too lengthy.

Pheru1404 avatar Mar 17 '22 09:03 Pheru1404

Thanks for your feedback.

Since esbuild support is still beta, we will work with our PM to find an alternative way, which should resolve this issue.

mndeveci avatar Apr 13 '22 04:04 mndeveci

Any news on this one? It is stopper for us when it comes to using esbuild.

stagr avatar Feb 27 '23 11:02 stagr

One workaround is to use yaml anchor merges (name something, then reuse), e.g.

x-anchors:
  Metadata: &esb_meta
    BuildMethod: esbuild
  BuildProperties: &esb_props
    Minify: true
    Target: es2020
    Sourcemap: true
    Packages: external

Resources:

  FunctionAlpha:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: func-alpha
      Handler: handler.lambdaHandler
    Metadata:
      <<: *esb_meta
      BuildProperties:
        <<: *esb_props
        EntryPoints: 
          - handlers/alpha/handler.ts

  FunctionBeta:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: func-beta
      Handler: handler.lambdaHandler
    Metadata:
      <<: *esb_meta
      BuildProperties:
        <<: *esb_props
        EntryPoints: 
          - handlers/beta/handler.ts

Ignore possible tooling errors saying it's not allowed. SAM CLI 1.95.0 seems to handle it just fine unless you sam validate.

x-anchors

yegorpetrov avatar Aug 24 '23 17:08 yegorpetrov

@yegorpetrov How about this? It should works on both sam validate and sam deploy

MetaData: &esbuild_props
    Minify: true
    Target: es2020
    Sourcemap: true
    Packages: external

Resources:

  FunctionAlpha:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: func-alpha
      Handler: handler.lambdaHandler
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        <<: *esbuild_props
        EntryPoints: 
          - handlers/alpha/handler.ts

ruiwei avatar Sep 19 '23 19:09 ruiwei

@yegorpetrov How about this? It should works on both sam validate and sam deploy

MetaData: &esbuild_props
    Minify: true
    Target: es2020
    Sourcemap: true
    Packages: external

Resources:

  FunctionAlpha:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: func-alpha
      Handler: handler.lambdaHandler
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        <<: *esbuild_props
        EntryPoints: 
          - handlers/alpha/handler.ts

Yes, that's what we ended up with for now.

yegorpetrov avatar Sep 19 '23 22:09 yegorpetrov

@yegorpetrov How about this? It should works on both sam validate and sam deploy

MetaData: &esbuild_props
    Minify: true
    Target: es2020
    Sourcemap: true
    Packages: external

Resources:

  FunctionAlpha:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: func-alpha
      Handler: handler.lambdaHandler
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        <<: *esbuild_props
        EntryPoints: 
          - handlers/alpha/handler.ts

Yes, that's what we ended up with for now.

Metadata, not MetaData

VasilyVP avatar Feb 23 '25 04:02 VasilyVP