Getting cors error when trying to call a lambda function from localhost:3000

0

I have created a lambda function from cloudformation template code, added authorization of cognito user and it works fine in postman, but when calling it from localhost:3000 it is giving cors error.

Have attached SS of calling api and cloudformation template code.

Would appreciate a help. Thanks in Advance..!

  • Sample code of cloudformation template:
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Application.
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Cors:
        AllowMethods: "'GET,POST,OPTIONS'"
        AllowHeaders: "'Content-Type','X-Amz-Date','Authorization','X-Api-Key','X-Amz-Security-Token'"
        AllowOrigin: "'http://localhost:3000'"
  ItemFunction:
    Type: AWS::Serverless::Function
    Name: ReadAllItem
    Properties:
      CodeUri: ''
      Handler: AWSServerless2::AWSServerless2.MyCRUDFunction::ReadAll
      Runtime: dotnet6
      Timeout: 30
      Events:
        GetItem: # Corrected event ID to match the event type
          Type: Api
          Properties:
            Path: /items
            Method: get
            RestApiId: !Ref MyApi # Reference the API resource
Outputs: {}
  • cors error in console

cors error in console

1 Answer
2
Accepted Answer
profile pictureAWS
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions