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ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ