API Gateway setup for CORS - OPTIONS and Mock

0

I have a Lambda that supports a GET request and is secured using a Cognito User Pool. I can get a JWT and call the GET successfully. However, I also need to configure CORS, as this API will be called from an Amplify React app.

My Cloudformation template defines the Lambda (AWS::Serverless::Function) and the API Gateway (AWS::Serverless::Api).

The OPTIONS is set up as a Resource, alongside another for "ANY". I can test both of these successfully using the AWS Management Console. However, I can't get a successful OPTIONS request (curl or Amplify-React app) - I always get a 500 response. The logs in CloudWatch say: "Execution failed due to configuration error: Unable to transform request"

The OPTIONS is set up with a Mock, as there is no need to call the Lambda.

Again - the OPTIONS Resource tests ok.

I've exhausted ChatGPT and Amazon Q - and myself!!

Any ideas?

1개 답변
1

Ensure you have properties for CorsConfiguration for this AWS Serverless Application Model AWS::Serverless::Api - below is an example from the documentation.

Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Cors:
        AllowMethods: "'POST, GET'"
        AllowHeaders: "'X-Forwarded-For'"
        AllowOrigin: "'www.example.com'"
        MaxAge: "'600'"
        AllowCredentials: true
profile pictureAWS
답변함 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠