subslicer

package module
v0.0.0-...-096f78b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2019 License: MIT Imports: 24 Imported by: 0

README

subslicer (local-lambda-server) - lambda runtime sandbox

GoDoc

Overview

local-lambda-server is a http server that takes local directory and serves it as a lambda handler. subslicer is a golang library to interact with native lambda runtime libraries. Both are written in pure go, without using docker-lambda or localstack.

Installation

$ go get -u github.com/dzeromsk/subslicer/...

This will make the local-lambda-server tool available in ${GOPATH}/bin, which by default means ~/go/bin.

local-lambda-server defines go1.x, python2.7 and python3.7 runtime and expects AWS Lambda chroot to be present:

mkdir -p $HOME/chroot/go1.x
curl https://lambci.s3.amazonaws.com/fs/base.tgz | sudo tar zxv -C $HOME/chroot/go1.x/
curl https://lambci.s3.amazonaws.com/fs/go1.x.tgz | sudo tar zxv -C $HOME/chroot/go1.x/
mkdir -p $HOME/chroot/python2.7
curl https://lambci.s3.amazonaws.com/fs/base.tgz | sudo tar zxv -C $HOME/chroot/python2.7/
curl https://lambci.s3.amazonaws.com/fs/python2.7.tgz | sudo tar zxv -C $HOME/chroot/python2.7/
mkdir -p $HOME/chroot/python3.7
curl https://lambci.s3.amazonaws.com/fs/base.tgz | sudo tar zxv -C $HOME/chroot/python3.7/
curl https://lambci.s3.amazonaws.com/fs/python3.7.tgz | sudo tar zxv -C $HOME/chroot/python3.7/

Usage of the binary (local-lambda-server)

local-lambda-server by default starts http server from current working directory merged with aws ami chroot, initializes native lambda runtime and invokes lambda handler in response to http requests.

Usage of local-lambda-server:
  -console string
        Console socket address (default "/tmp/console.sock")
  -debug
        Run with debug flag enabled
  -group string
        Lambda group (default "nogroup")
  -h string
        Lambda runtime handler (default "handler.my_handler")
  -http string
        HTTP address (default "127.0.0.1:9090")
  -logs string
        Logs socket address (default "/tmp/logs.sock")
  -prefix string
        Chroot dir prefix (default $HOME)
  -r string
        Lambda runtime name (default "python2.7")
  -task string
        Lambda task directory (default $CWD)
  -user string
        Lambda user (default "nobody")
  -workers int
        Max workers (default 1)

Start server and set runtime to python2.7, and handler to handler.my_handler:

sudo local-lambda-server -r python2.7 -h handler.my_handler 

root privileges are required because subslicer uses linux cgroup and seccomp filters

Invoke lambda handler:

curl http://127.0.0.1:9090/invoke

Features

  • You edit files in the task dir and server auto reloads handler.
  • Simple server for development.
  • No config files.
  • Does not require docker or anything.
  • It's reasonably fast.
  • Full abi compatibility with AWS Lambda.
  • Tres to reproduce Lambda sandbox syscall filter.
  • Freezes running handlers just like real lambda server does.

Downsides

  • Supports only single tenant/function.
  • Less features than localstack.
  • Requires root privileges because of old cgroup api

Philosophy

Sometimes you just want to serve lambda handler from local directory similar to how python -m SimpleHTTPServer works with static files. localstack and docker-lambda can do that but it may be slow and clunky. They also replace native lambda runtime with fake/mock interface, ignore lambda syscall filter and cgroup freezer. And sometimes you need all that to iterate quickly when developing lambda with low level functionality or binary libraries.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewShmem

func NewShmem(name string) (*shmem, error)

func TempUnixgramAddr

func TempUnixgramAddr(dir, pattern string) (*net.UnixAddr, error)

func UnixgramPair

func UnixgramPair() (c1, c2 *net.UnixConn, err error)

Types

type ControlConn

type ControlConn struct {
	*net.UnixConn
	// contains filtered or unexported fields
}

func (*ControlConn) Invoke

func (c *ControlConn) Invoke(ctx context.Context, args map[string]string) error

type Function

type Function struct {
	*freezer.Freezer
	Handler string
	Dir     string
	User    string
	Group   string
	// contains filtered or unexported fields
}

func NewFunction

func NewFunction(r Runtime, dir, handler string) (f *Function, err error)

func (*Function) Close

func (f *Function) Close() (err error)

func (*Function) Debug

func (f *Function) Debug() []byte

func (*Function) Invoke

func (f *Function) Invoke(ctx context.Context) error

func (*Function) Reset

func (f *Function) Reset()

func (*Function) Response

func (f *Function) Response() []byte

func (*Function) Write

func (f *Function) Write(data []byte) (int, error)

type FunctionPool

type FunctionPool struct {
	New func() (f *Function, err error)
	// contains filtered or unexported fields
}

func (*FunctionPool) Get

func (p *FunctionPool) Get() (f *Function, err error)

func (*FunctionPool) Purge

func (p *FunctionPool) Purge() error

func (*FunctionPool) Put

func (p *FunctionPool) Put(f *Function)

type Runtime

type Runtime struct {
	Name string

	ConsoleAddr *net.UnixAddr
	LogsAddr    *net.UnixAddr
	Cmd         string
	Args        []string
	User        string
	Group       string
	Chroot      string
}

type TCPServer

type TCPServer struct {
	// contains filtered or unexported fields
}

func NewTCPServer

func NewTCPServer(addr string, handler func(conn net.Conn)) (s *TCPServer, err error)

func (*TCPServer) Close

func (s *TCPServer) Close() (err error)

func (*TCPServer) Serve

func (s *TCPServer) Serve() error

type UDPServer

type UDPServer struct {
	// contains filtered or unexported fields
}

func NewUDPServer

func NewUDPServer(addr string, handler func(data []byte)) (s *UDPServer, err error)

func (*UDPServer) Close

func (s *UDPServer) Close() (err error)

func (*UDPServer) Serve

func (s *UDPServer) Serve() error

type UNIXServer

type UNIXServer struct {
	// contains filtered or unexported fields
}

func NewUNIXServer

func NewUNIXServer(addr *net.UnixAddr, handler func(conn net.Conn)) (s *UNIXServer, err error)

func (*UNIXServer) Close

func (s *UNIXServer) Close() (err error)

func (*UNIXServer) Serve

func (s *UNIXServer) Serve() error

Directories

Path Synopsis
cmd
pb
Package nsjail is a generated protocol buffer package.
Package nsjail is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL