Skip to content

Commit a8a54d9

Browse files
sebstoSebastien Stormacq
andauthored
Add docc for Lambda Managed Instances (#637)
Add a docc page to cover Lambda Managed instanced added at version 2.6.0 --------- Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
1 parent 190eb81 commit a8a54d9

File tree

8 files changed

+194
-1
lines changed

8 files changed

+194
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ xcuserdata
1010
Package.resolved
1111
.serverless
1212
.vscode
13-
Makefile
1413
.devcontainer
1514
.amazonq
1615
.kiro

.licenseignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.swiftformatignore
44
.spi.yml
55
.swift-format
6+
Makefile
67
.github/*
78
*.md
89
**/*.md

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Makefile for library
2+
3+
format:
4+
swift format format --parallel --recursive --in-place ./Package.swift Examples/ Sources/ Tests/
5+
6+
doc-dependency:
7+
# Dynamically add the swift-docc-plugin for doc generation
8+
cp Package.swift Package.swift.bak
9+
for manifest in Package.swift ; do \
10+
if [ -f "$$manifest" ] && ! grep -E -i "https://github.com/(apple|swiftlang)/swift-docc-plugin" "$$manifest" ; then \
11+
echo "package.dependencies.append(" >> "$$manifest" ; \
12+
echo " .package(url: \"https://github.com/swiftlang/swift-docc-plugin\", from: \"1.4.5\")" >> "$$manifest" ; \
13+
echo ")" >> "$$manifest" ; \
14+
fi ; \
15+
done
16+
17+
preview-docs: doc-dependency
18+
# xcrun docc preview Sources/AWSLambdaRuntime/Docs.docc --output-path docc-output
19+
swift package --disable-sandbox preview-documentation --target AWSLambdaRuntime
20+
mv Package.swift.bak Package.swift
21+
22+
# swiftpackageindex.com/awslabs/swift-aws-lambda-runtime/~/documentation/awslambdaruntime/
23+
generate-docs: doc-dependency
24+
touch .nojekyll
25+
swift package \
26+
--allow-writing-to-directory ./docs \
27+
generate-documentation \
28+
--target BedrockService \
29+
--disable-indexing \
30+
--transform-for-static-hosting \
31+
--hosting-base-path swift-aws-lambda-runtime \
32+
--output-path ./docs
33+
34+
mv Package.swift.bak Package.swift

Sources/AWSLambdaRuntime/Docs.docc/Deployment.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Deploying your Swift Lambda functions
22

3+
@Metadata {
4+
@PageKind(article)
5+
@PageColor(orange)
6+
@SupportedLanguage(swift)
7+
@PageImage(source: "lambda.png", alt: "AWS Lambda", purpose: icon)
8+
}
9+
310
Learn how to deploy your Swift Lambda functions to AWS.
411

512
### Overview

Sources/AWSLambdaRuntime/Docs.docc/Documentation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ``AWSLambdaRuntime``
22

3+
@Metadata {
4+
@PageKind(article)
5+
@PageColor(orange)
6+
@SupportedLanguage(swift)
7+
@PageImage(source: "lambda.png", alt: "AWS Lambda", purpose: icon)
8+
}
9+
310
An AWS Lambda runtime for the Swift programming language
411

512
## Overview
@@ -21,3 +28,7 @@ Swift AWS Lambda Runtime was designed to make building Lambda functions in Swift
2128
- <doc:/tutorials/table-of-content>
2229
- <doc:quick-setup>
2330
- <doc:Deployment>
31+
32+
### Advanced Deployment
33+
34+
- <doc:managed-instances>
147 KB
Loading
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Lambda Managed Instances
2+
3+
@Metadata {
4+
@PageKind(article)
5+
@PageColor(orange)
6+
@SupportedLanguage(swift)
7+
@PageImage(source: "lambda.png", alt: "AWS Lambda", purpose: icon)
8+
}
9+
10+
Deploy Swift Lambda functions on Amazon EC2 instances with concurrent execution support
11+
12+
## Overview
13+
14+
Lambda Managed Instances enables you to run Lambda functions on your current-generation Amazon EC2 instances while maintaining serverless simplicity. AWS handles all infrastructure management tasks including instance lifecycle, OS and runtime patching, routing, load balancing, and auto-scaling, while you benefit from EC2 flexibility and cost optimization.
15+
16+
The key difference from traditional Lambda is concurrent execution support—multiple invocations can run simultaneously within the same execution environment on the same EC2 host.
17+
18+
## When to Use Lambda Managed Instances
19+
20+
Lambda Managed Instances are ideal for:
21+
22+
- **Sustained workloads** where cost optimization through EC2 pricing models provides better economics than traditional Lambda
23+
- **Specialized compute requirements** needing specific EC2 instance types such as Graviton4 or network-optimized instances
24+
- **High-throughput scenarios** where concurrent execution on the same host improves performance and resource utilization
25+
- **Workloads requiring EC2 flexibility** while maintaining serverless operational simplicity
26+
27+
## Code Changes Required
28+
29+
Migrating existing Lambda functions to Lambda Managed Instances requires two simple changes:
30+
31+
### 1. Use `LambdaManagedRuntime` Instead of `LambdaRuntime`
32+
33+
Replace your standard `LambdaRuntime` initialization with `LambdaManagedRuntime`:
34+
35+
```swift
36+
import AWSLambdaRuntime
37+
38+
// Before (standard Lambda)
39+
let runtime = LambdaRuntime {
40+
(event: HelloRequest, context: LambdaContext) in
41+
HelloResponse(greetings: "Hello \(event.name)!")
42+
}
43+
44+
// After (Lambda Managed Instances)
45+
let runtime = LambdaManagedRuntime {
46+
(event: HelloRequest, context: LambdaContext) in
47+
HelloResponse(greetings: "Hello \(event.name)!")
48+
}
49+
50+
try await runtime.run()
51+
```
52+
53+
### 2. Ensure Handlers Conform to `Sendable`
54+
55+
Because Lambda Managed Instances support concurrent invocations, your handler functions and structs must conform to the `Sendable` protocol to ensure thread safety:
56+
57+
```swift
58+
import AWSLambdaRuntime
59+
60+
// Handler struct must explicitly conform to Sendable
61+
struct MyHandler: LambdaWithBackgroundProcessingHandler, Sendable {
62+
typealias Event = MyRequest
63+
typealias Output = MyResponse
64+
65+
func handle(
66+
_ event: Event,
67+
outputWriter: some LambdaResponseWriter<Output>,
68+
context: LambdaContext
69+
) async throws {
70+
try await outputWriter.write(MyResponse(message: "Processed"))
71+
}
72+
}
73+
74+
// Use LambdaCodableAdapter to pass the handler to LambdaManagedRuntime
75+
let adapter = LambdaCodableAdapter(handler: MyHandler())
76+
let runtime = LambdaManagedRuntime(handler: adapter)
77+
try await runtime.run()
78+
```
79+
80+
For simple data structures, the Swift compiler automatically infers `Sendable` conformance, but explicitly declaring it is recommended for clarity and safety.
81+
82+
## How It Works
83+
84+
The runtime automatically detects the configured concurrency level through the `AWS_LAMBDA_MAX_CONCURRENCY` environment variable and launches the appropriate number of Runtime Interface Clients (RICs) to handle concurrent requests efficiently.
85+
86+
When `AWS_LAMBDA_MAX_CONCURRENCY` is 1 or unset, the runtime maintains single-threaded behavior for optimal performance on traditional Lambda deployments, ensuring backward compatibility.
87+
88+
The managed instances support is implemented behind a Swift package trait (`ManagedRuntimeSupport`) that's enabled by default. If you're concerned about binary size and don't need managed instances support, you can disable this specific trait in your `Package.swift`:
89+
90+
```swift
91+
dependencies: [
92+
.package(
93+
url: "https://github.com/awslabs/swift-aws-lambda-runtime.git",
94+
from: "2.0.0",
95+
traits: [
96+
// Keep other default traits but exclude ManagedRuntimeSupport
97+
"FoundationJSONSupport",
98+
"ServiceLifecycleSupport",
99+
"LocalServerSupport"
100+
]
101+
),
102+
],
103+
targets: [
104+
.executableTarget(
105+
name: "MyLambda",
106+
dependencies: [
107+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
108+
]
109+
),
110+
]
111+
```
112+
113+
## Prerequisites
114+
115+
Before deploying to Lambda Managed Instances:
116+
117+
1. Create a [Lambda Managed Instances capacity provider](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-capacity-providers.html) in your AWS account
118+
2. Configure your deployment to reference the capacity provider ARN
119+
120+
## Example Functions
121+
122+
The Swift AWS Lambda Runtime includes three comprehensive examples demonstrating Lambda Managed Instances capabilities:
123+
124+
- **HelloJSON**: JSON input/output with structured data types and concurrent execution
125+
- **Streaming**: Response streaming with concurrent invocation handling
126+
- **BackgroundTasks**: Long-running background processing after response with concurrency support
127+
128+
See the [ManagedInstances example directory](https://github.com/awslabs/swift-aws-lambda-runtime/tree/main/Examples/ManagedInstances) for complete deployment instructions using AWS SAM.
129+
130+
## Additional Resources
131+
132+
- [AWS Lambda Managed Instances Documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances.html)
133+
- [Execution Environment Guide](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-execution-environment.html)
134+
- [Capacity Provider Configuration](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-capacity-providers.html)

Sources/AWSLambdaRuntime/Docs.docc/quick-setup.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Getting Started Quickly
22

3+
@Metadata {
4+
@PageKind(article)
5+
@PageColor(orange)
6+
@SupportedLanguage(swift)
7+
@PageImage(source: "lambda.png", alt: "AWS Lambda", purpose: icon)
8+
}
9+
310
Learn how to create your first project in 3 minutes.
411

512
Follow these instructions to get a high-level overview of the steps to write, test, and deploy your first Lambda function written in Swift.

0 commit comments

Comments
 (0)