mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
9f566bf4f1
This PR has been reworked. Instead of silently switching the default base image based on Swift version, we now: 1. **Keep Amazon Linux 2 as the default** base Docker image for the packager plugin 2. **Add a prominent deprecation warning** when AL2 is used (either via Docker or natively), informing developers that AL2 reaches End of Life on June 30, 2026 3. **Migrate all examples** (READMEs, SAM templates, scripts) to build and deploy on Amazon Linux 2023 (`provided.al2023` runtime + `--base-docker-image swift:amazonlinux2023`) 4. **Update documentation** (readme, quick-setup) with migration notes The warning includes the `--base-docker-image swift:6.3-amazonlinux2023` flag and reminds developers to use the `provided.al2023` runtime when deploying. After June 30, 2026, the default will switch to AL2023. --- <details> <summary>Original PR description (superseded)</summary> ~~Now that Docker Hub has official Swift images based on Amazon Linux 2023 (starting with 6.3), the packager plugin picks the right base image automatically depending on the Swift version:~~ ~~- Swift 6.3 and later: `swift:<version>-amazonlinux2023`~~ ~~- Earlier versions: `swift:<version>-amazonlinux2` (unchanged behavior)~~ ~~- No version specified (latest): defaults to `amazonlinux2023`~~ ~~When only a major version is provided (e.g. `--swift-version 6` without a minor), we conservatively treat it as 6.0 and use Amazon Linux 2, since we can't be sure it's 6.3+.~~ ~~Also added a verbose log line showing the resolved Swift version, Amazon Linux version, and final base image to help with debugging.~~ ~~The `--base-docker-image` flag still overrides everything as before.~~ </details> --------- Co-authored-by: Sébastien Stormacq <stormacq@amazon.lu>
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
- HelloJSON - JSON input/output with structured data types
- Streaming - Demonstrates response streaming capabilities
- BackgroundTasks - Handles long-running background processing
Prerequisites
- AWS CLI configured with appropriate permissions
- SAM CLI installed
- Swift 6.3+ installed
- An existing Lambda Managed Instances capacity provider
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 and package the Swift Lambda function
swift package archive --allow-network-connections docker --base-docker-image swift:amazonlinux2023
# 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}