API Gateway Domains Across Services

Link to chapter - https://serverless-stack.com/chapters/api-gateway-domains-across-services.html

Hi! I’m trying to implement this architecture on the project created during parts one and two. I have added the functions (create, get, list, delete, and update) to the notes service, deployed everything except the user service because the instructions indicated that the service only existed for the point of the tutorial. I hooked the endpoints up to the frontend running locally and was able sign up as a user but I when I try to add a note I get a 502 error. Any idea why that might be? I have cors set to true in my notes service serverless.yml file for all of my added functions.

Okay I figured it out. I had neglected to install the node modules needed for my functions import statements and it was throwing an error at the api gateway.

The chapters on debugging serverless API issues and API Gateway and Lambda logs were what saved me.

Hopefully that helps someone in the future. :v:

1 Like

Glad you figured it out!

1 Like

Hey!
I was finishing the section dealing with the mono repo - and I don’t know if this on purpose - yet I can’t find the API Gateway error-behavior for the 4XX and 5XX (which was called api-gateway-errors.yml in both sections 1 and 2) responses.

The missing code would be

Resources:
  GatewayResponseDefault4XX:
    Type: 'AWS::ApiGateway::GatewayResponse'
    Properties:
      ResponseParameters:
         gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
         gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
      ResponseType: DEFAULT_4XX
      RestApiId:
        Ref: 'ApiGatewayRestApi'
  GatewayResponseDefault5XX:
    Type: 'AWS::ApiGateway::GatewayResponse'
    Properties:
      ResponseParameters:
         gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
         gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
      ResponseType: DEFAULT_5XX
      RestApiId:
        Ref: 'ApiGatewayRestApi'

Or am I missing something here?

Other “issue” that I’m facing is finding a way to create the API Gateway as a separate service, so I don’t chain the dependency graph of services on a AWS Lambda function - to effectively splitting concerns. When I try to create a service only for API Gateway, I always get The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template. Do you guys know if there is a way to do this?

Thanks in advance!

Hmm I’m not totally clear on your first issue? Are you looking for where the snippet came from?

For the second issue. Serverless Framework treats Lambda and its API endpoints together. So I’m not sure if it makes sense to split API Gateway separately?

About the first issue: I was not clear at all, let me go through it again.

I was implementing your mono-repo (that make cross-references through multiple stacks), and I haven’t found the snippet that I’ve mentioned in the previous comment, the one that specifies default responses for our RestAPI. What I mean with “I haven’t found” is that it does not exist in the mono-repo approach, but exists in the first part of the tutorial. I’ve been able to implement it myself, but I was wondering if this was done on purpose?

About the second issue: I’ve actually grasped the concept of the RestAPI that the serverless framework does behind the scenes, so what I was trying to achieve is just “manually” creating a RestAPI, which doesn’t make much sense given the fact that serverless framework creates on in our stack whenever we have at least a function declared inside it. You’re correct, it makes no sense splitting it.

Thanks for the response.

Yeah so the mono-repo GitHub repo that we have is very barebones, just to display the concept of having multiple services. That’s the reason we left out some of those details.

We are going to update it soon to have a more usable template that people can use.