Solving CORS in API Gatway AWS.

Praveen Sambu
3 min readFeb 1, 2021

--

When we are working with serverless applications. It is quite often we get into Cors issue. Here I would explain how can we solve this in AWS API Gateway.

Cors is a browser security feature that restricts cross-origin HTTP requests that are initiated from scripts running in the browser. If your REST API’s resources receive non-simple cross-origin HTTP requests, you need to enable CORS support.

So, When we have our API exposed to outside we can control the calling applications using this CORS. So when integrating into client applications it is likely we see this issue.

The error tries to say it is failed to load the API Gate way as No ’Access-Control-Allow-Origin; header is present on the requested site.

In order to solve this we need to set up few changes in API Gateway

login to your AWS Console

Goto API Gateway and navigate to your API Gateway

Fig 1: API Gateway — Resources page.

Select the Get/Post/Put request where you would like to enable CORS

Go to Method Response in the above picture, Add Header and add the

Access-Control-Allow-Headers

Access-Control-Allow-Origin

Access-Control-Allow-Methods

Figure 2

Next, Go to Integration Response tab as shown in Figure 1. and open the header Mappings and provide the CORS values

Integration Response.

You can skip this step by passing these values through lambda also if you keep the request as proxy integration.

Lambda example Response

Now Select Actions tab under the Resources section

After setting the values, Enable Cors as shown in the picture. This would add the Cors to all the methods. Now you would be all set to use this API in your client side applications.

--

--

Praveen Sambu
Praveen Sambu

Written by Praveen Sambu

Software Engineer |AWS Community Builder |Technical Blogger | Trainer . Founder of Cloud In Detail (https://cloudindetail.com/) still working on building blog…

No responses yet