DEV Community

Adam Crockett 🌀
Adam Crockett 🌀

Posted on

Using lambda to get around cors

I am building my portfolio and one of the requirements is to not get tied to a web server, in fact I am probably going to host in S3 for the time being. There is one particular API which I want to request in the browser, the GitHub API over graphql, unfortunately I get cors issues as you might expect. So my idea was to create a lambda that makes that request and returns the result. Is this a good use of a lambda? It strikes me that I am making 2 requests. Even then I am struggling to configure cors. I have to say though the serverless framework has been very helpful.

Top comments (3)

Collapse
 
andrewbrown profile image
Andrew Brown 🇨🇦 • Edited

On your S3 bucket you can set CORS

If the API call you are making is a public API and not with credentials then it would make sense to call this from your javascript code.

If it is credentialed information you'd want to use a Lambda which you can back by API Gateway.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Looks like I choose wisely, it was the latter. My personal API oauth token is now in environmental variables in both local .env file.

I wondered how much work my lambda should do alter the result in a format I need so compute would do all the work, but instead I went with returning the data as is.

Either way this is my first lambda and a great intro into serverless.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Update, I don't know what happened between last night and this morning but my lambda now returns data to my browser, nieeeeeece!