Jason Michael
Jason Michael

Reputation: 569

can a lambda invoke another lambda, and then exit before child lambda completes?

I have two AWS lambdas written in go. One lambda invokes the other like this:

payload, err := json.Marshal(request)
if err != nil {
    log.Printf("ERROR: could not marshal request [%v] into model.ChildLambdaRequest - %v\n", request, err)
    return false
}

log.Printf("--- debug sending payload: %s", payload)

// Invoke Child

result, err := client.Invoke(&lambda.InvokeInput{
    FunctionName: aws.String(lambdaName),
    Payload:      payload,
})
if err != nil {
    log.Printf("ERROR: could not invoke Lambda function client [%v] - %v\n", lambdaName, err)
    return false
}

The child lambda completes like this:

return model.EventResponse{Success: true}, nil

I know this is doing two things: 1.) it's finishing the execution and 2.) it's returning a value. Is there any way to separate these two actions, so that I can return a value at the top, but complete execution at a later time?

Upvotes: 1

Views: 915

Answers (2)

Dani G
Dani G

Reputation: 1242

It's possible with Destinations in Lambda.

Although I would check out AWS Step Functions, which gives you this functionality together with built-in retries and error handling. It works with existing Lambda functions and has a pretty easy to use interface.

Last option that could work, you send a message from the first function to an SQS Queue, and your second lambda is triggered by the SQS Queue. This also gives you retry capabilities.

Upvotes: 0

Mark B
Mark B

Reputation: 200562

Yes, you need to pass InvocationType: "Event" to the Invoke call, documented here. The default invocation type is RequestResponse which waits for the response from the invoked Lambda.

Upvotes: 2

Related Questions