- Newest
- Most votes
- Most comments
If you are using a Lambda custom (non-proxy) integration:
When you enable CORS by using the AWS Management Console, API Gateway creates an OPTIONS method and attempts to add the Access-Control-Allow-Origin
header to your existing method integration responses. This doesn’t always work, and sometimes you need to manually modify the integration response to properly enable CORS. Usually this just means manually modifying the integration response to return the Access-Control-Allow-Origin
header.
You may need to add OPTIONS
to the AllowMethods
in your template.yaml
file:
Cors:
AllowMethods: "'GET, POST, OPTIONS'"
If you are using a Lambda proxy integration or HTTP proxy integration:
Your backend is responsible for returning the Access-Control-Allow-Origin
and Access-Control-Allow-Headers
headers, because a proxy integration doesn't return an integration response.
You will need to ensure that your Lambda function returns the relevant headers:
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': 'https://www.example.com',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps('Hello from Lambda!')
}
See: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html
Relevant content
- asked 3 years ago
- asked 2 months ago
- asked a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
Thank you for responding to my question.
After some investigating, I learned that the SAM CLI templates performs a proxy lambda function. So I implemented the example lambda_handler and finally got a 200 response from my react app. With that said, I tried changing up the function by adding in a event['queryStringParameters']['name'] and returned that in the body, but Im getting CORS errors again. Is there something different I need to do with the parameters?