- 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
+------------------+
| 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.
- 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.
- Deploy Lambda microservices
We create two simple Lambda functions (Service1 and Service2), each returning a unique response.
- Create API Gateway HTTP API
Define routes /service1 and /service2 pointing to the respective Lambda integrations.
- (Optional) Configure Route 53 DNS
Set up a custom domain for the API Gateway.
- (Optional) Setup VPC Endpoint / PrivateLink
For private networking if microservices need access to internal resources.
# 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'
}
}| Path | Integration |
|---|---|
| /service1 | Lambda Service1 |
| /service2 | Lambda Service2 |
- 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/service2You should see respective JSON responses.
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',
});
}
}- Install AWS CDK (if not installed):
npm install -g aws-cdk-
Create a new CDK project and replace the
lib/microservice-routing-stack.tscontent with the above code. -
Bootstrap and deploy:
cdk bootstrap
cdk deploy- Use the output API endpoint to test routes.
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!