13 Commits

Author SHA1 Message Date
Sébastien Stormacq 97b2e6d017 Add support for Lambda functions exposed behind an Application Load Balancer (ALB) (#29)
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
2025-10-26 09:10:09 +01:00
Sébastien Stormacq 4f2e953097 Adjust license files for /awslabs (#28)
Change the legal files and the license header in source files according
to AWS standards
2025-10-21 21:06:59 +02:00
Sébastien Stormacq c72834d93c Update to support AWS Lambda Runtime for Swift v2 (#22)
- Update this library to support the upcoming release of AWS Lambda
Runtime for Swift v2.
This change affects the public API (some `Sendable` added) and requires
a major version bump. I propose to release v2 to align this library's
major with the Swift AWS Lambda Runtime's major.

- Add a QuoteAPI example in the `Examples` directory
2025-09-06 11:34:56 +02:00
Emlyn Murphy eec0a058f7 Fix Query String Parameters Being Ignored (#11)
* Put query string into the path for OpenAPIGenerator code

* minor comments and formatting changes

---------

Co-authored-by: Sébastien Stormacq <sebastien.stormacq@gmail.com>
2024-10-30 18:22:56 +01:00
Sebastien Stormacq a1caf89756 Update swift lambda event dependency to version 0.3.0 2024-05-23 11:01:48 +02:00
Sebastien Stormacq 0a4c77d7c8 make swift-format happy 2024-05-15 12:57:52 +02:00
Sebastien Stormacq 790e571e91 map Swift errors to HTTP return codes (https://github.com/sebsto/swift-openapi-lambda/issues/2) 2023-12-14 08:08:22 -05:00
Sebastien Stormacq b2fa4ccf00 rename LambdaOpenAPITransport to OpenAPILambdaTransport for consistency 2023-12-13 20:41:26 -05:00
Sebastien Stormacq 2c177eb1a3 add correct license header to each file 2023-12-13 18:24:01 -05:00
Sebastien Stormacq 138c608975 comply to soundness checks 2023-12-13 18:14:16 -05:00
Sebastien Stormacq ef2e29fc26 simplify code for API Gateway V2 (HTTP API) 2023-12-13 16:46:45 -05:00
Sebastien Stormacq e522a27ac1 cleanup 2023-12-13 12:28:07 -05:00
Sebastien Stormacq 4c84d010a2 initial commit 2023-12-12 21:51:48 -05:00