aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

Generic Build Proposal #858

Closed j closed 1 year ago

j commented 5 years ago

Putting a quick list of what was discussed in #samdev.

The builder implementation is great and sam build is exactly what we need. It seems to be missing the ability to go above and beyond typical build solutions. The builder (for node.js) as it stands expects the function code to build for the exact runtime. The issue is that most people use updated versions of node.js (or typescript) and deploy to older node.js runtimes that Lambda supports, as well as a majority of people need more advanced building. Popular tools like https://github.com/zeit/ncc, https://webpack.js.org/, gulp, browserify, babel, etc, etc, help build stuff that can compile down to AWS Lambda specific versions, perform minification, optimize images, etc, etc. It would be great if anyone who uses sam can also build using the tools they wish. We personally use serverless with functions that utilize webpack (react apps), and typescript (tsc -b).

I'm proposing a more generic builder solution for anyone to implement their own build functionality that other Lambda deployment tools have.

Apex Up (https://up.docs.apex.sh/)

Up has an .upignore and up.json file. The up.json file can contain custom build commands:

{
  "name": "app",
  "hooks": {
    "build": "browserify --node app.js > server.js",
    "clean": "rm server.js"
  }
}

Serverless

There are tons of build plugins for serverless (typescript, webpack, browserify, etc, etc)

Sam Proposal

It'd be awesome if we can take from what's been around for a while and that people are used to and implement it. Automated builds is awesome, but not flexible. If each lambda function could contain their own build directions, that'd make it more flexible.

template.yaml

Resources:
    FunctionOne:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: one/
            Build:
                PreBuild: "npm install"
                Build: " npm run build"
                PostBuild: "npm install --production"
                Exclude:
                    - src/**
                    - node_modules/.bin/**
                    - node_modules/foo/.bin/bar
            Handler: one.handler
            Runtime: nodejs8.10
            Events:
                One:
                    Type: Api
                    Properties:
                        Path: /one
                        Method: get
    FunctionTwo:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: two/
            Build:
                PreBuild: "npm install"
                Build: " npm run build"
                PostBuild: "npm install --production"
            Handler: two.handler
            Runtime: nodejs8.10
            Events:
                Two:
                    Type: Api
                    Properties:
                        Path: /two
                        Method: get

OR sam.json & .samignore files contained in the function directory (not preferred)

{
  "scripts": {
    "prebuild": "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm install",
    "build": "tsc -b",
    "postbuild": "npm install --production"
  }
}

Example of excluding everything and cherry picking what's needed.. you should be able to do the opposite of this too.

**
!dist/**
!node_modules/**
!.env
!shared/**
node_modules/some-package/.bin/some-large-unneeded-file

You can go even further and look at scripts.build inside of package.json and do this in the NPM builder, but I think keeping it simple would be best.

I'm more in favor with keeping everything inside of template.yaml if possible.

whiteshadoww commented 4 years ago

I will switch from SAM to Serverless if this is not returned to the backlog

awsjeffg commented 4 years ago

@whiteshadoww as you wish! Although Serverless is also a fine tool and if its features fit your needs better, you should consider it.

It has been quite a while since this issue was created and in the meantime the team added support for using makefiles in sam build, allowing for various kinds of build customization:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html

wchengru commented 2 years ago

Thanks for this suggestion! We will consider improving our build options to include the feature you mentioned.

praneetap commented 2 years ago

Hello! Just so commenters are aware we have esbuild support in preview, which might solve some of the original commenter's reported issues. To find out more and leave feedback - https://github.com/aws/aws-sam-cli/issues/3700 Blog post here - https://aws.amazon.com/blogs/compute/building-typescript-projects-with-aws-sam-cli/

jfuss commented 1 year ago

Our generic builder is Makefile. There might be more things we can do here but in terms of the initial request we solved it through the Makefile builder.

Closing

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.