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>}
已提问 2 年前1455 查看次数
2 回答
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.

已回答 2 年前
  • I am trying to get the data being returned by Lambda B in Lambda A.

1
已接受的回答

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
专家
Uri
已回答 2 年前
profile picture
专家
已审核 1 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则