0
votes

I have an aws lambda function that returns the following response:

var responseBody = { cost: price };
var response = {
   statusCode: 200,
   headers: {
          "Access-Control-Allow-Origin": "*"
     },
    body: JSON.stringify(responseBody),
    isBase64Encoded: false
};

callback(null, response);

But I get the following error in my frontend Angular application.

Access to XMLHttpRequest at 'https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/price' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

2
You are being blocked at the API level. Not your lambda. You may want to add that configuration.Darren Forsythe

2 Answers

6
votes

If you are using API Gateway HTTP APIs (not sure if this is relevant for the REST APIs):

Lets say I have an endpoint at /POST products. I had to add another endpoint at /OPTIONS products and integrate it with a simple Lambda function that just returns the HTTP 200 OK (or HTTP 204 No Content), and the "Access-Control-Allow-Origin": "*" header (or even better, specify the URL of your origin/client).

This is because browsers issue a preflight /OPTIONS request to the same endpoint, before issuing the actual request (see more), for all HTTP requests except GET and POST with certain MIME types (source).

1
votes

You need to enable CORS on your resource using API gateway, check this link to more information https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html