You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Serverless Framework拆分栈后多文件共享同一API Gateway的实现咨询

Sharing a Single API Gateway or Unifying Access with a Custom Domain for Multiple Serverless Services

Great question! I’ve hit this exact CloudFormation resource limit when scaling Serverless APIs, so let me walk you through two proven approaches to solve this—either sharing one API Gateway across all your services, or unifying access via a custom domain even if you keep separate API Gateways.

Approach 1: Share a Single API Gateway Across Multiple Serverless Services

This is the most direct way to stay under the CloudFormation resource limit while keeping all your API endpoints in one place. Here’s how to set it up:

Step 1: Deploy a "Shared API Gateway" Base Service

First, create a minimal Serverless service whose sole job is to define the core API Gateway resource (no Lambda functions attached). This will be the foundation all your other services attach to.

In its serverless.yml:

service: api-shared
provider:
  name: aws
  runtime: nodejs18.x
  stage: prod
  region: us-east-1

resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: SharedMainAPI

outputs:
  RestApiId:
    Value: !Ref ApiGatewayRestApi
    Export:
      Name: ${self:service}-rest-api-id
  RootResourceId:
    Value: !GetAtt ApiGatewayRestApi.RootResourceId
    Export:
      Name: ${self:service}-root-resource-id

Deploy this service first—it will create the API Gateway and export its IDs for other services to reference.

Step 2: Attach Child Services to the Shared API Gateway

In each of your other Serverless services (e.g., user-service, order-service), update the provider section to reference the shared API Gateway’s IDs. This tells Serverless not to create a new API Gateway for the service, but instead add endpoints to the existing one.

Example serverless.yml for a child service:

service: user-service
provider:
  name: aws
  runtime: nodejs18.x
  stage: prod
  region: us-east-1
  apiGateway:
    restApiId: ${cf:api-shared.RestApiId} # Reference the shared API's ID
    restApiRootResourceId: ${cf:api-shared.RootResourceId} # Reference the root resource ID

functions:
  getUsers:
    handler: handler.getUsers
    events:
      - http:
          path: /users
          method: get

When you deploy this child service, its /users endpoint will be added to the shared API Gateway instead of creating a new one. Repeat this for all your services, and you’ll keep everything under one API Gateway while splitting your CloudFormation stacks.

Approach 2: Unify Access with a Custom Domain (Separate API Gateways)

If you prefer to keep separate API Gateways for each service (maybe for better isolation), you can use a custom domain to map different paths to each API Gateway. This gives users a single entry point while keeping your stacks split.

Step 1: Set Up a Custom Domain in AWS

First, you’ll need a custom domain (e.g., api.yourdomain.com) and an SSL certificate from AWS Certificate Manager (ACM) for that domain. Make sure the certificate is in the same region as your API Gateways.

Step 2: Use the Serverless Domain Manager Plugin

The serverless-domain-manager plugin automates the process of setting up base path mappings between your custom domain and each service’s API Gateway.

  1. Install the plugin in each child service:
npm install serverless-domain-manager --save-dev
  1. Add the configuration to each service’s serverless.yml:
service: order-service
provider:
  name: aws
  runtime: nodejs18.x
  stage: prod
  region: us-east-1

plugins:
  - serverless-domain-manager

custom:
  customDomain:
    domainName: api.yourdomain.com
    basePath: orders # This path maps to this service's API Gateway
    stage: ${self:provider.stage}
    createRoute53Record: true # If you're using Route53 for DNS

Repeat this for each service, using a unique basePath (e.g., /users for the user service, /orders for the order service). When you deploy each service, the plugin will create a base path mapping in API Gateway, so users can access:

  • api.yourdomain.com/users → User service’s API Gateway
  • api.yourdomain.com/orders → Order service’s API Gateway

Key Notes to Keep in Mind

  • Permissions: When sharing an API Gateway, ensure each child service’s Lambda functions have the necessary permissions to be invoked by API Gateway (the Serverless Framework usually handles this automatically, but double-check if you have custom IAM roles).
  • Resource Limits: Even with a shared API Gateway, you’ll still need to split your stacks by business domain (e.g., users, orders) to stay under CloudFormation’s 200-resource limit per stack.
  • Custom Domain Certificates: Make sure your ACM certificate covers all regions where your API Gateways are deployed, or use a regional certificate for each region.

内容的提问来源于stack exchange,提问作者Jesuspc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:11:19