user1619524
user1619524

Reputation: 1624

aws sqs lambda: messages not re-appearing

I have followed standard aws lambda creation with sqs trigger. Then I sent message to sqs queue which then kicks lambda, which in turn writes to stdout. All good.

The issue is: I have not yet deleted receipt of the message and I expect the message to appear again for processing after the visibility period of 60 seconds(default). This is not happening, wonder why.

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, sqsEvent events.SQSEvent) error {

    for _, message := range sqsEvent.Records {
        fmt.Println("Id", message.MessageId)
        fmt.Println("Source", message.EventSource)
        fmt.Println("Body", message.Body)
    }

    return nil
}

func main() {
    lambda.Start(handler)
}

Upvotes: 0

Views: 1980

Answers (2)

Brian McCall
Brian McCall

Reputation: 1907

By default if your lambda succeeds it will automatically delete the message from the queue. If you want to keep the message in the queue, you would have to explicitly fail the lambda using the callback and an error or context.fail

Upvotes: 1

yorodm
yorodm

Reputation: 4461

Making an answer out of the conversation with the OP

Question: Op wants to check how SQS retries sending events to lambda when events fail. Op provides the code for a lambda function written in Go

Problem: Provided lambda does not fail so no retry behavior happens.

Solution: Rewrite lambda so it always fail.

    func handler(ctx context.Context, sqsEvent events.SQSEvent) error {

    for _, message := range sqsEvent.Records {
        fmt.Println("Id", message.MessageId)
        fmt.Println("Source", message.EventSource)
        fmt.Println("Body", message.Body)
    }

    return error.New("Song by B.S.")
    }

Upvotes: 1

Related Questions