Home > Blockchain >  Null body response to ALB from lambd function
Null body response to ALB from lambd function

Time:04-28

I am trying to send some logs from the AWS Lambda function to the ALB.

I am using StringIO as handler for the logger.

Below is the snippet of the code

logger.addHandler(logging.StreamHandler(sys.stdout))

response_logger = logging.getLogger("ResponseLogger")
response_str = StringIO()
response_handler = logging.StreamHandler(response_str)
response_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
response_logger.addHandler(response_handler)

def lambda_handler(event, context):
    response = {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": False,
        "headers": {
        "Content-Type": 'text/html'
        }
            }
    response['body'] = json.dumps(response_str.getvalue())
    return response

Expectation: The body should return logs in html format.

Output: body is null

CodePudding user response:

You are not returning anything from your function. It should be:

def lambda_handler(event, context):
    response = {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": False,
        "headers": {
        "Content-Type": 'text/html'
        }
            }
    response['body'] = json.dumps(response_str.getvalue())

    return response

CodePudding user response:

Make sure not to forget to return your function

just add return response at the bottom of your function

  • Related