Search code examples
amazon-web-servicesaws-lambdacors

AWS Lambda Function URL - CORS Allow origin is not working properly


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:

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:

  1. 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?
  2. 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:

Angular request screenshot

I tried 'Access-Control-Allow-Origin': '*' as well but it didn't work.

What am I doing wrong here?


Solution

  • 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.

    Lambda Function URL config

    I'm still open to suggestions about whether I should change those allowed and exposed headers, if all is not required.

    Thanks.