Skip to content

Use the main API gateway ​

This guide will walk you through moving your service from its own standalone API Gateway to the central, shared one. The main benefits are less boilerplate code for you to manage and a more consistent, secure setup for everyone. This process is based on the migration of the kudos service, which you can see in the backend service changes MR and the corresponding frontend changes MR.

You'll be making changes in both the backend service and the frontend client.


Before You Start: Is Migration Right for Your Service? ​

This migration process is designed for services that use standard JWT authentication via our shared Cognito authorizer.

If your service requires a special configuration on its API Gateway, do not migrate it. Keep its existing standalone gateway.

A key example is the ai-scribe service. It uses a custom Lambda authorizer (an x-api-key check) and therefore is not a candidate for this migration. Before you begin, check your service's application.yml to ensure it doesn't have any unique requirements that would conflict with the shared gateway.


Step 1: Clean Up Your Template ​

First, you need to delete all the old, service-specific API Gateway resources from your service's application.yml file.

You'll be removing:

  • The HttpApi Resource: Delete the entire AWS::Serverless::HttpApi resource. This is the main thing you're replacing.

  • The ACMCertificate Resource: Your service doesn't need to manage its own SSL certificate anymore.

  • DNS/Domain Mappings: Get rid of any Mappings related to Route53 or frontend domains. The shared gateway handles all of this now.

  • Old Lambda Event Triggers: Find the Events section inside each of your Lambda function resources and delete it completely. This section ties your function to the old, standalone HttpApi. For example, this entire Events block was removed from the KudosQuerierFunction:

    yaml
    # In your service's application.yml
    
    KudosQuerierFunction:
      Type: AWS::Serverless::Function
      Properties:
        # ... other function properties ...
    
      # ✂️ DELETE THIS ENTIRE BLOCK
      Events:
        ListKudos:
          Type: HttpApi
          Properties:
            Path: /kudos
            Method: get
            ApiId: !Ref HttpApi
        ListAndiKudos:
          Type: HttpApi
          Properties:
            Path: /andis/{andiId}/kudos
            Method: get
            ApiId: !Ref HttpApi

Step 2: Add New Connections ​

Now you'll add the new resources to your application.yml to connect your functions to the shared gateway.

Add the Stack Parameter ​

First, add this parameter to the Parameters section at the top of your file. This lets your service find the shared gateway.

yaml
Parameters:
  ApiGatewayInfrastructureStackName:
    Type: String

Naming Your New Resources ​

It's a good practice to follow a clear naming convention for the new resources you add.

  • Integration & Permission: Name these after the function they are associated with. For example, KudosQuerierIntegration and KudosQuerierPermission.
  • Route: Name the Route resource by converting its RouteKey to PascalCase and appending Route. For example, a RouteKey of 'POST /users/{userId}' should have a resource name like PostUsersRoute.

You can see this pattern in the kudos examples below.

Connect Each Endpoint ​

For each Lambda function that needs to be exposed, you'll add an Integration and a Permission. Then, for each specific path that function handles, you'll add a Route.

Here are the concrete examples from the kudos service.

Example 1: A function with multiple GET routes (KudosQuerierFunction)

yaml
#### 1. The Integration (connects gateway to the Lambda)
KudosQuerierIntegration:
  Type: AWS::ApiGatewayV2::Integration
  Properties:
    ApiId:
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId
    IntegrationType: AWS_PROXY
    IntegrationUri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${KudosQuerierFunction.Arn}/invocations
    PayloadFormatVersion: '2.0'

#### 2. The Routes (the public URLs for the function)
GetKudosRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId:
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId
    AuthorizationType: JWT
    AuthorizerId: 
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-CognitoAuthorizerId
    RouteKey: 'GET /kudos'
    Target: !Join ['/', ['integrations', !Ref KudosQuerierIntegration]]

GetAndiKudosRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId:
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId
    AuthorizationType: JWT
    AuthorizerId: 
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-CognitoAuthorizerId
    RouteKey: 'GET /andis/{andiId}/kudos'
    Target: !Join ['/', ['integrations', !Ref KudosQuerierIntegration]]

#### 3. The Permission (lets the gateway run the Lambda)
KudosQuerierPermission:
  Type: AWS::Lambda::Permission
  Properties:
    FunctionName: !GetAtt KudosQuerierFunction.Arn
    Action: lambda:InvokeFunction
    Principal: apigateway.amazonaws.com
    SourceArn: !Sub 
      - "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${HttpApiId}/*/*"
      - HttpApiId:
          Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId

Example 2: A function with a single POST route (KudosCommandsFunction)

yaml
KudosCommandsIntegration:
  Type: AWS::ApiGatewayV2::Integration
  Properties:
    ApiId:
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId
    IntegrationType: AWS_PROXY
    IntegrationUri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${KudosCommandsFunction.Arn}/invocations
    PayloadFormatVersion: '2.0'

PostAndiKudosRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId:
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId
    AuthorizationType: JWT
    AuthorizerId: 
      Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-CognitoAuthorizerId
    RouteKey: 'POST /andis/{andiId}/kudos'
    Target: !Join ['/', ['integrations', !Ref KudosCommandsIntegration]]

KudosCommandsPermission:
  Type: AWS::Lambda::Permission
  Properties:
    FunctionName: !GetAtt KudosCommandsFunction.Arn
    Action: lambda:InvokeFunction
    Principal: apigateway.amazonaws.com
    SourceArn: !Sub 
      - "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${HttpApiId}/*/*"
      - HttpApiId:
          Fn::ImportValue: !Sub ${ApiGatewayInfrastructureStackName}-HttpApiId

Step 3: Update Your SAM Config ​

The final step for the service is to update your samconfig.toml file. You need to add the new ApiGatewayInfrastructureStackName to the parameter_overrides list for every environment defined in the file.

The value should follow the pattern "<environment-name>-apigateway-infrastructure".

Example for a staging environment:

toml
[staging.deploy.parameters]
...
parameter_overrides = [
  "Environment=\"staging\"",
  ...
  "ApiGatewayInfrastructureStackName=\"staging-apigateway-infrastructure\"" # <-- Add this line
]

Step 4: Update the Frontend Client ​

Finally, you need to tell the luna-front-end client how to call your service at its new address. The key is to stop building a service-specific subdomain and instead use the main V3 API domain.

Update Your Local Environment (.env.local) ​

For the changes to work during local development, you'll need to update your personal .env.local file. Add the new VITE_APP_API_DOMAIN_V3 variable and point it to the development environment you're working against.

# .env.local

VITE_APP_API_DOMAIN_V3='api.v3.polaris.dev.luna.and.sh'

Update Your Service's API File ​

The crucial change is in your service's specific API file, for example src/utils/api/kudos.api.js. You must change how the domain for API calls is defined.

You'll change the kudosDomain constant to point to the new shared domain.

Before: The old way constructed a unique subdomain for the kudos service.

javascript
const kudosDomain = `${config.api.services.kudos.prefix}.${config.api.domainV2}`;

After: The new way points directly to the shared V3 gateway domain. The gateway itself knows how to route /kudos requests to the correct service.

javascript
const kudosDomain = config.api.domainV3;

After making this change, the frontend will correctly call your service via the new shared API gateway. ✅

Made with ❤️ by the Luna team