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>}
asked 2 years ago1437 views
2 Answers
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.

answered 2 years ago
  • I am trying to get the data being returned by Lambda B in Lambda A.

1
Accepted Answer

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
answered 2 years ago
profile picture
EXPERT
reviewed 20 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions