A client-side web application hosted on `https://portal.member-services.org` makes an HTTP `PUT` request to an Amazon API Gateway REST API. The API is integrated with a backend AWS Lambda function using Lambda Proxy integration. Users report that the updates fail. Inspecting the browser console reveals a CORS error stating that the `Access-Control-Allow-Origin` header is missing, while the API Gateway execution logs show a `502 Bad Gateway` error due to a malformed Lambda response. Which two actions should the developer take to resolve these errors?
- Configure the API Gateway resource to support CORS by enabling the OPTIONS method and returning the required CORS headers for preflight requests.Cevap
- Update the backend Lambda function response payload to return a JSON object containing a statusCode, a body, and a headers map that includes the Access-Control-Allow-Origin header.Cevap
- CAdd a CORS configuration policy containing the Access-Control-Allow-Origin header to the Amazon S3 bucket that hosts the client-side web application.
- DConfigure the API Gateway Integration Request settings for the PUT method to inject the Access-Control-Allow-Origin header into the incoming request headers.
- ESwitch the integration type to Lambda Custom Integration and configure a Gateway Response mapping template to format the string returned by the Lambda function.
Cevap
Configure the API Gateway resource to support CORS by enabling the OPTIONS method, and update the backend Lambda function response payload to return a JSON object containing a statusCode, a body, and a headers map that includes the Access-Control-Allow-Origin header.
The correct options are to configure the OPTIONS preflight method in API Gateway and update the backend Lambda function to return a structured JSON response containing the Access-Control-Allow-Origin header in its headers map. The preflight OPTIONS method handles the browser's initial handshake. Since Lambda Proxy integration is used, API Gateway passes the response directly from the Lambda function, requiring the function itself to return the necessary CORS headers and a valid format (statusCode, headers, and body) to avoid a 502 Bad Gateway error.
Adım Adım Çözüm
Anahtar Kavram
CORS troubleshooting and Lambda Proxy response formatting in Amazon API Gateway.