mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-05-03 07:22:27 +00:00
2abe7eb7de
Address https://github.com/awslabs/swift-aws-lambda-runtime/issues/605 NEW Lambda Tenant isolation capability: https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation.html # Add Support for Lambda Tenant Isolation Mode ## Summary This PR adds support for AWS Lambda's tenant isolation mode to the Swift AWS Lambda Runtime, enabling developers to build multi-tenant applications with strict execution environment isolation per tenant. ## Changes ### Runtime Support - Added `tenantID` property to `LambdaContext` to expose the tenant identifier - Extended `InvocationMetadata` to capture the `Lambda-Runtime-Aws-Tenant-Id` header - Added `AmazonHeaders.tenantID` constant for the tenant ID header - Added trace logging for invocation headers to aid debugging ### New Example: MultiTenant A complete working example demonstrating tenant isolation mode: - **Request tracking system** that maintains separate counters and histories per tenant - **Actor-based storage** (`TenantDataStore`) for thread-safe tenant data management - **Immutable data structures** (`TenantData`) following Swift best practices - **API Gateway integration** with tenant ID passed via query parameter - **SAM template** configured with `TenancyConfig.TenantIsolationMode: PER_TENANT` - **Comprehensive documentation** covering architecture, deployment, testing, and best practices ### Testing - Added unit test for tenant ID extraction from invocation headers - Integrated MultiTenant example into CI/CD pipeline ### Documentation The example includes detailed documentation on: - When to use tenant isolation (user code execution, sensitive data processing) - How tenant isolation works (dedicated environments, no cross-tenant reuse) - Concurrency limits and scaling considerations - Pricing implications - Security best practices - CloudWatch monitoring with tenant dimensions ## Files Changed - `Sources/AWSLambdaRuntime/LambdaContext.swift` - Added tenantID property - `Sources/AWSLambdaRuntime/ControlPlaneRequest.swift` - Capture tenant ID from headers - `Sources/AWSLambdaRuntime/Utils.swift` - Added tenantID header constant - `Sources/AWSLambdaRuntime/Lambda.swift` - Pass tenant ID to context - `Sources/AWSLambdaRuntime/LambdaRuntimeClient+ChannelHandler.swift` - Added trace logging - `Tests/AWSLambdaRuntimeTests/InvocationTests.swift` - Added tenant ID test - `Examples/MultiTenant/*` - New complete example with SAM template - `.github/workflows/pull_request.yml` - Added MultiTenant to CI pipeline ## Testing Instructions 1. Build and deploy the example: bash cd Examples/MultiTenant swift package archive --allow-network-connections docker sam deploy --guided 2. Test with different tenants: bash curl "https://<api-id>.execute-api.<region>.amazonaws.com/Prod?tenant-id= alice" curl "https://<api-id>.execute-api.<region>.amazonaws.com/Prod?tenant-id= bob" 3. Verify isolation by checking that each tenant maintains separate request counts ## Related Documentation - [AWS Lambda Tenant Isolation](https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation.html) - [AWS Blog: Streamlined Multi-Tenant Application Development](https://aws.amazon.com/blogs/aws/streamlined-multi-tenant-application-development-with-tenant-isolation-mode-in-aws-lambda/) --------- Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu> Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
122 lines
5.0 KiB
YAML
122 lines
5.0 KiB
YAML
AWSTemplateFormatVersion: '2010-09-09'
|
|
Transform: AWS::Serverless-2016-10-31
|
|
Description: SAM Template for Multi Tenant Lambda Example
|
|
|
|
# This is an example SAM template for the purpose of this project.
|
|
# When deploying such infrastructure in production environment,
|
|
# we strongly encourage you to follow these best practices for improved security and resiliency
|
|
# - Enable access logging on API Gateway
|
|
# See: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html)
|
|
# - Ensure that AWS Lambda function is configured for function-level concurrent execution limit
|
|
# See: https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html
|
|
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html
|
|
# - Check encryption settings for Lambda environment variable
|
|
# See: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html
|
|
# - Ensure that AWS Lambda function is configured for a Dead Letter Queue(DLQ)
|
|
# See: https://docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html#invocation-dlq
|
|
# - Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources
|
|
# See: https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html
|
|
# Code Example: https://github.com/awslabs/swift-aws-lambda-runtime/tree/main/Examples/ServiceLifecycle%2BPostgres
|
|
|
|
Resources:
|
|
# API Gateway REST API
|
|
MultiTenantApi:
|
|
Type: AWS::Serverless::Api
|
|
Properties:
|
|
StageName: Prod
|
|
DefinitionBody:
|
|
openapi: 3.0.1
|
|
info:
|
|
title: MultiTenant API
|
|
version: 1.0.0
|
|
paths:
|
|
/{proxy+}:
|
|
x-amazon-apigateway-any-method:
|
|
parameters:
|
|
- name: tenant-id
|
|
in: query
|
|
required: true
|
|
schema:
|
|
type: string
|
|
- name: proxy
|
|
in: path
|
|
required: true
|
|
schema:
|
|
type: string
|
|
x-amazon-apigateway-request-validator: params-only
|
|
x-amazon-apigateway-integration:
|
|
type: aws_proxy
|
|
httpMethod: POST
|
|
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MultiTenantLambda.Arn}/invocations
|
|
requestParameters:
|
|
integration.request.header.X-Amz-Tenant-Id: method.request.querystring.tenant-id
|
|
/:
|
|
x-amazon-apigateway-any-method:
|
|
parameters:
|
|
- name: tenant-id
|
|
in: query
|
|
required: true
|
|
schema:
|
|
type: string
|
|
x-amazon-apigateway-request-validator: params-only
|
|
x-amazon-apigateway-integration:
|
|
type: aws_proxy
|
|
httpMethod: POST
|
|
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MultiTenantLambda.Arn}/invocations
|
|
requestParameters:
|
|
integration.request.header.X-Amz-Tenant-Id: method.request.querystring.tenant-id
|
|
x-amazon-apigateway-request-validators:
|
|
params-only:
|
|
validateRequestParameters: true
|
|
validateRequestBody: false
|
|
|
|
# Lambda function
|
|
MultiTenantLambda:
|
|
Type: AWS::Serverless::Function
|
|
Properties:
|
|
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MultiTenant/MultiTenant.zip
|
|
Timeout: 60
|
|
Handler: swift.bootstrap # ignored by the Swift runtime
|
|
Runtime: provided.al2023
|
|
MemorySize: 128
|
|
Architectures:
|
|
- arm64
|
|
# https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation-configure.html#tenant-isolation-cfn
|
|
TenancyConfig:
|
|
TenantIsolationMode: PER_TENANT
|
|
Environment:
|
|
Variables:
|
|
# by default, AWS Lambda runtime produces no log
|
|
# use `LOG_LEVEL: debug` for lifecycle and event handling information
|
|
# use `LOG_LEVEL: trace` for detailed input event information
|
|
LOG_LEVEL: trace
|
|
Events:
|
|
RootPath:
|
|
Type: Api
|
|
Properties:
|
|
RestApiId: !Ref MultiTenantApi
|
|
Path: /
|
|
Method: ANY
|
|
ProxyPath:
|
|
Type: Api
|
|
Properties:
|
|
RestApiId: !Ref MultiTenantApi
|
|
Path: /{proxy+}
|
|
Method: ANY
|
|
|
|
# Permission for API Gateway to invoke Lambda
|
|
MultiTenantLambdaPermission:
|
|
Type: AWS::Lambda::Permission
|
|
Properties:
|
|
FunctionName: !Ref MultiTenantLambda
|
|
Action: lambda:InvokeFunction
|
|
Principal: apigateway.amazonaws.com
|
|
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${MultiTenantApi}/*/*
|
|
|
|
Outputs:
|
|
# print API Gateway endpoint
|
|
APIGatewayEndpoint:
|
|
Description: API Gateway endpoint URL
|
|
# https://docs.aws.amazon.com/lambda/latest/dg/tenant-isolation-invoke.html#tenant-isolation-invoke-apigateway
|
|
Value: !Sub "https://${MultiTenantApi}.execute-api.${AWS::Region}.amazonaws.com/Prod?tenant-id=seb"
|