go-nacelle / lambdabase

Abstract AWS Lambda server process for nacelle.

Home Page:https://nacelle.dev/docs/base-processes/lambdabase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

https://nacelle.dev/docs/base-processes/lambdabase

License:MIT License


Languages

Language:Go 96.8%Language:Shell 3.2%