mirror of
https://github.com/swift-server/swift-openapi-lambda.git
synced 2026-05-03 07:22:26 +00:00
97b2e6d017
This PR adds support for exposing Swift OpenAPI Lambda functions behind
an Application Load Balancer (ALB), providing an alternative to API
Gateway for HTTP routing to Lambda functions.
## Changes
### New ALB Support
- **OpenAPILambdaALB Protocol**: New protocol for ALB integration
alongside existing API Gateway support
- **ALB Event Handling**: Added `ALBTargetGroupRequest` and
`ALBTargetGroupResponse` support
- **HTTP Request Conversion**: Extension methods to convert ALB events
to/from HTTP requests/responses
### Core Library Updates
- **ALB-related source files**: New `/Sources/ALB/` directory with
ALB-specific implementations
- **Event Type Support**: Support for `ALBTargetGroupRequest` events
from Elastic Load Balancing
- **Response Mapping**: Proper mapping from OpenAPI responses to ALB
target group responses
### Complete ALB Example
- **QuoteAPI ALB Example**: Full working example in
`Examples/quoteapi-alb/`
- **Infrastructure as Code**: Complete SAM template with VPC, subnets,
security groups, and ALB
- **Build System**: Makefile and Docker build support for ALB deployment
- **Documentation**: Comprehensive README with ALB-specific deployment
instructions
### Key Files Added
```
Sources/ALB/
├── OpenAPILambdaALB.swift
└── ALBTargetGroup+HTTPRequest.swift
Examples/quoteapi-alb/
├── Package.swift
├── template.yaml
├── Makefile
├── README.md
├── Sources/QuoteAPI/QuoteService.swift
├── Sources/QuoteAPI/openapi.yaml
├── Sources/QuoteAPI/openapi-generator-config.yaml
└── events/GetQuote.json
```
## Usage
### Simple ALB Integration
```swift
@main
struct QuoteServiceALBImpl: APIProtocol, OpenAPILambdaALB {
func register(transport: OpenAPILambdaTransport) throws {
try self.registerHandlers(on: transport)
}
static func main() async throws {
let service = QuoteServiceALBImpl()
try await service.run()
}
// Your OpenAPI implementation...
}
```
### Key Differences from API Gateway
- Uses `OpenAPILambdaALB` instead of `OpenAPILambdaHttpApi`
- Handles `ALBTargetGroupRequest` events instead of
`APIGatewayV2Request`
- Returns `ALBTargetGroupResponse` instead of `APIGatewayV2Response`
- Requires VPC infrastructure (included in SAM template)
- No built-in authorization (implement via custom middleware if needed)
## Benefits
- **Cost Optimization**: ALB can be more cost-effective for high-traffic
applications
- **VPC Integration**: Native VPC support for private network access
- **Load Balancing**: Advanced load balancing features and health checks
- **WebSocket Support**: Future WebSocket support through ALB
- **Flexibility**: Choice between API Gateway and ALB based on use case
## Testing
- ✅ ALB example builds successfully with `sam build`
- ✅ Local testing with `sam local invoke`
- ✅ Complete infrastructure deployment via SAM
- ✅ HTTP requests properly routed through ALB to Lambda
- ✅ OpenAPI specification compatibility maintained
## Deployment
Deploy the ALB example:
```bash
cd Examples/quoteapi-alb
sam build && sam deploy --guided
```
Test the deployed endpoint:
```bash
curl http://[alb-dns-name]/stocks/AAPL
```
## Backward Compatibility
This is a purely additive change:
- Existing API Gateway implementations continue to work unchanged
- No breaking changes to existing APIs
- New ALB support is opt-in via protocol conformance
63 lines
2.2 KiB
Makefile
63 lines
2.2 KiB
Makefile
### Add functions here and link them to builder-bot format MUST BE "build-FunctionResourceName in template.yaml"
|
|
|
|
build-QuoteServiceALB: builder-bot
|
|
|
|
# Helper commands
|
|
build:
|
|
sam build
|
|
|
|
deploy:
|
|
sam deploy
|
|
|
|
logs:
|
|
sam logs --stack-name QuoteServiceALB
|
|
|
|
tail:
|
|
sam logs --stack-name QuoteServiceALB --tail
|
|
|
|
local:
|
|
swift run QuoteServiceALB
|
|
|
|
local-invoke:
|
|
sam local invoke QuoteServiceALB --event events/GetQuote.json
|
|
|
|
###################### No Change required below this line ##########################
|
|
|
|
builder-bot:
|
|
$(eval $@PRODUCT = $(subst build-,,$(MAKECMDGOALS)))
|
|
$(eval $@BUILD_DIR = $(PWD)/.aws-sam/build-swift)
|
|
$(eval $@STAGE = $($@BUILD_DIR)/lambda)
|
|
$(eval $@ARTIFACTS_DIR = $(PWD)/.aws-sam/build/$($@PRODUCT))
|
|
|
|
## Building from swift-openapi-lambda in a local directory (not from Github)
|
|
## 2. Change `Package.swift` dependency to path: "../.."
|
|
|
|
## 3. add /../.. to BUILD_SRC
|
|
$(eval $@BUILD_SRC = $(PWD)/../..)
|
|
## $(eval $@BUILD_SRC = $(PWD))
|
|
|
|
## 4. add `cd Examples/quoteapi-alb &&` to the docker BUILD_CMD
|
|
$(eval $@BUILD_CMD = "cd Examples/quoteapi-alb && swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
|
|
## $(eval $@BUILD_CMD = "swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
|
|
|
|
# build docker image to compile Swift for Linux
|
|
docker build -f Dockerfile . -t swift-builder
|
|
|
|
# prep directories
|
|
mkdir -p $($@BUILD_DIR)/lambda $($@ARTIFACTS_DIR)
|
|
|
|
# compile application inside Docker image using source code from local project folder
|
|
|
|
docker run --rm -v $($@BUILD_DIR):/build-target -v $($@BUILD_SRC):/build-src -w /build-src swift-builder bash -cl $($@BUILD_CMD)
|
|
|
|
# create lambda bootstrap file
|
|
docker run --rm -v $($@BUILD_DIR):/build-target -v `pwd`:/build-src -w /build-src swift-builder bash -cl "cd /build-target/lambda && ln -s $($@PRODUCT) /bootstrap"
|
|
|
|
# copy binary to stage
|
|
cp $($@BUILD_DIR)/release/$($@PRODUCT) $($@STAGE)/bootstrap
|
|
|
|
# copy resources to stage (if they exist)
|
|
[ -d "$($@BUILD_DIR)/release/$($@PRODUCT)_$($@PRODUCT).resources" ] && cp $($@BUILD_DIR)/release/$($@PRODUCT)_$($@PRODUCT).resources/* $($@STAGE) || true
|
|
|
|
# copy app from stage to artifacts dir
|
|
cp $($@STAGE)/* $($@ARTIFACTS_DIR)
|