invoker

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2021 License: Apache-2.0 Imports: 7 Imported by: 1

README

lambda-invoker

Lightweight library to simplify invoking a lambda function.

Usage

Initialze an Invoker, passing it a lambda.Service and the arn of the lambda function being invoked. Then invoke the function passing it your json request.

invoker := New(svc, "function-arn")
rsp, err := invoker.Invoke(ctx, []byte(`{"request":"content"}`))
// Do something with output.
Router

It's likely you'll want to use the invoker with 'edstell/lambda-router'; an Option has been included with this package to make this easy. Initialize a new Invoker, passing it the 'AsProcedure' option. AsProcedure takes a procedure name which instructs the invoked function which handler to route this request to, and an 'unmarshalError' func - which will be used when unmarshaling a returned error. This allows you to define your own custom error implementations and pass them between lambda functions. You'll need to initialize a new Invoker for each procedure you intend to call.

invoker := New(svc, "function-arn", AsProcedure("On", unmarshalErrorFunc))
rsp, err := invoker.Invoke(ctx, []byte(`{"request":"content"}`))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	StatusCode int64
	// contains filtered or unexported fields
}

Error wraps an error message with a status code.

type Invoker

type Invoker struct {
	MutateInput  func(*lambda.InvokeInput) error
	MutateOutput func(*lambda.InvokeOutput) error
	// contains filtered or unexported fields
}

Invoker is a wrapper around the aws lambda invoker implementation. It provides a convenient layer for middleware, as well as exposing a simpler method to invoke a lambda function with.

func New

func New(li LambdaInvoker, arn string, opts ...Option) *Invoker

New initializes an Invoker with the options passed.

func (*Invoker) Invoke

func (i *Invoker) Invoke(ctx context.Context, body json.RawMessage) (json.RawMessage, error)

Invoke _invokes_ the lambda function passing body as the InvokeInput.Payload and returning the InvokeOutput.Payload as the result. If InvokeOutput contains a FunctionError an Error is returned, wrapping the status code. By default lambda functions are invoked as a 'RequestResponse', but input mutators can be passed to change the InvocationType.

type LambdaInvoker

type LambdaInvoker interface {
	InvokeWithContext(context.Context, *lambda.InvokeInput, ...awsreq.Option) (*lambda.InvokeOutput, error)
}

LambdaInvoker abstracts the logic of invoking a lambda function behind an interface, this is to allow mocking the aws Lambda implementation.

type Option

type Option func(*Invoker)

Option implementations can mutate the Invoker allowing configuration of how invokations of a lambda function should be performed.

func AsProcedure

func AsProcedure(procedure string, unmarshalError func(json.RawMessage) error) Option

AsProcedure configures invocation to wrap payloads in router.Request/Response setting the Procedure field, and unmarshaling returned errors with the unmarshaler.

Jump to

Keyboard shortcuts

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