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>
139 lines
3.5 KiB
YAML
139 lines
3.5 KiB
YAML
AWSTemplateFormatVersion: '2010-09-09'
|
|
Transform: AWS::Serverless-2016-10-31
|
|
Description: Multi-source API Lambda function with ALB and API Gateway V2
|
|
|
|
Resources:
|
|
MultiSourceAPIFunction:
|
|
Type: AWS::Serverless::Function
|
|
Properties:
|
|
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MultiSourceAPI/MultiSourceAPI.zip
|
|
Handler: provided
|
|
Runtime: provided.al2
|
|
Architectures:
|
|
- arm64
|
|
MemorySize: 256
|
|
Timeout: 30
|
|
Environment:
|
|
Variables:
|
|
LOG_LEVEL: trace
|
|
Events:
|
|
ApiGatewayEvent:
|
|
Type: HttpApi
|
|
Properties:
|
|
Path: /{proxy+}
|
|
Method: ANY
|
|
|
|
# VPC for ALB
|
|
VPC:
|
|
Type: AWS::EC2::VPC
|
|
Properties:
|
|
CidrBlock: 10.0.0.0/16
|
|
EnableDnsHostnames: true
|
|
EnableDnsSupport: true
|
|
|
|
PublicSubnet1:
|
|
Type: AWS::EC2::Subnet
|
|
Properties:
|
|
VpcId: !Ref VPC
|
|
CidrBlock: 10.0.1.0/24
|
|
AvailabilityZone: !Select [0, !GetAZs '']
|
|
MapPublicIpOnLaunch: true
|
|
|
|
PublicSubnet2:
|
|
Type: AWS::EC2::Subnet
|
|
Properties:
|
|
VpcId: !Ref VPC
|
|
CidrBlock: 10.0.2.0/24
|
|
AvailabilityZone: !Select [1, !GetAZs '']
|
|
MapPublicIpOnLaunch: true
|
|
|
|
InternetGateway:
|
|
Type: AWS::EC2::InternetGateway
|
|
|
|
AttachGateway:
|
|
Type: AWS::EC2::VPCGatewayAttachment
|
|
Properties:
|
|
VpcId: !Ref VPC
|
|
InternetGatewayId: !Ref InternetGateway
|
|
|
|
RouteTable:
|
|
Type: AWS::EC2::RouteTable
|
|
Properties:
|
|
VpcId: !Ref VPC
|
|
|
|
Route:
|
|
Type: AWS::EC2::Route
|
|
DependsOn: AttachGateway
|
|
Properties:
|
|
RouteTableId: !Ref RouteTable
|
|
DestinationCidrBlock: 0.0.0.0/0
|
|
GatewayId: !Ref InternetGateway
|
|
|
|
SubnetRouteTableAssociation1:
|
|
Type: AWS::EC2::SubnetRouteTableAssociation
|
|
Properties:
|
|
SubnetId: !Ref PublicSubnet1
|
|
RouteTableId: !Ref RouteTable
|
|
|
|
SubnetRouteTableAssociation2:
|
|
Type: AWS::EC2::SubnetRouteTableAssociation
|
|
Properties:
|
|
SubnetId: !Ref PublicSubnet2
|
|
RouteTableId: !Ref RouteTable
|
|
|
|
# Application Load Balancer
|
|
ALBSecurityGroup:
|
|
Type: AWS::EC2::SecurityGroup
|
|
Properties:
|
|
GroupDescription: Security group for ALB
|
|
VpcId: !Ref VPC
|
|
SecurityGroupIngress:
|
|
- IpProtocol: tcp
|
|
FromPort: 80
|
|
ToPort: 80
|
|
CidrIp: 0.0.0.0/0
|
|
|
|
ApplicationLoadBalancer:
|
|
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
|
|
Properties:
|
|
Scheme: internet-facing
|
|
Subnets:
|
|
- !Ref PublicSubnet1
|
|
- !Ref PublicSubnet2
|
|
SecurityGroups:
|
|
- !Ref ALBSecurityGroup
|
|
|
|
ALBTargetGroup:
|
|
Type: AWS::ElasticLoadBalancingV2::TargetGroup
|
|
DependsOn: ALBLambdaInvokePermission
|
|
Properties:
|
|
TargetType: lambda
|
|
Targets:
|
|
- Id: !GetAtt MultiSourceAPIFunction.Arn
|
|
|
|
ALBListener:
|
|
Type: AWS::ElasticLoadBalancingV2::Listener
|
|
Properties:
|
|
LoadBalancerArn: !Ref ApplicationLoadBalancer
|
|
Port: 80
|
|
Protocol: HTTP
|
|
DefaultActions:
|
|
- Type: forward
|
|
TargetGroupArn: !Ref ALBTargetGroup
|
|
|
|
ALBLambdaInvokePermission:
|
|
Type: AWS::Lambda::Permission
|
|
Properties:
|
|
FunctionName: !GetAtt MultiSourceAPIFunction.Arn
|
|
Action: lambda:InvokeFunction
|
|
Principal: elasticloadbalancing.amazonaws.com
|
|
|
|
Outputs:
|
|
ApiGatewayUrl:
|
|
Description: API Gateway endpoint URL
|
|
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
|
|
|
|
ALBUrl:
|
|
Description: Application Load Balancer URL
|
|
Value: !Sub "http://${ApplicationLoadBalancer.DNSName}"
|