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
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠