serverless / dashboard

MIT License
26 stars 10 forks source link

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

Open dpeters1 opened 1 year ago

dpeters1 commented 1 year ago

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.