Coder Social home page Coder Social logo

serverless-go-build's Introduction

Serverless Go Builds

Serverless License NPM

A Serverless v1.x plugin to making building Go easy!

Use your serverless.yml file as your build script, allowing specifying public functions or .go files as your entry points. Also can start other serverless plugins before running tests, and of course properly packages the built binary for upload (by default even individually packages each binary for increased performance!).

Features

  • Runs build for all go files listed as handlers (no separate build script!)
  • Specify go files or Public Functions directly (or continue to use path to binary)
    • Allows specifying packageName.FunctionName as function handler
  • Runs go tests
  • Can start serverless plugins before tests
    • eg: S3 or Dynalite test server - these can initialize resources based on yaml file
  • Individually packages each function for minimum lambda code size
    • Instead of sending one zip up with all binaries
  • Heavily customizable!
    • Doesn't even have to build Go!

Installation

npm install --save serverless-go-build

Usage

  • serverless build : Builds all Go binaries listed as function handlers
  • serverless build --function getWidget : Builds specific Go binaries
  • serverless test : Runs tests specified in serverless.yml
    • Passes in Environment variables GO_TEST=serverless and stage=testing

serverless deploy will not run the builds - run serverless build first.

Example serverless.yml

The below is a full serverless.yml example - however the only unique parts are:

  • custom.go-build - Location of custom overrides (see below)
  • package - Optionally specify individually: true for individual packaging
  • functions.{yourFunction}.handler - Specify your handler as .go file or module.PublicFunction
service: myService
plugins:
  - serverless-go-build
custom:
  go-build:
    # Example where we start "serverless-dynalite" prior to testing
    testPlugins:
      - dynalite:start
    # Run tests defined in endpoints module/folder
    tests:
      - ./endpoints
provider:
  name: aws
  runtime: go1.x
  stage: ${opt:stage, 'testing'}
package:
  individually: true
  # No need to include / exclude globally as each function
  # specifies it's include / exclude
functions:
  getWidget:
    # In this case this file must be a main package with main()
    handler: entrypoints/widget/get.go
    name: myService-${self:provider.stage}-getWidget
    events:
      - http:
          path: widget
          method: get
  postWidget:
    # In this case this file must be a main package with main()
    handler: entrypoints/widget/post.go
    name: myService-${self:provider.stage}-postWidget
    events:
      - http:
          path: widget
          method: post
  getPiece:
    # Shows how to call into a modules public function
    handler: piece.GetPiece
    name: myService-${self:provider.stage}-postWidget
    events:
      - http:
          path: widget
          method: post

Customization

You can override any of these fields inside of custom.go-build:

{
  // Prefix used for building for AWS
  awsbuildPrefix: 'GOOS=linux ',
  // Build command - followed by bin dest and input path
  buildCmd: `go build -ldflags="-s -w" -o %2 %1`,
  // Test command - followed by value in tests array below
  testCmd: `stage=testing GO_TEST=serverless go test %1`,
  // Path to store build results
  binPath: 'bin',
  // Runtime to require
  runtime: "go1.x",
  // The path to aws-lambda-go/lambda - autogenerated include in main.go
  // (needed when referring to module/PubFunction)
  pathToAWSLambda: "github.com/aws/aws-lambda-go/lambda",
  // Path to put generated main.go files (module/PubFunction)
  generatedMainPath: "generatedEntrypoints",
  // Location of go path - needed for (module/PubFunction)
  // Must point fully to the /src segment of the path
  // (By default pulls it from $GOPATH)
  goPath: undefined,
  // Pass this to minimize the package uploaded to just the binary
  // for that endpoint
  minimizePackage: true,
  // Test plugins to start before running 
  testPlugins: [],
  // Delay in milliseconds between starting plugins and starting tests
  testStartDelay: 0,
  // Array of tests to run
  tests: [],
}

Coming Soon

Will support in the future:

  • serverless test command supporting running individual test
  • Building locally vs for AWS

Important Notes

  • If you override package for a function you must include the bin file
    • We do not override package or add to it if you specify it explicitly

serverless-go-build's People

Contributors

sean9keenan avatar thinkski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

serverless-go-build's Issues

Not compatible with `serverless-offline` plugin.

No errors are thrown, but when you try to hit an endpoint it just doesn't return anything.

It looks like serverless-offline is trying to run the main.go file as though it is a compiled go binary (since the main.go file is the one set as the handler in the serverless.yml file).

This isn't exactly a problem with your plugin per se, but it is related to your plugin and a way to change the build behavior would be super helpful.

Not able to run `sls invoke local`

sls build run successfully, it creates bin folder and inside that it created hello/main.
But when I run, sls invoke local -f hello to invoke the function locally, it says

$ sls invoke local -f hello
Serverless: Reassigning go paths to point to bin
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
START RequestId: b70a5bb1-e907-1b2b-c0bc-36dc127cdcb9 Version: $LATEST
END RequestId: b70a5bb1-e907-1b2b-c0bc-36dc127cdcb9
REPORT RequestId: b70a5bb1-e907-1b2b-c0bc-36dc127cdcb9  Init Duration: 57.26 ms Duration: 3.37 ms       Billed Duration: 4 ms   Memory Size: 1024 MB    Max Memory Used: 14 MB    

{"errorType":"exitError","errorMessage":"RequestId: a52cf12f-6450-1c6e-cc47-e12f58bf98a5 Error: fork/exec /var/task/hello/main.go: no such file or directory"}

This is the code, simple enough.

plugins:
  - serverless-go-build
package:
  individually: true

functions:
  hello:
    handler: hello/main.go
    events:
      - http:
          path: hello
          method: get

Does not set the correct path on Windows

When compiling on Windows, the outputbin will be generated with path.join(binPath, outputbin) and this will change the forward slashes into back slashes. Later, the function will fail in AWS as Linux does not recognize back slashes.

I have created this patch if it helps :)

diff --git a/node_modules/serverless-go-build/index.js b/node_modules/serverless-go-build/index.js
index 0034760..03c64a2 100644
--- a/node_modules/serverless-go-build/index.js
+++ b/node_modules/serverless-go-build/index.js
@@ -161,7 +161,7 @@ class ServerlessPlugin {
   getOutputBin(func) {
       let outputbin = func.handler.replace(/\.go$/, "")
       const binPath = this.getGoConfigParam('binPath')
-      outputbin = path.join(binPath, outputbin)
+      outputbin = path.join(binPath, outputbin).replace(/\\/g, '/')
       return outputbin
   }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.