Serverless 'ConflictException' when using two different nested 'paths' in two different lambdas

The Problem

I have two different NodeJs lambdas. Both contain their own serverless.yml file. The lambdas will form part of the same service so they share the same base URL. I’ve made use of the restApiId & restApiRootResourceId keys in the serverless.yml file to achieve this.

The problem I have is, when I try to deploy both lambdas, the second one I deploy returns a serverless error:

[0]  
[0]   An error occurred: ApiGatewayResourceApi - Another resource with the same parent already has this name: api (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: 93167f58-5890-4f45-bf02-e036b2c04727; Proxy: null).
[0]  
[0]   Get Support --------------------------------------------
[0]      Docs:          docs.serverless.com
[0]      Bugs:          github.com/serverless/serverless/issues
[0]      Issues:        forum.serverless.com

I want a set up like follows:

Lambda1: https://some.base.url.from.aws/api/v1/staff/getall - GET Lambda2: https://some.base.url.from.aws/api/v1/staff/insert - POST

My serverless.yml file for each lambda is as follows:

# serverless.yml

service: api-lambda-insert-staff

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-west-1
  apiGateway:
    restApiId: xxxxxxxxx
    restApiRootResourceId: xxxxxxxx

functions:
  app:
    handler: index.handler
    events:
      - http:
          path: api/v1/staff/insert
          method: POST

# serverless.yml

service: api-lambda-getall-staff

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-west-1
  apiGateway:
    restApiId: xxxxxxx
    restApiRootResourceId: xxxxxxxxx

functions:
  app:
    handler: index.handler
    events:
      - http:
          path: api/v1/staff/getall
          method: GET

what I’ve tried

I’ve looked through serverless documentation, and found a few examples which have a similar set up to me, so not quite sure what’s going wrong.

I’ve changed the function name ‘app’ on each Lambda to make them unique, but again, same problem.

At a loose end now, any help, is much appreciated.

Thanks.

1 Like

I know this question was posted a couple of years ago, but I thought it might be helpful to others if I shared my findings here. Using provider.apiGateway.restApiResources to declare existing resources solves this issue.

There’s AWS::ApiGateway::Resource resource type created for each path part in your API gateway.
In your case, api, api/v1 & api/v1/staff are three common API gateway resources between the two services you shared. These resources are created when you deploy your first service. And, since these resources already exist on second service’s deployment, you get a conflict error.

You’ll have to export all 3 path resources from the first stack and reference it in the second service to solve the issue.

Service api-lambda-insert-staff [Assuming that’s the first stack to be deployed]

    Outputs:
      OPApiGatewayResourceApi:
        Value: !Ref ApiGatewayResourceApi # Please confirm the logical resource name from the cloudformation stack created on AWS
        Export:
          Name: resource-api

      OPApiGatewayResourceApiV1:
        Value: !Ref ApiGatewayResourceApiV1
        Export:
          Name: resource-api-v1

      OPApiGatewayResourceApiV1Staff:
        Value: !Ref ApiGatewayResourceApiV1Staff
        Export:
          Name: resource-api-v1-staff

Serivce api-lambda-getall-staff

provider:
  ...
  apiGateway:
    ...
    restApiResources:
      api: !ImportValue resource-api
      api/v1: !ImportValue resource-api-v1
      api/v1/staff: !ImportValue resource-api-v1-staff