title: cross origin for amazon lambda function from localhost in gatsby site
body: <p>I have the following code which works when I run it as a local serverless function with netlify dev, but I need it to run cross origin from a dev server to the hosted server function. I put the function in a aws lambda function but I am getting a cross origin blocked error on my https:dev.website.com, I thought I have the correct headers in the return object so not sure why I am getting a cross origin error.</p>
  <p>Any help would be great</p>
  <pre><code>const sanityClient = require(&quot;@sanity/client&quot;);
  const client = sanityClient({
    projectId: &quot;random-id&quot;,
    dataset: &quot;production&quot;,
    useCdn: true,
  });
  
  exports.lambdaHandler = async (event, context) =&gt; {
    var body = JSON.parse(event.body);
  
    //console.log(body.price_id)
  
    try {
      const checkPriceId = async (test) =&gt; {
        const query = `*[_type == &quot;products&quot; &amp;&amp; price_id == &quot;${body.price_id}&quot;]`;
  
        const documents = await client.fetch(query, {}); // this could throw
  
        return documents.map((document) =&gt; document.sold);
      };
  
      var ok = checkPriceId().then((test) =&gt; {
        return new Promise(function (resolve, reject) {
          //console.log(test) // this will log the return value from line 7
          console.log(test);
          resolve(test);
        });
      });
  
      var bools = await ok;
      // prettier-ignore
  
      return {
        statusCode: 200,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Headers': 'Content-Type',
          'Access-Control-Allow-Methods':'GET, POST, OPTION',
        },
        body: JSON.stringify({
          sold: bools,
        }),
      };
    } catch (err) {
      return { statusCode: 500, body: err.toString() };
    }
  };
  </code></pre>
  <p>This is my request to the function if that helps</p>
  <pre><code> var fetchUrl = https://random.executue-api.aws.com/prod/sold //not exact 
  
   var fetchData = async function () {
      const response = await fetch(fetchUrl, {
        method: &quot;post&quot;,
        headers: {
          &quot;Content-Type&quot;: &quot;application/json&quot;,
        },
        body: JSON.stringify({
          price_id: final,
        }),
      })
        .then(res =&gt; {
          return res.json()
        })
        .catch(error =&gt; console.log(error))
  
      return response
    }
  </code></pre>
  <p><strong>Update</strong>:</p>
  <p>I tried adding cors the way suggested in the answer below, but it failed seen below so I tried manually adding the method response seen after.</p>
  <p>I still get a cross domain error. And I have changed the domain so it is now https as well. Really stuck here.</p>
  <p><a href="https://i.stack.imgur.com/w6KEf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w6KEf.png" alt="cors failing" /></a></p>
  <p><a href="https://i.stack.imgur.com/Njmeo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Njmeo.png" alt="manual cors addition" /></a></p>
  <p>I was looking into this more, and it seems like before it does the actual post it does a cors check at the options method, so I added in the same access control headers, and deployed but did not work. Don't quite get this.</p>
  <p><a href="https://i.stack.imgur.com/jhTqp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jhTqp.png" alt="enter image description here" /></a></p>