AWS API Gateway – Lambda CORS troubles: Access to fetch at execute-api from origin cloudfront.net has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

Problem

Access to fetch at execute-api from origin cloudfront.net has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

Solution

First of all; I assume you already have CORS enabled via API Gateway (also don’t forget to deploy it).

In my case the problem was on the lambda side.

I was using ASP.net Core and it was doing its own CORS checks (you can easily see this via CloudWatch logs).

To allow any requests through you can add the following to corresponding methods in Startup.cs

            services.AddCors(o => o.AddPolicy("AllowAllPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
            }));

////////

            app.UseCors("AllowAllPolicy");

 

 

 

About Neil Rubens

see http://ActiveIntelligence.org
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*