## Overview This PR reorganizes and enhances the streaming Lambda examples by splitting them into two distinct examples that demonstrate different invocation methods: 1. **Streaming+FunctionUrl** - Streaming responses via Lambda Function URLs 2. **Streaming+APIGateway** - Streaming responses via API Gateway REST API ## Changes ### 🔄 Restructured Examples - **Renamed**: `Examples/Streaming/` → `Examples/Streaming+FunctionUrl/` - Maintains the original streaming example using Lambda Function URLs - Updated documentation to clarify Function URL-specific configuration - Improved AWS credentials handling in curl examples - **New**: `Examples/Streaming+APIGateway/` - Comprehensive example demonstrating API Gateway REST API with response streaming - Complete SAM template with proper IAM roles and streaming configuration - Detailed documentation covering API Gateway-specific setup ### 📚 Documentation Improvements #### Streaming+FunctionUrl - Clarified that this example uses Lambda Function URLs - Updated curl examples to use `eval $(aws configure export-credentials --format env)` for cleaner credential handling - Maintained all existing functionality and deployment instructions #### Streaming+APIGateway (New) - **316-line comprehensive README** covering: - Response streaming concepts and benefits - HTTP status code and header configuration - Streaming response body patterns - Local testing instructions - Complete SAM deployment guide with detailed template explanation - API Gateway-specific invocation with AWS Sigv4 authentication - Payload format documentation with example JSON - Security and reliability best practices - How API Gateway streaming works under the hood ### 🛠️ Technical Details #### API Gateway Streaming Configuration The new example demonstrates: - Special Lambda URI: `/response-streaming-invocations` endpoint - `responseTransferMode: STREAM` configuration - IAM role with both `lambda:InvokeFunction` and `lambda:InvokeWithResponseStream` permissions - Proper timeout configuration (60s) to accommodate streaming duration #### SAM Template Features ```yaml - Lambda function with streaming support (arm64, provided.al2) - API Gateway REST API with OpenAPI 3.0 definition - IAM execution role for API Gateway to invoke Lambda with streaming - Complete outputs for easy testing (API URL and Lambda ARN) ``` ### 🔐 Security Enhancements Both examples now include comprehensive security best practices: - API Gateway access logging - Throttling configuration - AWS WAF integration recommendations - Lambda concurrent execution limits - Environment variable encryption - Dead Letter Queue (DLQ) configuration - VPC configuration guidance ### 🧪 Testing Both examples support: - **Local testing**: `swift run` with curl invocation on port 7000 - **AWS deployment**: Complete SAM templates with deployment instructions - **Authenticated invocation**: AWS Sigv4 examples with proper credential handling ## Benefits 1. **Clearer separation**: Developers can now easily choose between Function URLs and API Gateway based on their use case 2. **Better documentation**: Each example has tailored documentation for its specific invocation method 3. **Production-ready**: Includes security best practices and proper IAM configuration 4. **Easier testing**: Improved credential handling in curl examples ## Breaking Changes None - this is purely additive. The original streaming example is preserved as `Streaming+FunctionUrl`. ## Testing Checklist - [x] Local testing works for both examples - [x] SAM deployment templates are valid - [x] Documentation is comprehensive and accurate - [x] Security best practices are documented - [x] Curl examples work with proper authentication ## Related Documentation - [AWS Lambda Response Streaming](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html) - [API Gateway Lambda Proxy Integration with Streaming](https://docs.aws.amazon.com/apigateway/latest/developerguide/response-streaming-lambda-configure.html) - [Lambda Function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) EOF --------- Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
This directory contains example code for Lambda functions.
Pre-requisites
-
Ensure you have the Swift 6.x toolchain installed. You can install Swift toolchains from Swift.org
-
When developing on macOS, be sure you use macOS 15 (Sequoia) or a more recent macOS version.
-
To build and archive your Lambda functions, you need to install docker.
-
To deploy your Lambda functions and invoke them, you must have an AWS account and install and configure the
awscommand line. -
Some examples are using AWS SAM. Install the SAM CLI before deploying these examples.
-
Some examples are using the AWS CDK. Install the CDK CLI before deploying these examples.
Examples
-
API Gateway V1: an REST API with Amazon API Gateway and a Lambda function as backend (requires AWS SAM).
-
API Gateway V2: an HTTPS API with Amazon API Gateway and a Lambda function as backend (requires AWS SAM).
-
API Gateway V2 with Lambda Authorizer: an HTTPS API with Amazon API Gateway protected by a Lambda authorizer (requires AWS SAM).
-
BackgroundTasks: a Lambda function that continues to run background tasks after having sent the response (requires AWS CLI).
-
CDK: a simple example of an AWS Lambda function invoked through an Amazon API Gateway and deployed with the Cloud Development Kit (CDK).
-
HelloJSON: a Lambda function that accepts JSON as an input parameter and responds with a JSON output (requires AWS CLI).
-
HelloWorld: a simple Lambda function (requires AWS CLI).
-
Hummingbird: a Lambda function using the Hummingbird web framework with API Gateway integration (requires AWS SAM).
-
S3EventNotifier: a Lambda function that receives object-upload notifications from an Amazon S3 bucket.
-
S3_AWSSDK: a Lambda function that uses the AWS SDK for Swift to invoke an Amazon S3 API (requires AWS SAM).
-
S3_Soto: a Lambda function that uses Soto to invoke an Amazon S3 API (requires AWS SAM).
-
Streaming with APIGateway: create a Lambda function exposed by a REST API Gateway. The Lambda function streams its response over time. (requires AWS SAM).
-
Streaming with Function Url: create a Lambda function exposed as an URL. The Lambda function streams its response over time. (requires AWS SAM).
-
Streaming+Codable: a Lambda function that combines JSON input decoding with response streaming capabilities, demonstrating a streaming codable interface (requires AWS SAM or the AWS CLI).
-
Testing: a test suite for Lambda functions.
AWS Credentials and Signature
This section is a short tutorial on the AWS Signature protocol and the AWS credentials.
What is AWS SigV4?
AWS SigV4, short for "Signature Version 4," is a protocol AWS uses to authenticate and secure requests. When you, as a developer, send a request to an AWS service, AWS SigV4 makes sure the request is verified and hasn’t been tampered with. This is done through a digital signature, which is created by combining your request details with your secret AWS credentials. This signature tells AWS that the request is genuine and is coming from a user who has the right permissions.
How to Obtain AWS Access Keys and Session Tokens
To start making authenticated requests with AWS SigV4, you’ll need three main pieces of information:
-
Access Key ID: This is a unique identifier for your AWS account, IAM (Identity and Access Management) user, or federated user.
-
Secret Access Key: This is a secret code that only you and AWS know. It works together with your access key ID to sign requests.
-
Session Token (Optional): If you're using temporary security credentials, AWS will also provide a session token. This is usually required if you're using temporary access (e.g., through AWS STS, which provides short-lived, temporary credentials, or for federated users).
To obtain these keys, you need an AWS account:
-
Sign up or Log in to AWS Console: Go to the AWS Management Console, log in, or create an AWS account if you don’t have one.
-
Create IAM User: In the console, go to IAM (Identity and Access Management) and create a new user. Ensure you set permissions that match what the user will need for your application (e.g., permissions to access specific AWS services, such as AWS Lambda).
-
Generate Access Key and Secret Access Key: In the IAM user credentials section, find the option to generate an "Access Key" and "Secret Access Key." Save these securely! You’ll need them to authenticate your requests.
-
(Optional) Generate Temporary Security Credentials: If you’re using temporary credentials (which are more secure for short-term access), use AWS Security Token Service (STS). You can call the
GetSessionTokenorAssumeRoleAPI to generate temporary credentials, including a session token.
With these in hand, you can use AWS SigV4 to securely sign your requests and interact with AWS services from your Swift app.
⚠️ Security and Reliability Notice
These are example applications for demonstration purposes. When deploying such infrastructure in production environments, we strongly encourage you to follow these best practices for improved security and resiliency:
- Enable access logging on API Gateway (documentation)
- Ensure that AWS Lambda function is configured for function-level concurrent execution limit (concurrency documentation, configuration guide)
- Check encryption settings for Lambda environment variables (documentation)
- Ensure that AWS Lambda function is configured for a Dead Letter Queue (DLQ) (documentation)
- Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources (documentation, code example)