Skip to content

sam validate: OpenApi errors with CORS on HttpApi #3041

Closed

Description

@manurana

Description:

A seemingly innocuous config with template.yaml and api.yaml gives an error on running sam validate.
The api.yaml seems to be a valid openAPI document as validated in a couple of sites (https://editor.swagger.io/ , https://apitools.dev/swagger-parser/online/).
The error only occurs if you add the CorsConfiguration to the HttpApi in the template. If you remove that section, the validation succeeds.

Steps to reproduce:

run sam validate in the directory with the following
template.yaml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: AltLinen

Resources:
  MyHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      AccessLogSettings:
        DestinationArn: !GetAtt MyLogGroup.Arn
      CorsConfiguration:
        AllowMethods:
          - POST
        AllowHeaders:
          - "Content-Type"
        AllowOrigins:
          - https://www.example.com
      DefinitionBody:
        "Fn::Transform":
          Name: "AWS::Include"
          Parameters:
            Location: "./api.yaml"

  MyLogGroup:
    Type: AWS::Logs::LogGroup

  MyHttpApiRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service: "apigateway.amazonaws.com"
            Action:
              - "sts:AssumeRole"

  ShippingRatesLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/
      Handler: appShipping.lambdaHandler
      Runtime: nodejs12.x
      Events:
        APIPost:
          Type: HttpApi
          Properties:
            Path: /shopify/shipping_rates
            Method: POST
            PayloadFormatVersion: "2.0"

Outputs:
  ApiEndpoint:
    Description: "HTTP API endpoint URL"
    Value: !Sub "https://${MyHttpApi}.execute-api.${AWS::Region}.amazonaws.com"

and api.yaml

openapi: "3.0.1"
info:
  title: "Alt Linen Backend"
  version: "1.0.0"
paths:
  /shopify/shipping_rates:
    post:
      responses:
        default:
          description: "Default response"

x-amazon-apigateway-importexport-version: "1.0"

Observed result:

2020-11-26 16:18:38,967 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
Template provided at '/Users/manu/developer/kindl/yamltest/template.yaml' was invalid SAM Template.
2020-11-26 16:18:46,181 | Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam validate', 'duration': 7212, 'exitReason': 'InvalidSamTemplateException', 'exitCode': 1, 'requestId': 'a0e88027-fe00-4231-9622-b936e502d9bd', 'installationId': '8ec6424d-7836-4a60-886a-e0ad452769d0', 'sessionId': '6758c9cb-cb59-4206-bc69-b51899bb089d', 'executionEnvironment': 'CLI', 'pyversion': '3.8.6', 'samcliVersion': '1.12.0'}}]}
2020-11-26 16:18:47,359 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: [InvalidResourceException('MyHttpApi', "Unable to add Cors configuration because 'DefinitionBody' does not contain a valid OpenApi definition.")] ('MyHttpApi', "Unable to add Cors configuration because 'DefinitionBody' does not contain a valid OpenApi definition.")

Expected result:

Validation should succeed
OR, the correct validation error should be shown

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS
  2. sam --version: SAM CLI, version 1.12.0

Add --debug flag to command you are running

Activity

  1. transferred this issue fromaws/aws-sam-clion Nov 27, 2020
  2. NickLiffen commented on Feb 3, 2021

    @NickLiffen

    I can also replicate this problem 👍 This is something that I am running into as well 👍 @manurana how did you overcome the problem?

  3. manurana commented on Feb 4, 2021

    @manurana
    Author

    @NickLiffen : I just ignored it !
    The deployment worked fine.

  4. sriram-mv commented on Apr 11, 2021

    @sriram-mv
    Contributor

    I just tried inlining the definition body without removing anything else and it just works.

    AWSTemplateFormatVersion: "2010-09-09"
    Transform: AWS::Serverless-2016-10-31
    Description: AltLinen
    
    Resources:
      MyHttpApi:
        Type: AWS::Serverless::HttpApi
        Properties:
          AccessLogSettings:
            DestinationArn: arn:aws:logs:us-west-2:847756679349:log-group:anewone:*
            Format: "$context.requestId"
          CorsConfiguration:
            AllowMethods:
              - POST
            AllowHeaders:
              - "Content-Type"
            AllowOrigins:
              - https://www.example.com
          DefinitionBody:
            openapi: "3.0.1"
            info:
              title: "Alt Linen Backend"
              version: "1.0.0"
            paths:
              /shopify/shipping_rates:
                post:
                  responses:
                    default:
                      description: "Default response"
            x-amazon-apigateway-importexport-version: "1.0"
    
      MyHttpApiRole:
        Type: "AWS::IAM::Role"
        Properties:
          AssumeRolePolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Principal:
                  Service: "apigateway.amazonaws.com"
                Action:
                  - "sts:AssumeRole"
    
      ShippingRatesLambda:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: hello_world/
          Handler: app.lambda_handler
          Runtime: python3.8
          Events:
            APIPost:
              Type: HttpApi
              ApiId: !Ref MyHttpApi
              Properties:
                Path: /shopify/different_shipping_rates
                Method: POST
                PayloadFormatVersion: "2.0"
    
    Outputs:
      ApiEndpoint:
        Description: "HTTP API endpoint URL"
        Value: !Sub "https://${MyHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
    

    sam validate worked when it was inlined like this, so did a deploy. This seems to do more with intrinsics not being resolved as well.

  5. jfuss commented on Jul 13, 2021

    @jfuss
    Contributor

    From Sriram's comment above this seems more like a SAM CLI issue given the deployment works but sam validate doesn't. Transferring to SAM CLI's Repo.

  6. added
    area/validate
    on Aug 16, 2021
  7. added
    stage/waiting-for-release
    on Jan 21, 2022
  8. mndeveci commented on Feb 16, 2022

    @mndeveci
    Contributor

    Released with SAM CLI v1.38.0

  9. evin-murphy commented on Oct 4, 2023

    @evin-murphy

    This issue is still occurring on SAM 1.72.0 when setting Auth ResourcePolicy and using Fn::Transform on my OpenAPI file. I added a comment to #803 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/validatestage/waiting-for-release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions