Skip to content

go-nacelle/lambdabase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Abstract AWS Lambda server process for nacelle.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published