Tom Schreck
Tom Schreck

Reputation: 5287

Multiple microservices inside same AWS API Gateway

I'm developing a series of microservices which need to share the same AWS API Gateway. Here's my structure:

/
   /assessments
      /skills
      /work-values      
   /graphql

/skills, /work-values, and /graphql are 3 different microservices I'm trying to register with the same AWS API Gateway. The problem I'm having is getting the serverless.yaml files for /skills, /work-values routes to nest under 'assessments'. There is no functionality for /assessments in-and-of-itself. It exists just so we can organize all of our assessments under the same URL path structure.

Here's my serverless.yaml file for `/work-values':

service:
  name: assessments-workvalues

...
custom:
  stage: ${opt:stage, self:provider.stage}

provider:
  ...
  apiGateway:
    restApiId:
       # THE FOLLOWING REFERENCES A VARIABLE FROM MY API GATEWAY ROOT
      'Fn::ImportValue': ${self:custom.stage}-ApiGatewayRestApiId
    restApiRootResourceId: 
      'Fn::ImportValue':   # HOW DO I GET THE PROPER VALUE HERE TO MAP TO `/assessments`?
    ...

functions:
  ...

Here's my serverless.yaml file for `/assessments':

service:
  name: assessments

custom:
  stage: ${opt:stage, self:provider.stage}

provider:
  ...
  apiGateway:
    restApiId:
       # THE FOLLOWING REFERENCES A VARIABLE FROM MY API GATEWAY ROOT
      'Fn::ImportValue': ${self:custom.stage}-ApiGatewayRestApiId
    restApiRootResourceId:
      'Fn::ImportValue': ${self:custom.stage}-ApiGatewayRestApiRootResourceId   

functions:
  ...

resources:
  Outputs:
    ApiGatewayRestApiId:
      Value:
        Ref: ApiGatewayRestApi
      Export:
        Name: ${self:custom.stage}-Assessments-ApiGatewayRestApiId
    ApiGatewayRestApiRootResourceId:
      Value:
         Fn::GetAtt:
          - ApiGatewayRestApi
          - RootResourceId 
      Export:
        Name: ${self:custom.stage}-Assessments-ApiGatewayRestApiRootResourceId

The problem seems to be coding the Outputs in serverless.yaml file for assessments route. When I run serverless deploy, I get this error message:

Error: The CloudFormation template is invalid: Unresolved resource dependencies [ApiGatewayRestApi] in the Outputs block of the template

At the end of Share an API Endpoint Between Services article, the author mentions 'You HAVE TO import /billing from the billing-api, so the new service will only need to create the /billing/xyz part.' (which seems to be the situation I'm in). But, the author does not explain how to import /billing. Or in my case, how do I import /assessments into the serverless.yaml files for each assessment microservice?

Upvotes: 0

Views: 1031

Answers (1)

Tom Schreck
Tom Schreck

Reputation: 5287

After further research, I found this link:

Splitting Your Serverless Framework API on AWS

I ended up reworking my original approach following what's in the article above. The piece I was missing was having a root or base serverless file which is used to create your routing in AWS API Gateway and expose those placeholders as output which your subsequent child serverless files consume as input for wiring up your child lambda functions to routes under the API Gateway umbrella.

Upvotes: 2

Related Questions