Files
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

4.3 KiB

Lambda Managed Instances Example

This example demonstrates deploying Swift Lambda functions to Lambda Managed Instances using AWS SAM. Lambda Managed Instances provide serverless simplicity with EC2 flexibility and cost optimization by running your functions on customer-owned EC2 instances.

Functions Included

  1. HelloJSON - JSON input/output with structured data types
  2. Streaming - Demonstrates response streaming capabilities
  3. BackgroundTasks - Handles long-running background processing

Prerequisites

Capacity Provider Configuration

Create your own capacity provider before deploying this example.

This example uses a pre-configured capacity provider with the ARN:

arn:aws:lambda:us-west-2:486652066693:capacity-provider:TestEC2

Deployment

# Build the Swift packages
# when compiling a standalone or new project
swift package archive --allow-network-connections docker 
# When compiling the example in this repository 
# LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker 

# Change the values below to match your setup 
REGION=us-west-2
CAPACITY_PROVIDER=arn:aws:lambda:us-west-2:<YOUR ACCOUNT ID>:capacity-provider:<YOUR CAPACITY PROVIDER NAME>

# Deploy using SAM
sam deploy \
    --resolve-s3 \
    --template-file template.yaml \
    --stack-name swift-lambda-managed-instances \
    --capabilities CAPABILITY_IAM \
    --region ${REGION} \
    --parameter-overrides \
        CapacityProviderArn=${CAPACITY_PROVIDER}

Function Details

HelloJSON Function

  • Timeout: 15 seconds (default)
  • Concurrency: 8 per execution environment (default)
  • Input: JSON {"name": "string", "age": number}
  • Output: JSON {"greetings": "string"}

Streaming Function

  • Timeout: 60 seconds
  • Concurrency: 8 per execution environment (default)
  • Features: Response streaming enabled
  • Output: Streams numbers with pauses

BackgroundTasks Function

  • Timeout: 300 seconds (5 minutes)
  • Concurrency: 8 per execution environment (default)
  • Input: JSON {"message": "string"}
  • Features: Long-running background processing after response

Testing with AWS CLI

After deployment, invoke each function with the AWS CLI:

Test HelloJSON Function

REGION=us-west-2
aws lambda invoke \
--region ${REGION} \
--function-name swift-lambda-managed-instances-HelloJSON \
--payload $(echo '{ "name" : "Swift Developer", "age" : 50 }' | base64)  \
out.txt && cat out.txt && rm out.txt

# Expected output: {"greetings": "Hello Swift Developer. You look older than your age."}

Test Streaming Function

# Get the Streaming URL
REGION=us-west-2
STREAMING_URL=$(aws cloudformation describe-stacks \
    --stack-name swift-lambda-managed-instances \
    --region ${REGION} \
    --query 'Stacks[0].Outputs[?OutputKey==`StreamingFunctionUrl`].OutputValue' \
    --output text)

# Set the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables
eval $(aws configure export-credentials --format env)

# Test with curl (streaming response)
curl "$STREAMING_URL" \
    --user "${AWS_ACCESS_KEY_ID}":"${AWS_SECRET_ACCESS_KEY}"   \
    --aws-sigv4 "aws:amz:${REGION}:lambda" \
    -H "x-amz-security-token: ${AWS_SESSION_TOKEN}" \
    --no-buffer

# Expected output: Numbers streaming with pauses

Test BackgroundTasks Function

# Test with AWS CLI
REGION=us-west-2
aws lambda invoke \
--region ${REGION} \
--function-name swift-lambda-managed-instances-BackgroundTasks \
--payload $(echo '{ "message" : "Additional processing in the background" }' | base64)  \
out.txt && cat out.txt && rm out.txt

# Expected output: {"echoedMessage": "Additional processing in the background"}
# Note: Background processing continues after response is sent

Cleanup

To remove all resources:

sam delete --stack-name swift-lambda-managed-instances --region ${REGION}