grat

package module
v0.0.0-...-6c4c03a Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 16 Imported by: ? 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

grat

Integration of AWS Lambda and SQS Polling Server

Example

grat provides transparent development with local and AWS Lambda runtime

package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/aws/aws-lambda-go/events"
	"github.com/mashiike/grat"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
	defer cancel()

	if err := grat.RunWithContext(ctx, "grat-hello", 10, handler); err != nil {
		log.Fatalln("[error]", err)
	}
	log.Println("[info] shutdown complate")
}

func handler(ctx context.Context, event *events.SQSEvent) error {
	log.Printf("[info] %d messages received", len(event.Records))
	encoder := json.NewEncoder(os.Stdout)
	encoder.SetIndent("", "  ")
	if err := encoder.Encode(event); err != nil {
		return err
	}
	return nil 
}
  1. Create IAM role "grat" for Lambda which have attached policy AWSLambdaSQSQueueExecutionRole.
  2. Install lambroll.
  3. Place main.go to example/.
  4. Run make deploy to deploy a lambda function.
  5. Create SQS queue grat-hello and set event source mapping.
grat.RunWithContext(ctx, queue, batchSize, handler)

grat.RunWithContext(ctx, queue, batchSize, handler) works as below.

  • If a process is running on Lambda (AWS_EXECUTION_ENV or AWS_LAMBDA_RUNTIME_API environment variable defined),
    • Call lambda.StartWithOptions()
  • Otherwise start a sqs polling server using queue and batchSize.

LICENSE

The MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxDeleteRetry = 8

Functions

func Run

func Run(queue string, batchSize int, handler interface{}, options ...lambda.Option) error

Run runs lambda handler on AWS Lambda runtime or sqs polling server.

func RunWithContext

func RunWithContext(ctx context.Context, queue string, batchSize int, handler interface{}, options ...lambda.Option) error

RunWithContext runs lambda handler on AWS Lambda runtime or sqs polling server with context.

Types

type BatchItemFailureItem

type BatchItemFailureItem struct {
	ItemIdentifier string `json:"itemIdentifier"`
}

type BatchItemFailureResponse

type BatchItemFailureResponse struct {
	BatchItemFailures []BatchItemFailureItem `json:"batchItemFailures,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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