lambdabase

package module
v0.0.0-...-07febc6 Latest Latest
Warning

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

Go to latest
Published: May 1, 2023 License: MIT Imports: 14 Imported by: 0

README

Nacelle Base AWS Lambda Process

PkgGoDev Build status Latest release

Abstract AWS Lambda server process for nacelle.


This library supplies an abstract AWS Lambda RPC server process whose behavior can be be configured by implementing a Handler interface. This interface wraps the handler defined by aws-lambda-go.

This library comes with an example project that logs values received from a Kinesis stream.

Process

A Lambda server process is created by supplying a handler, described below, that controls its behavior.

server := lambdabase.NewServer(NewHandler(), options...)
Event Sources

This library also supplies several additional abstract server processes that respond to specific Lambda event sources. These servers require a more specific handler interface invoked with unmarshalled request data and additional log context.

NewDynamoDBEventServer
NewDynamoDBEventServer invokes the backing handler with a list of DynamoDBEventRecords.
NewDynamoDBRecordServer
NewDynamoDBRecordServer invokes the backing handler once for each DynamoDBEventRecord in the batch.
NewKinesisEventServer
NewKinesisEventServer invokes the backing handler with a list of KinesisEventRecords.
NewKinesisRecordServer
NewKinesisRecordServer invokes the backing handler once for each KinesisEventRecord in the batch.
NewSQSEventServer
NewSQSEventServer invokes the backing handler with a list of SQSMessages.
NewSQSRecordServer
NewSQSRecordServer invokes the backing handler once for each SQSMessage in the batch.
Handler

A handler is a struct with an Init and a Handle method. The initialization method, like the process that runs it, that takes a config object as a parameter. The handle method of the base server takes a context object and the request payload as parameters and returns the response payload and an error value. The handle method of an event-specific server takes a context object, the request payload, and a logger populated with request and event identifiers as parameters and returns an error value. Return an error from either method signals a fatal error to the process that runs it.

The following example implements a handler that transforms a request value to upper-case.

type Handler struct {}

type ReqResp struct {
    Value string `json:"text"`
}

func (h *Handler) Init(config nacelle.Config) error {
    return nil
}

func (h *Handler) (ctx context.Context, payload []byte) ([]byte, error) {
    request := &ReqResp{}
    if err := json.Unmarshal(payload, &request); err != nil {
        return nil, err
    }

    return json.Marshal(&ReqResp{Value: strings.ToUpper(request.Value)})
}
Configuration

The default process behavior can be configured by the following environment variables.

Environment Variable Required Description
_LAMBDA_SERVER_PORT yes The port on which to listen for RPC commands.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

Types

type Config

type Config struct {
	LambdaServerPort int `env:"_lambda_server_port" required:"true"`
}

type DynamoDBEventHandler

type DynamoDBEventHandler interface {
	Handle(ctx context.Context, batch []events.DynamoDBEventRecord, logger nacelle.Logger) error
}

type DynamoDBRecordHandler

type DynamoDBRecordHandler interface {
	Handle(ctx context.Context, record events.DynamoDBEventRecord, logger nacelle.Logger) error
}

type Handler

type Handler interface {
	nacelle.Initializer
	lambda.Handler
}

type KinesisEventHandler

type KinesisEventHandler interface {
	Handle(ctx context.Context, batch []events.KinesisEventRecord, logger nacelle.Logger) error
}

type KinesisRecordHandler

type KinesisRecordHandler interface {
	Handle(ctx context.Context, record events.KinesisEventRecord, logger nacelle.Logger) error
}

type LambdaHandlerFunc

type LambdaHandlerFunc func(ctx context.Context, payload []byte) ([]byte, error)

func (LambdaHandlerFunc) Invoke

func (f LambdaHandlerFunc) Invoke(ctx context.Context, payload []byte) ([]byte, error)

type SQSEventHandler

type SQSEventHandler interface {
	Handle(ctx context.Context, batch []events.SQSMessage, logger nacelle.Logger) error
}

type SQSMessageHandler

type SQSMessageHandler interface {
	Handle(ctx context.Context, message events.SQSMessage, logger nacelle.Logger) error
}

type Server

type Server struct {
	Config   *nacelle.Config           `service:"config"`
	Logger   nacelle.Logger            `service:"logger"`
	Services *nacelle.ServiceContainer `service:"services"`
	Health   *nacelle.Health           `service:"health"`
	// contains filtered or unexported fields
}

func NewDynamoDBEventServer

func NewDynamoDBEventServer(handler DynamoDBEventHandler) *Server

func NewDynamoDBRecordServer

func NewDynamoDBRecordServer(handler DynamoDBRecordHandler) *Server

func NewKinesisEventServer

func NewKinesisEventServer(handler KinesisEventHandler) *Server

func NewKinesisRecordServer

func NewKinesisRecordServer(handler KinesisRecordHandler) *Server

func NewSQSEventServer

func NewSQSEventServer(handler SQSEventHandler) *Server

func NewSQSRecordServer

func NewSQSRecordServer(handler SQSMessageHandler) *Server

func NewServer

func NewServer(handler Handler) *Server

func (*Server) Init

func (s *Server) Init(ctx context.Context) error

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Jump to

Keyboard shortcuts

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