0

Lets say I have two lambdas: A & B. A is listening to an SQS.

I want the flow to be as followed:

  1. A message arrived to the SQS.
  2. Lambda A pulls the message and starts working on it.
  3. After lambda A started, lambda B also takes the message and does other things. But lambda B must start after A has already started.

I know I can use lambda A to put the message on another SQS, and then lambda B will listen to the new SQS. But I was wondering if there is a better way to run the lambdas one after another.

Thanks

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Gal
  • 1
  • 2
    you can make your worker lambda (ie lambda A) invoke lambda B. if you want to decouple it, make your lambda A publish to an SNS topic and have lambda B subscribe to it. – LostJon Dec 10 '19 at 15:20

3 Answers3

2

You can invoke lambda B directly from lambda A with Lambda SDK.

You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event. For example:

import boto3

client = boto3.client('lambda')
response = client.invoke(
  FunctionName='lambda-b',
  InvocationType='Event',
  LogType='Tail',
  ClientContext='string',
  Payload=file,
  Qualifier='string'
)

Documentation for Python SDK (boto3) here

anton.uspehov
  • 332
  • 3
  • 9
0

You can use the Step Functions service. It will help you to coordinate different AWS services (Lambda, SQS, DynamoDb, ...) into a serverless workflow.

This is a nice Tutorial on how to use them: AWS Step Functions — A User’s Guide

AWS Step Functions Developer Guides is the offical docs that will show you all the things that you could do with Step Functions.

alberto vielma
  • 2,302
  • 2
  • 8
  • 15
  • only issue here is that you would need to have a lambda that was triggered by SQS to StartExecution for a step function. So, one more compute needs to be added to make this work, but I agree that ANY orchestration of lambda should be used w/ step functions. – LostJon Dec 10 '19 at 16:27
  • You could trigger your step function from whenever you want using the [Step Functions SDK](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/StepFunctions.html). That could solve your issue depending in the serverless architecture you design. Let me know if works for you – alberto vielma Dec 10 '19 at 16:30
  • yea...to run that, you need some compute instance (being a lambda trigger). You cannot start a step function as a trigger from SQS – LostJon Dec 10 '19 at 16:33
  • Oh ok I get it better now. [Here](https://stackoverflow.com/questions/53422296/aws-sqs-trigger-step-functions), it was discussed the subject about triggering a step function using SQS. It seems it is not directly possible – alberto vielma Dec 10 '19 at 16:40
0

You can invoke Lambda B directly from Lambda A.

here is a code for python

from boto3 import client as boto3_client
from datetime import datetime
import json

lambda_client = boto3_client('lambda', region_name='eu-west-1')

def lambda_handler(event, context):
    response = lambda_client.invoke(FunctionName="lambda_b",
                                           InvocationType='Event',
                                           Payload=json.dumps(event))
    print(response)

Please remember: you need to edit your role to allow a lambda invocation:

   {
        "Sid": "St125642120",
        "Effect": "Allow",
        "Action": [
            "lambda:InvokeFunction"
        ],
        "Resource": "*"
    }