mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-05-03 07:22:27 +00:00
97583a78c2
This PR adds a new example demonstrating how to build a Lambda function that handles requests from multiple sources (Application Load Balancer and API Gateway V2) using a single handler. ### What's New **New Example: `Examples/MultiSourceAPI`** A Lambda function that: - Implements `StreamingLambdaHandler` to accept raw `ByteBuffer` events - Dynamically decodes events as either `ALBTargetGroupRequest` or `APIGatewayV2Request` - Returns appropriate responses based on the detected event source - Demonstrates handling multiple AWS service integrations with a single function ### Key Features - **Type-safe event detection**: Uses Swift's `Decodable` to identify the event source at runtime - **Streaming response**: Implements `StreamingLambdaHandler` for efficient response handling - **Complete deployment**: Includes SAM template with both ALB and API Gateway V2 infrastructure - **Production-ready**: Full VPC setup with subnets, security groups, and load balancer configuration ### Files Added - `Examples/MultiSourceAPI/Sources/main.swift` - Lambda handler implementation - `Examples/MultiSourceAPI/Package.swift` - Swift package configuration - `Examples/MultiSourceAPI/template.yaml` - SAM deployment template with ALB and API Gateway V2 - `Examples/MultiSourceAPI/README.md` - Documentation with build, deploy, and test instructions - Updated CI to include the new example ### Use Case This pattern is useful when you want to: - Expose the same Lambda function through multiple AWS services - Reduce code duplication by handling similar requests from different sources - Maintain a single codebase for multi-channel APIs --------- Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu> Co-authored-by: Josh Elkins <jbelkins@users.noreply.github.com>
66 lines
1.6 KiB
Markdown
66 lines
1.6 KiB
Markdown
# Multi-Source API Example
|
|
|
|
This example demonstrates a Lambda function that handles requests from both Application Load Balancer (ALB) and API Gateway V2 by accepting a raw `ByteBuffer` and decoding the appropriate event type.
|
|
|
|
## Overview
|
|
|
|
The Lambda handler receives events as `ByteBuffer` and attempts to decode them as either:
|
|
- `ALBTargetGroupRequest` - for requests from Application Load Balancer
|
|
- `APIGatewayV2Request` - for requests from API Gateway V2
|
|
|
|
Based on the successfully decoded type, it returns an appropriate response.
|
|
|
|
## Building
|
|
|
|
```bash
|
|
swift package archive --allow-network-connections docker
|
|
```
|
|
|
|
## Deploying
|
|
|
|
Deploy using SAM:
|
|
|
|
```bash
|
|
sam deploy \
|
|
--resolve-s3 \
|
|
--template-file template.yaml \
|
|
--stack-name MultiSourceAPI \
|
|
--capabilities CAPABILITY_IAM
|
|
```
|
|
|
|
## Testing
|
|
|
|
After deployment, SAM will output two URLs:
|
|
|
|
### Test API Gateway V2:
|
|
```bash
|
|
curl https://<api-id>.execute-api.<region>.amazonaws.com/apigw/test
|
|
```
|
|
|
|
Expected response:
|
|
```json
|
|
{"source":"APIGatewayV2","path":"/apigw/test"}
|
|
```
|
|
|
|
### Test ALB:
|
|
```bash
|
|
curl http://<alb-dns-name>/alb/test
|
|
```
|
|
|
|
Expected response:
|
|
```json
|
|
{"source":"ALB","path":"/alb/test"}
|
|
```
|
|
|
|
## How It Works
|
|
|
|
The handler uses Swift's type-safe decoding to determine the event source:
|
|
|
|
1. Receives raw `ByteBuffer` event
|
|
2. Attempts to decode as `ALBTargetGroupRequest`
|
|
3. If that fails, attempts to decode as `APIGatewayV2Request`
|
|
4. Returns appropriate response based on the decoded type
|
|
5. Throws error if neither decoding succeeds
|
|
|
|
This pattern is useful when a single Lambda function needs to handle requests from multiple sources.
|