Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save misskecupbung/887dd1be14f69589f115551eb8eea531 to your computer and use it in GitHub Desktop.

Select an option

Save misskecupbung/887dd1be14f69589f115551eb8eea531 to your computer and use it in GitHub Desktop.
A Deep Dive into AWS-native Microservice Routing

A Deep Dive into AWS-native Microservice Routing


What the guide will cover:

  • Intro to AWS-native microservice routing (no service mesh)
  • Key AWS components used:
    • API Gateway HTTP APIs for routing
    • AWS Lambda for microservices
    • VPC + PrivateLink for secure service communication (optional advanced)
    • Route 53 for DNS-based routing
    • ALB (Application Load Balancer) for HTTP routing to ECS or Lambda targets
  • Sample demo architecture diagram (described in text)
  • Example code snippets and commands
  • Complete AWS CDK code to deploy API Gateway, Lambda microservices, and routing

Architecture Summary

               +------------------+
               |   Route 53 DNS   |
               +--------+---------+
                        |
               +--------v---------+
               |  API Gateway     |
               |  HTTP API        |
               +--------+---------+
                        |
          +-------------+--------------+
          |                            |
   +------+-----+              +-------+------+
   | Lambda Svc1|              | Lambda Svc2  |
   +------------+              +--------------+

(Optional)
- Lambda services connect securely via VPC Endpoints or PrivateLink to databases or other VPC resources.

Key AWS Services Used

  • API Gateway HTTP API: Entry point, performs routing based on path and methods.
  • AWS Lambda: Microservices with isolated business logic.
  • Route 53: DNS management with custom domain names.
  • VPC Endpoints/PrivateLink: Secure, private networking between services and data stores.
  • ALB (optional): For ECS/Fargate microservices, enables HTTP routing and load balancing.

Implementations

  1. Deploy Lambda microservices

We create two simple Lambda functions (Service1 and Service2), each returning a unique response.

  1. Create API Gateway HTTP API

Define routes /service1 and /service2 pointing to the respective Lambda integrations.

  1. (Optional) Configure Route 53 DNS

Set up a custom domain for the API Gateway.

  1. (Optional) Setup VPC Endpoint / PrivateLink

For private networking if microservices need access to internal resources.


Lambda function code with Python (Example)

# Service1 Lambda (Python)
def handler(event, context):
    return {
        'statusCode': 200,
        'body': '{"message": "Hello from Service 1"}',
        'headers': {
            'Content-Type': 'application/json'
        }
    }

# Service2 Lambda (Python)
def handler(event, context):
    return {
        'statusCode': 200,
        'body': '{"message": "Hello from Service 2"}',
        'headers': {
            'Content-Type': 'application/json'
        }
    }

API Gateway routes (Example)

Path Integration
/service1 Lambda Service1
/service2 Lambda Service2

Running the demo

  • Deploy the stack using AWS CDK (see below)
  • Invoke API endpoints:
curl https://{api-id}.execute-api.{region}.amazonaws.com/service1
curl https://{api-id}.execute-api.{region}.amazonaws.com/service2

You should see respective JSON responses.


AWS CDK TypeScript Template

Below is the AWS CDK code to deploy the Lambda functions and API Gateway routing.

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigatewayv2 from 'aws-cdk-lib/aws-apigatewayv2';
import * as integrations from 'aws-cdk-lib/aws-apigatewayv2-integrations';

export class MicroserviceRoutingStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Lambda service 1 in Python
    const service1 = new lambda.Function(this, 'Service1Lambda', {
      runtime: lambda.Runtime.PYTHON_3_11,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`
def handler(event, context):
    return {
        'statusCode': 200,
        'body': '{"message": "Hello from Service 1"}',
        'headers': {
            'Content-Type': 'application/json'
        }
    }
      `),
    });

    // Lambda service 2 in Python
    const service2 = new lambda.Function(this, 'Service2Lambda', {
      runtime: lambda.Runtime.PYTHON_3_11,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`
def handler(event, context):
    return {
        'statusCode': 200,
        'body': '{"message": "Hello from Service 2"}',
        'headers': {
            'Content-Type': 'application/json'
        }
    }
      `),
    });

    // Create HTTP API Gateway
    const httpApi = new apigatewayv2.HttpApi(this, 'MicroserviceHttpApi', {
      apiName: 'MicroserviceRoutingAPI',
      createDefaultStage: true,
    });

    // Add routes to API Gateway
    httpApi.addRoutes({
      path: '/service1',
      methods: [apigatewayv2.HttpMethod.GET],
      integration: new integrations.HttpLambdaIntegration('Service1Integration', service1),
    });

    httpApi.addRoutes({
      path: '/service2',
      methods: [apigatewayv2.HttpMethod.GET],
      integration: new integrations.HttpLambdaIntegration('Service2Integration', service2),
    });

    new cdk.CfnOutput(this, 'ApiUrl', {
      value: httpApi.apiEndpoint ?? 'undefined',
      description: 'HTTP API Endpoint URL',
    });
  }
}

How to Deploy

  1. Install AWS CDK (if not installed):
npm install -g aws-cdk
  1. Create a new CDK project and replace the lib/microservice-routing-stack.ts content with the above code.

  2. Bootstrap and deploy:

cdk bootstrap
cdk deploy
  1. Use the output API endpoint to test routes.

Conclusion

This demo shows a simple but effective AWS-native microservice routing using API Gateway and Lambda. For the next step:

  • ECS services behind ALB for containerized microservices
  • VPC endpoints for private, secure access
  • Route 53 custom domains and advanced routing policies

Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment