lamblocal

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 8 Imported by: 2

README

lamblocal

Go Reference

Description

lamblocal is a library that allows you to run AWS Lambda functions handler implemented by Go locally as a CLI command.

Usage

package main

import (
	"context"

	"log/slog"

	"github.com/aws/aws-lambda-go/events"
	"github.com/fujiwara/lamblocal"
)

func myHandler(ctx context.Context, payload events.CloudWatchEvent) (string, error) {
	lamblocal.Logger.Info("hello", slog.String("ID", payload.ID))
	// do something
	return "OK", nil
}

func main() {
	lamblocal.Run(context.TODO(), myHandler)
}

lamblocal.Run() executes the provided function, but the way it's executed depends on the current environment. Specifically, if the environment variable AWS_EXECUTION_ENV starts with "AWS_Lambda", or if AWS_LAMBDA_RUNTIME_API is set, the current environment is assumed to be AWS Lambda, and fn is executed as a Lambda function. This is achieved by calling lambda.Start(fn).

In all other environments, fn is executed as a CLI (Command Line Interface) function. In this case, the payload is read from the standard input (os.Stdin) and passed to fn. If the function returns an error, the error message is logged, and the program exits with an error code of 1.

lamblocal.CLISrc

lamblocal.CLISrc is a source of the payload for the CLI execution mode. You can set the payload as a io.Reader instead of os.Stdin.

lamblocal.CLISrc = strings.NewReader(`{"id": "123"}`)
lamblocal.Run(context.TODO(), myHandler) // will call myHandler with events.{"id": "123"}
Logger

lamblocal.Logger is a logger that outputs to stderr as JSON format, using slog.

Limitation

Supports handler function interface func (context.Context, T) (U, error) only.

aws-lambda-go supports other handler function interface func () and func (ctx context.Context), and etc. but lamblocal does not support them.

T and U are allowed generic types that can be marshaled by json.Marshal and json.Unmarshal.

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CLISrc io.Reader

Functions

func Run

func Run[T any, U any](ctx context.Context, fn func(context.Context, T) (U, error))

Run runs a lambda handler func detect the environment (lambda or not) and run it.

func RunCLI

func RunCLI[T any, U any](ctx context.Context, src io.Reader, fn func(context.Context, T) (U, error)) (U, error)

RunCLI is a helper function for running a lambda handler func on CLI.

func RunWithError added in v0.0.4

func RunWithError[T any, U any](ctx context.Context, fn func(context.Context, T) (U, error)) error

RunWithError runs a lambda handler func detect the environment (lambda or not) and run it.

Types

This section is empty.

Directories

Path Synopsis
examples
kong Module

Jump to

Keyboard shortcuts

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