2

I currently have a Lambda function that works when I test it (it is a Mailchimp integration for subscribing a user to a list).

See my lambda function here on Github

I made an API endpoint using API gateway through lambda.

However, when I am testing it on my app (currently through localhost:3000, I am getting a CORS error from API gateway:

Access to XMLHttpRequest at 'https://MY_API.execute-api.us-west-2.amazonaws.com/default/mailchimp-lambda' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried multiple things:

  1. Adding the npm package middy as recommended on https://serverless.com/blog/cors-api-gateway-survival-guide/
  2. Using a callback in lambda and adding CORS as recommended on this question: Configure CORS response headers on AWS Lambda?

Can anyone explain how to access a lambda function?

Zach G
  • 597
  • 2
  • 10
  • 23
  • Looks like you don't have cors enabled. This may help you: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html – deosha Feb 10 '19 at 02:15
  • I enabled CORS through the API Gateway console: ✔ Create OPTIONS method ✔ Add 200 Method Response with Empty Response Model to OPTIONS method ✔ Add Mock Integration to OPTIONS method ✔ Add 200 Integration Response to OPTIONS method ✔ Add Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin Method Response Headers to OPTIONS method ✔ Add Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin Integration Response Header Mappings to OPTIONS method – Zach G Feb 10 '19 at 03:18
  • Did you redeploy api gateway after that? – deosha Feb 10 '19 at 05:03
  • @deosha yes I redeployed, I also added a Resource Policy including "Principal": "*" in the Statement, to no avail. Also, I'm looking at CloudWatch logs and I am console logging the event and event.body is null when it comes from my Browser, but it is defined when it comes through Postman. It says the httpMethod is OPTIONS which is weird, I wouldn't think that the lambda function runs during the automatic OPTIONS requests. – Zach G Feb 10 '19 at 05:10
  • What is your configuration in the API Gateway configuration->Gateway Responses-> Default 4XX ? Can you add Access-Control-Allow-Origin: '* over there and redeploy and check again? – deosha Feb 10 '19 at 05:41
  • I added Access-Control-Allow-Origin: '*' to the default 4XX gateway response and deployed, and it still doesn't fix it. The lambda API actually is currently successfully adding my user to mailchimp and working, but the response from lambda back to API Gateway is not working as a 200 response. I am currently using the callback method as described in example #2 above in the description. – Zach G Feb 10 '19 at 16:15
  • I got it to work! See the lambda function on Github for the code. – Zach G Feb 10 '19 at 19:38
  • How did you solve it? – aless80 Jul 30 '19 at 09:39
  • @aless80 it's in the Github repo linked in the question. – Zach G Aug 08 '19 at 02:32

1 Answers1

0

If you're using the LAMBDA-PROXY integration which means that your response needs the Access-Control-Allow-Origin header.

Here's the function example:

exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin" : "*" // Required for CORS
        },
        body: JSON.stringify('Hello from Lambda!!'),
    };
    return response;
};

Here you can find more details about similar problem: https://github.com/serverless/serverless/issues/1955