Question

Difficulty: MediumTroubleshooting API Gateway Errors and CORS

A web application hosted on `https://manager.fleet-operations.com` sends an HTTP `POST` request to an Amazon API Gateway REST API. The API is integrated with a backend AWS Lambda function using a Lambda Proxy integration. Although the developer enabled CORS on the API Gateway resource, the browser console displays a CORS preflight blocked error and a `502 Bad Gateway` status. Which two actions must the developer take to resolve these errors?

  1. Modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the value 'https://manager.fleet-operations.com'.Answer
  2. Ensure the Lambda function returns a structured JSON object containing the 'statusCode', 'headers', and 'body' fields.Answer
  3. C
    Add a CORS configuration policy to the Amazon S3 bucket that hosts the frontend application at 'https://manager.fleet-operations.com'.
  4. D
    Configure an API Gateway integration response mapping template to extract the headers and format the response.
  5. E
    Change the API Gateway integration type to Lambda Custom integration and configure the Lambda function to return a plain text string instead of a JSON object.

Answer

To resolve the CORS and 502 Bad Gateway errors, the developer must modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the value 'https://manager.fleet-operations.com', and ensure the Lambda function returns a structured JSON object containing the 'statusCode', 'headers', and 'body' fields.
The correct options are to modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the client's origin, and ensure the Lambda function returns a structured JSON object containing 'statusCode', 'headers', and 'body'. In a Lambda Proxy integration, the backend Lambda function is responsible for both the formatting of the response payload (which prevents the 502 Bad Gateway error) and returning the necessary CORS headers.

Step-by-Step Solution

1
Analyze the error context and integration type.
Identify that the API uses Lambda Proxy integration and returns both a 502 Bad Gateway error and a CORS preflight blocked error.
With Lambda Proxy integration, API Gateway expects a structured JSON output from Lambda, and does not automatically inject CORS headers into the backend response.
2
Fix the response payload format of the Lambda function.
Format the Lambda function's return value as a JSON object containing 'statusCode', 'headers', and 'body'.
This resolves the 502 Bad Gateway error, which is caused by a malformed response that API Gateway cannot parse.
3
Add the required CORS headers to the Lambda response.
Include 'Access-Control-Allow-Origin': 'https://manager.fleet-operations.com' within the 'headers' object of the Lambda response.
This resolves the CORS preflight blocked error for the actual POST request, as the proxy integration passes backend headers directly to the client.

Key Concept

CORS and Response Formatting in API Gateway Lambda Proxy Integrations
Rate this question