How to get response value from function B to function A

0

I am invoking a lambda function (Function B) in another lambda function (Function A). as you can see in the code: Function A

import json
import boto3
client = boto3.client('lambda')

def lambda_handler(event, context):
    ab = {
        'a': 'A',
        'b': 'B'
        }
    
    Response = client.invoke(
        FunctionName = 'arn:aws:lambda:us-west-2:747966+1193930:function:toInvoke',
        InvocationType = 'RequestResponse',
        Payload = json.dumps(ab)
        )
    print(Response)

Function B

import json

def lambda_handler(event, context):
    a = "I am Rehan"
    print(a)
    return a

I want to get the value of the function B in Response but it is giving me response like this:

{'ResponseMetadata': {'RequestId': 'f70db108-a9f44747474740-345367b39c53', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Tue, 31 May 2022 06:55:20 GMT', 'content-type': 'application/json', 'content-length': '12', 'connection': 'keep-alive', 'x-amzn-requestid': 'f70db108-a9f4-36363636363667b39c53', 'x-amzn-remapped-content-length': '0', 'x-amz-executed-version': '$LATEST', 'x-amzn-trace-id': 'root=1-6295bbd8-7e71eebd66666666655;sampled=0'}, 'RetryAttempts': 0}, 'StatusCode': 200, 'ExecutedVersion': '$LATEST', 'Payload': <botocore.response.StreamingBody object at 0x7f355556ed430>}
demandé il y a 2 ans1453 vues
2 réponses
2

What you are trying to do is to "tightly couple" two lambda functions together.

I am not sure what business requirements you have, but, in general, this could be called an "anti-pattern". The approach to use with "modern applications" is to use "loose coupling". https://docs.aws.amazon.com/wellarchitected/latest/framework/a-workload-architecture.html

For example, you might want to send messages between Lambda functions using SNS (publish/subscribe) or SQS (message queues).

On the other hand, if you are trying to put together some over-arching workflow that controls the execution of multiple business functions, then have a look at Step Functions as a way of orchestrating multiple actions: https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html

Hope this helps.

répondu il y a 2 ans
  • I am trying to get the data being returned by Lambda B in Lambda A.

1
Réponse acceptée

The response is returned in the Payload field. As you can see, the response type is botocore.response.StreamingBody. You should call the read() method on the Payload to get the content, e.g., body = Response['Payload'].read()

profile pictureAWS
EXPERT
Uri
répondu il y a 2 ans
profile picture
EXPERT
vérifié il y a un mois

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions