A client-side SvelteKit dashboard application hosted on `https://admin.service.internal` sends a `DELETE` request to an Amazon API Gateway REST API. The request fails, and the browser console displays a CORS preflight error indicating that the `Access-Control-Allow-Origin` header is missing. The REST API is configured with a Lambda Proxy Integration and a custom Lambda Authorizer on the `DELETE` method. The developer has already used the API Gateway Console to enable CORS on the resource, which created an `OPTIONS` method. Which two actions must the developer take to resolve this issue?
- Configure the OPTIONS method on the resource to use NONE for its Authorization type in API Gateway, then redeploy the API.Answer
- Update the backend Lambda function mapped to the DELETE method to include the Access-Control-Allow-Origin header in the headers object of the returned JSON payload.Answer
- CConfigure an Integration Response in the API Gateway Console for the DELETE method to map the Access-Control-Allow-Origin header to the client's origin.
- DModify the custom Lambda Authorizer code to validate the authorization header and return an Allow policy for the OPTIONS method.
- EModify the CORS configuration on the S3 bucket where the SvelteKit application is hosted to permit DELETE methods from API Gateway.
Answer
To resolve the CORS preflight block, the developer must set the Authorization type of the OPTIONS method to NONE in the API Gateway Console, and modify the backend Lambda function for the DELETE method to return the Access-Control-Allow-Origin header in its response headers.
CORS preflight (OPTIONS) requests are initiated by the browser to determine whether the target server permits the cross-origin request. Because these preflight requests lack credentials, they cannot pass authorizers. Consequently, the OPTIONS method must have its Authorization set to NONE. Furthermore, under a Lambda Proxy Integration, API Gateway relies entirely on the backend payload structure to formulate the HTTP response. The developer must return the Access-Control-Allow-Origin header directly from the backend Lambda function to satisfy browser security validations during the subsequent DELETE request.
Step-by-Step Solution
Key Concept
Handling CORS preflight authorization and header injection in API Gateway Lambda Proxy integrations.
Estimated Time:3m 0s