Files
swift-aws-lambda-runtime/Examples/ManagedInstances/template.yaml
T
Sébastien Stormacq 190eb81876 Add support for Lambda Managed Instances without changing the public API [Convenience + Example] (#623)
This PR builds on
https://github.com/awslabs/swift-aws-lambda-runtime/pull/629 to add
convenience structs (Handlers and Adapters) that are `Sendable`

**Changes**

- **Added Sendable adapter types**: Implemented `ClosureHandlerSendable`
- a thread-safe version of existing closure handler that enforces
`Sendable` conformance for concurrent execution environments - and added
conditional conformance to `Sendable` for other Adapters when the
Handler is `Sendable`

- **Enhanced handler protocols for concurrency**: Extended handler
protocols to support `Sendable` constraints and concurrent response
writing through `LambdaResponseStreamWriter & Sendable`, enabling safe
multi-threaded invocation processing

- **Created comprehensive Lambda Managed Instances examples**: Built
three demonstration functions showcasing concurrent execution
capabilities, streaming responses, and background processing patterns
specific to the new managed instances deployment model

**Context**
Lambda Managed Instances support multi-concurrent invocations where
multiple invocations execute simultaneously within the same execution
environment. The runtime now detects the configured concurrency level
and launches the appropriate number of RICs to handle concurrent
requests efficiently.

When `AWS_LAMBDA_MAX_CONCURRENCY` is 1 or unset, the runtime maintains
the existing single-threaded behaviour for optimal performance on
traditional Lambda deployments.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
2026-02-12 00:32:31 +01:00

82 lines
2.7 KiB
YAML

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for Lambda Managed Instances Example
# This template deploys three Lambda functions to Lambda Managed Instances
# using a pre-existing capacity provider.
Parameters:
CapacityProviderArn:
Type: String
Default: arn:aws:lambda:us-west-2:${AWS::AccountId}:capacity-provider:MyCapacityProvider # TODO : CHANGE The Name!
Description: ARN of the existing capacity provider for Lambda Managed Instances
Globals:
Function:
Handler: swift.bootstrap # ignored by the Swift runtime
Runtime: provided.al2023
Architectures:
- arm64
Timeout: 15
CapacityProviderConfig:
Arn: !Ref CapacityProviderArn
PerExecutionEnvironmentMaxConcurrency: 8
Environment:
Variables:
LOG_LEVEL: trace
Resources:
# HelloJSON Function - JSON input/output with structured data
HelloJSONFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip
FunctionName: !Sub "${AWS::StackName}-HelloJSON"
# Streaming Function - Demonstrates response streaming capabilities
StreamingFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/Streaming/Streaming.zip
FunctionName: !Sub "${AWS::StackName}-Streaming"
Timeout: 60 # Longer timeout for streaming operations
FunctionUrlConfig:
AuthType: AWS_IAM
InvokeMode: RESPONSE_STREAM
# BackgroundTasks Function - Handles long-running background processing
BackgroundTasksFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/BackgroundTasks/BackgroundTasks.zip
FunctionName: !Sub "${AWS::StackName}-BackgroundTasks"
Timeout: 300 # 5 minutes for background processing
Environment:
Variables:
LOG_LEVEL: debug
Outputs:
# Function URL for reference
StreamingFunctionUrl:
Description: Streaming Function URL
Value: !GetAtt StreamingFunctionUrl.FunctionUrl
# Function ARNs for reference
HelloJSONFunctionArn:
Description: "HelloJSON Function ARN"
Value: !GetAtt HelloJSONFunction.Arn
Export:
Name: !Sub "${AWS::StackName}-HelloJSONArn"
StreamingFunctionArn:
Description: "Streaming Function ARN"
Value: !GetAtt StreamingFunction.Arn
Export:
Name: !Sub "${AWS::StackName}-StreamingArn"
BackgroundTasksFunctionArn:
Description: "BackgroundTasks Function ARN"
Value: !GetAtt BackgroundTasksFunction.Arn
Export:
Name: !Sub "${AWS::StackName}-BackgroundTasksArn"