dashboard icon indicating copy to clipboard operation
dashboard copied to clipboard

AWS Observability Instrumentation breaks HTTP API lambda functions when using response format 2.0

Open dpeters1 opened this issue 9 months ago • 0 comments

After setting up instrumentation in the new serverless dashboard, all of our API endpoints return a 500 - Internal Server Error code.

Peeking into the logs, this is caused by serverless looking for a dict response with statusCode key that is not required by AWS HTTP API V2 response format.

{
    "source": "serverlessSdk",
    "type": "ERROR_TYPE_CAUGHT_SDK_INTERNAL",
    "name": "AttributeError",
    "message": "'str' object has no attribute 'get'",
    "stack": "Traceback (most recent call last):\n  File \"/opt/python/serverless_aws_lambda_sdk/instrument/__init__.py\", line 483, in _close_trace\n    resolve_response_tags(outcome_result)\n  File \"/opt/python/serverless_aws_lambda_sdk/instrument/lib/response_tags.py\", line 11, in resolve\n    status_code = response and response.get(\"statusCode\")\nAttributeError: 'str' object has no attribute 'get'\n",
    "description": "Internal Serverless SDK Error. Please report at https://github.com/serverless/console/issue"
}

Per AWS documentation, it is valid to return a string response and API gateway will convert it to a dictionary:

With the 2.0 format version, API Gateway can infer the response format for you. API Gateway makes the following assumptions if your Lambda function returns valid JSON and doesn't return a statusCode:

  • isBase64Encoded is false.
  • statusCode is 200.
  • content-type is application/json.
  • body is the function's response.

dpeters1 avatar Oct 04 '23 22:10 dpeters1