-1

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

error404
  • 2,684
  • 2
  • 13
  • 21

2 Answers2

0

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
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I did add the return response forgot to include that in the code. The main issue is that the ```response_str.getvalue()``` is returning null but on local it works fine – error404 Apr 28 '22 at 07:12
0

Make sure not to forget to return your function

just add return response at the bottom of your function

fier
  • 74
  • 7