Files
swift-aws-lambda-runtime/Examples/MultiSourceAPI
Sébastien Stormacq ddad9b9bee Use published dependency by default in examples (#665)
## Summary

Examples now default to the published `swift-aws-lambda-runtime` package
from GitHub, so they work out of the box when cloned standalone (as
described in the READMEs).

CI scripts swap the dependency to the local path (`../..`) before
building, ensuring we still test against the current branch.

### Changes

- **`Examples/APIGatewayV2/Package.swift`** — Default to remote URL,
local path is commented out with clear instructions.
- **`.github/workflows/scripts/use-local-deps.sh`** (new) — Shared
script that rewrites Package.swift to use the local dependency.
- **`integration_tests.sh`**, **`check-archive-plugin.sh`**,
**`check-link-foundation.sh`** — Source `use-local-deps.sh` before
building.
2026-05-11 21:38:11 +02:00
..
2025-11-02 21:58:02 +01:00
2025-11-02 21:58:02 +01:00

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

swift package archive --allow-network-connections docker

Deploying

Deploy using SAM:

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:

curl https://<api-id>.execute-api.<region>.amazonaws.com/apigw/test

Expected response:

{"source":"APIGatewayV2","path":"/apigw/test"}

Test ALB:

curl http://<alb-dns-name>/alb/test

Expected response:

{"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.