0

I have developed lambda functions in node.js and trying to deploy to api gateway with proxy integration using SAM templates. my lambda functions are returning headers as mentioned in AWS documentation

const response = {
       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.stringify('Hello from Lambda!'),
   };

template.yaml code

GetClientFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: functions/GetClient/
        Handler: index.handler
        Runtime: nodejs14.x
      Events:
        GetClientCustomFieldMap:
          Type: Api
          Properties:
            RestApiId: !Ref ClientApi # this is api with authorizer configured
            Path: api/Client/GetClient/{name}   
            Method: get

It is creating all resources which mentioned in the path like 'api', 'Client', 'GetClient' but not with Options method, also getting cors error. Tried multiple ways from stackoverflow but could not resolve it. Followed this url as well How to enable CORS with AWS SAM

Sai Kiran
  • 55
  • 6

1 Answers1

-1

You need to configure Cors of the Api resource for the OPTIONS methods, see https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-cors

Milan Gatyás
  • 2,509
  • 1
  • 17
  • 23
  • I did that, Seems it is issue with SAM templates. Posted a question in their GitHub forum. https://github.com/aws/serverless-application-model/issues/2372 – Sai Kiran Apr 11 '22 at 17:48