Micro services with shared API Gateway

I have not been able to get Resources/Outputs/Exports to work in an effort to use multiple serverless configs that build out micro services to a single api gateway. According to this issue/discussion this should now work.

i’m sure its my config, but I have tried the documented way from this link… I am getting the error The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template

I would be deeply grateful for any advice… thank you. This is the config I am using:

service: api-model 
frameworkVersion: '^1.33.0'

provider:
  name: aws
  runtime: nodejs8.10
  apiName: myapp-api-${opt:stage,'dev'}
  
  Resources:
    Outputs:
      apiGatewayRestApiId:
        Value:
          Ref: ApiGatewayRestApi
        Export:
          Name: apiGateway-restApiId
      apiGatewayRestApiRootResourceId:
        Value:
          Fn::GetAtt:
            - ApiGatewayRestApi
            - RootResourceId
        Export:
          Name: apiGateway-rootResourceId

  apiGateway:
    restApiId:
      Ref: ApiGatewayRestApi
    restApiResources:
      Fn::GetAtt:
        - ApiGatewayRestApi
        - RootResourceId

functions:
  api-model-read:
    environment:
      DEBUG: 'false'
    events:
      - http:
          path: /data/model
          method: get
      - http:
          path: /data/model/{modelId}
          method: get
          request:
            parameters:
              paths:
                modelId: true

    handler: read.handler
    memorySize: 128
    name: api-${opt:stage, 'dev'}-model-read
    timeout: 30

The Resources need to go under a resources key in the serverless.yml for the service that creates the API GW. See https://serverless.com/framework/docs/providers/aws/guide/resources#configuration

You then import it using the ${cf:...} variables syntax into other services. See https://serverless.com/framework/docs/providers/aws/guide/variables#reference-cloudformation-outputs

1 Like

Thank you. I added resources node, and I still get an error

The CloudFormation template is invalid: Invalid template resource property ‘apiGatewayRestApiId’

For some reason, when deploying the original “service a” with functions at the same time was causing my error. As soon as I created a separate sls deployment, one for just the API resource. The documentation worked, and I can treat all of my micro-services with the referenced out api id.

Thanks for your help!

1 Like