Serverless with swagger/openapi

I was using SAM before. In SAM, I can have a template like this

Resources:
  GetCaseFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: get_case/
      Handler: app.lambda_handler
      FunctionName: get-case
      Runtime: python3.7
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /case
            Method: get
            RestApiId: 
              Ref: MyRestApi
  MyRestApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionBody:
        'Fn::Transform':
          Name: 'AWS::Include'
          Parameters:
            Location: ./openapi.yaml

In openapi,yaml, I have path set as below, which reference the “GetCaseFunction” defined above

paths:
  /case:
    get:
      responses:
        xxxxxxxx
      x-amazon-apigateway-integration:
        uri:
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetCaseFunction.Arn}/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        type: "aws_proxy"

This way I have everything for API defined in openapi.yaml, and it works fine.

In serverless, I tried to achieve same thing. I try not to define api related stuff under functions. so this this is what I did

provider:
  name: aws
  runtime: python3.7
functions:
  hello:
    handler: handler.getcase
    name: get-case
    events:
     - http:
        path: /case
        method: get
resources:
 Resources:
  ApiGatewayRestApi:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Body: ${file(api/openapi.yaml)}

I thought openapi.yaml will override api created by funtions.events. But instead I got
An error occurred: ApiGatewayResourceCase - Another resource with the same parent already has this name: case (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException;..)

Is there any way serverless can achieve what SAM does in this case?

Thanks

1 Like