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.
A Lambda server process is created by supplying a handler, described below, that controls its behavior.
server := lambdabase.NewServer(NewHandler(), options...)
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.
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)})
}
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. |