Serverless Go Plugin

2.4.1 · active · verified Tue Apr 21

This Serverless Framework plugin, currently at version 2.4.1, automates the compilation of Go functions for AWS Lambda deployments. It integrates directly with Serverless Framework commands like `deploy`, `deploy function`, and `invoke local`, compiling Go source code on the fly before packaging. Key differentiators include concurrent compilation across all CPU cores for efficiency, support for various Go runtimes including `go1.x` and `provided.al2` (with bootstrap), and features for monorepo-like project structures. The plugin manages handler path transformations and package exclusions/inclusions, simplifying the deployment workflow for Go-based serverless applications. Releases generally include minor feature enhancements, dependency updates, and bug fixes, maintaining an active development cadence. It specifically requires Serverless Framework version 1.52 or above to function.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a Serverless project with `serverless-go-plugin` to deploy Go functions. It includes a `package.json` for dependencies, a `serverless.yaml` configured for both standard `go1.x` and `provided.al2` (ARM64) runtimes, and two example Go Lambda functions (`hello` and `greet`). The `greet` function illustrates using path parameters and environment variables.

{
  "name": "my-go-serverless-app",
  "version": "1.0.0",
  "description": "A serverless Go application with serverless-go-plugin",
  "main": "handler.js",
  "scripts": {
    "deploy": "serverless deploy"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "serverless": "^3.0.0",
    "serverless-go-plugin": "^2.0.0",
    "@aws-cdk/aws-lambda": "^1.0.0"
  }
}

// serverless.yaml
service: my-go-app

frameworkVersion: '3'

provider:
  name: aws
  runtime: go1.x # Default runtime, can be overridden per function
  region: us-east-1
  architecture: x86_64 # Default architecture

plugins:
  - serverless-go-plugin

custom:
  go:
    binDir: .bin # Target folder for compiled binaries
    cmd: 'GOOS=linux go build -ldflags="-s -w"' # Default compile command
    supportedRuntimes: ["go1.x", "provided.al2"]

functions:
  hello:
    handler: functions/hello/main.go # Path to the Go source file
    events:
      - httpApi:
          path: /hello
          method: get
  greetArm:
    runtime: provided.al2 # Example for a bootstrapped runtime
    handler: functions/greet # Package path (assuming main.go inside)
    architecture: arm64
    environment:
      MESSAGE: "Hello from ARM64 Go Lambda!"
    events:
      - httpApi:
          path: /greet/{name}
          method: get


// functions/hello/main.go
package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

type Response events.APIGatewayProxyResponse

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {
	body := fmt.Sprintf("Hello from Go Lambda on x86_64! Path: %s", request.Path)
	resp := Response{
		StatusCode:      200,
		IsBase64Encoded: false,
		Body:            body,
		Headers: map[string]string{
			"Content-Type": "application/json"
		},
	}
	return resp, nil
}

func main() {
	lambda.Start(Handler)
}

// functions/greet/main.go
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func GreetHandler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	name := request.PathParameters["name"]
	if name == "" {
		name = "stranger"
	}
	messageEnv := os.Getenv("MESSAGE")
	message := fmt.Sprintf("%s Greetings, %s!", messageEnv, name)

	return events.APIGatewayProxyResponse{
		StatusCode: http.StatusOK,
		Headers:    map[string]string{"Content-Type": "text/plain"},
		Body:       message,
	},
	nil
}

func main() {
	lambda.Start(GreetHandler)
}

view raw JSON →