lambdastub

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

lambdastub

Documentation GitHub go.mod Go version (branch) GitHub tag (latest SemVer) Github Actions test License

Lambda Stub API Server for github.com/aws/aws-lambda-go/lambda

Usage

func main() {
	handler := func() (interface{}, error) {
		return "hello world!?", nil
	}

	mux, err := lambdastub.New(
		lambdastub.WithInvokeEndpoint(map[string]interface{}{
			"HelloWorldFunction": handler,
		}),
	)
    if err != nil {
        log.Fatal(err)
    }
    http.ListenAndServe(":8080", mux)
}
$ go run main.go 
$ aws lambda --endpoint http://127.0.0.1:8080 invoke --function-name HelloWorldFunction

LICENSE

MIT License

Copyright (c) 2022 IKEDA Masashi

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"net/http/httptest"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/credentials"
	"github.com/aws/aws-sdk-go-v2/service/lambda"
	"github.com/mashiike/lambdastub"
)

func main() {
	handler := func() (interface{}, error) {
		return "hello world!?", nil
	}

	mux, err := lambdastub.New(
		lambdastub.WithInvokeEndpoint(map[string]interface{}{
			"HelloWorldFunction": handler,
		}),
	)
	if err != nil {
		fmt.Println("error: ", err)
	}
	server := httptest.NewServer(mux)
	defer server.Close()
	client := lambda.New(
		lambda.Options{
			Credentials: credentials.NewStaticCredentialsProvider("ACCESS_KEY_ID", "SECRET_KEY", "TOKEN"),
			EndpointResolver: lambda.EndpointResolverFunc(func(region string, options lambda.EndpointResolverOptions) (aws.Endpoint, error) {
				return aws.Endpoint{
					URL:               server.URL,
					PartitionID:       "aws",
					SigningRegion:     "us-west-1",
					HostnameImmutable: true,
				}, nil
			}),
		},
	)
	output, err := client.Invoke(context.Background(), &lambda.InvokeInput{
		FunctionName: aws.String("HelloWorldFunction"),
		Payload:      nil,
	})
	if err != nil {
		fmt.Println("error: ", err)
	}
	fmt.Println(string(output.Payload))
}
Output:
"hello world!?"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithInvokeEndpoint

func WithInvokeEndpoint(handlerFuncByFunctionName map[string]interface{}) func(*StubOptions) error

Types

type Endpoint

type Endpoint interface {
	Register(r *mux.Router) error
	http.Handler
}

type InvokeEndpoint

type InvokeEndpoint struct {
	// contains filtered or unexported fields
}

https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html

func (*InvokeEndpoint) Register

func (e *InvokeEndpoint) Register(r *mux.Router) error

func (*InvokeEndpoint) ServeHTTP

func (e *InvokeEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Stub

type Stub struct {
	// contains filtered or unexported fields
}

func New

func New(optFns ...func(*StubOptions) error) (*Stub, error)

func (*Stub) ServeHTTP

func (stub *Stub) ServeHTTP(w http.ResponseWriter, r *http.Request)

type StubOptions

type StubOptions struct {
	Endpoints map[string]Endpoint
}

Jump to

Keyboard shortcuts

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