I am trying to allow requests to my Lambda Function URL only from my specific URL and only for POST methods.
Here is my Lambda Function URL configuration:
Here is the Lambda function content:
import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!!')
}
Problems:
- When I go to the function URL on my browser I see "Hello from
Lambda!!". Considering it is a GET call I shouldn’t see the result
right? - When I call the function URL with HTTP POST method from my Angular App (origin: localhost:4200), I am getting CORS error "No ‘Access-Control-Allow-Origin’ header is present on the requested resource." But I added that header to my request as shown in the screenshot below:
I tried ‘Access-Control-Allow-Origin’: ‘*’ as well but it didn’t work.
What am I doing wrong here?
2
Answers
After some trial and error, I found the solution.
As seen in the screenshot below, I added the same headers to Expose Headers in Lambda Function URL config. This way, "No 'Access-Control-Allow-Origin' header is present on the requested resource." error is gone, since they are now exposed to the requester.
I'm still open to suggestions about whether I should change those allowed and exposed headers, if all is not required.
Thanks.
It’s answered over in: API Gateway CORS: no 'Access-Control-Allow-Origin' header – you need to return the CORS headers also in the lambda response OR add an OPTIONS call to have the CORS response.