Code Monkey home page Code Monkey logo

serverless-plugin-go's Introduction

⚡️Serverless Framework Go Plugin

npm

serverless-plugin-go is a Serverless Framework plugin that compiles Go functions on the fly. You don't need to do it manually before serverless deploy. Once the plugin is installed it will happen automatically.

Features

  • Concurrent compilation happens across all CPU cores.
  • Support for both serverless deploy and serverless deploy function commands.
  • Support for serverless invoke local command.
  • Additional command serverless go build.

Install

  1. Install the plugin

    npm i --save-dev serverless-plugin-go
  2. Add it to your serverless.yaml

    plugins:
      - 'serverless-plugin-go'
  3. Replace every Go function's handler with *.go file path or a package path. E.g.

    functions:
      example:
        runtime: 'provided.al2'
        handler: 'functions/example/main.go' # or just functions/example

Configuration

Default values:

custom:
  go:
    # folder where go.mod file lives, if set `handler` property should be set relatively to that folder
    baseDir: '.'

    # Final target destination of the binary outputs
    binDir: '.bin'

    # Compile command that should be used to build binaries
    cmd: 'go build -ldflags="-s -w"'

    # If enabled, builds function every directory (useful for monorepo where go.mod is managed by each function
    monorepo: false

    # Number of concurrent process used to compile functions, you can fine tune this number to your needs.
    # Alternatively you can use the env var `SP_GO_CONCURRENCY` to override this configuration
    concurrency: 5

    # Environment variables used to compile binaries
    env:
      GOOS: 'linux' # Default compile OS
      CGO_ENABLED: '0' # By default CGO is disabled

How does it work?

The plugin compiles every Go function defined in serverless.yaml into .bin directory. After that it internally changes handler so that the Serverless Framework will deploy the compiled file not the source file.

For every matched function it also overrides package parameter to the next config:

individually: true
artifact: <path to the generated zip with go binary>

How to run Golang Lambda on ARM?

Change architecture field from provider or function configuration to arm64:

provider:
  architecture: 'arm64'
  runtime: 'provided.al2'

# If you define architecture at function level, this will have preference instead the provider configuration

functions:
  test:
    architecture: 'arm64'
    handler: test/main.go

Caveats

This implementation doesn't allow to add any other files to the lambda artifact apart from the binary. To address this problem you should use Lambda Layers to allow your lambdas to hold external files.

Credits

This repo is forked from serverless-go-plugin created by mthenw and modified to fix some issues and reach the new AWS standards (go1.x provider deprecation).

serverless-plugin-go's People

Contributors

danteay avatar draftea-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

serverless-plugin-go's Issues

go build command failed

Hi,

Thank you for forking of https://github.com/mthenw/serverless-go-plugin.

Continuing from issue mthenw/serverless-go-plugin#41, I managed to install your package. However, there was this error (see below) when I ran sls deploy. And, I think the command I set on serverless.yml was correct, cmd: 'go build -ldflags="-s -w"'.

Error log:

Error:
Error: error compiling function (cwd: .)
    at Go.execCompilation (file:///Users/leonardy/go/src/anapp/node_modules/serverless-plugin-go/index.js:194:13)
    at async Go.compile (file:///Users/leonardy/go/src/anapp/node_modules/serverless-plugin-go/index.js:146:5)
    at async Promise.all (index 0)
    at async Go.compileFunctions (file:///Users/leonardy/go/src/anapp/node_modules/serverless-plugin-go/index.js:87:5)
    at async PluginManager.runHooks (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:530:9)
    at async PluginManager.invoke (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:563:9)
    at async PluginManager.spawn (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:585:5)
    at async before:deploy:deploy (/opt/homebrew/lib/node_modules/serverless/lib/plugins/deploy.js:48:11)
    at async PluginManager.runHooks (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:530:9)
    at async PluginManager.invoke (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:563:9)
    at async PluginManager.run (/opt/homebrew/lib/node_modules/serverless/lib/classes/plugin-manager.js:604:7)
    at async Serverless.run (/opt/homebrew/lib/node_modules/serverless/lib/serverless.js:179:5)
    at async /opt/homebrew/lib/node_modules/serverless/scripts/serverless.js:819:9
GoPlugin: Error compiling function (cwd: .): Command failed: go build -ldflags="-s -w" -o .bin/receive receive/main.go
/bin/sh: go: command not found

Here is the content of my serverless.yml:

service: anapp

frameworkVersion: '3'

provider:
  name: aws
  runtime: provided.al2
  architecture: arm64

  stage: ${opt:stage, 'dev'}
  region: us-east-1
  environment: ${file(.env.${self:provider.stage}.yml)}

package:
  patterns:
    - '!./**'
    - './bin/**'

functions:
  receive:
    handler: receive/main.go
    events:
      - http:
          path: ${self:custom.functionPath.${self:provider.stage}}/receive
          method: post
    timeout: 30
    memorySize: 128
  verify:
    handler: verify/main.go
    events:
      - http:
          path: ${self:custom.functionPath.${self:provider.stage}}/receive
          method: get
    timeout: 30
    memorySize: 128

custom:
  go:
    baseDir: '.'
    binDir: '.bin'
    cmd: 'go build -ldflags="-s -w"'
    monorepo: false
    env:
      GOOS: 'linux'
      CGO_ENABLED: '0'

plugins:
  - serverless-plugin-go

Really appreciate your assistance, thanks.

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.