typescriptaws-lambdaserverlessaws-samesbuild

How to add esbuild plugins in AWS SAM's template.yaml?


I am currently using the AWS Serverless Application Model (SAM) to manage my TypeScript Lambda functions, and I rely on esbuild for the build process.

I'd like to use esbuild plugins in my build process to support TypeScript Project References, to share code between multiple Lambda functions. However, I'm not sure how to include these plugins directly in the template.yaml or if it's even supported. Here's a snippet of my current template.yaml:

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Metadata: 
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: es2020
        # ... other properties

Is there a way to specify esbuild plugins directly in the SAM template? If it's not directly supported, are there any recommended workarounds or best practices for integrating esbuild plugins with SAM?


Solution

  • AFAIK, I don't think you can find any information about it since to share code among Lambdas, AWS propose to use layers. Layers can be seen as a plugin that can be attached to one or more Lambdas, allowing the code to be shared with any Lambda that has it attached.

    AWS Official layer example

    How to build layers in the YAML https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-layers.html

    Layers documentation https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html