Skip to content

Commit 2669009

Browse files
authored
Update the tutorial for v2 (#450)
Update the text, code sample, and screenshots for runtime v2 Address #371
1 parent fe2cf5d commit 2669009

File tree

67 files changed

+377
-225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+377
-225
lines changed

.github/workflows/pull_request.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
# We pass the list of examples here, but we can't pass an array as argument
3737
# Instead, we pass a String with a valid JSON array.
3838
# The workaround is mentioned here https://github.com/orgs/community/discussions/11692
39-
examples: "[ 'APIGateway', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing' ]"
39+
examples: "[ 'APIGateway', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]"
4040

4141
archive_plugin_enabled: true
4242

Examples/Tutorial/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/Tutorial/Package.swift

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
import struct Foundation.URL
7+
8+
let package = Package(
9+
name: "Palindrome",
10+
platforms: [.macOS(.v15)],
11+
dependencies: [
12+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
13+
],
14+
targets: [
15+
// Targets are the basic building blocks of a package, defining a module or a test suite.
16+
// Targets can depend on other targets in this package and products from dependencies.
17+
.executableTarget(
18+
name: "Palindrome",
19+
dependencies: [
20+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
21+
]
22+
)
23+
]
24+
)
25+
26+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
27+
localDepsPath != "",
28+
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
29+
v.isDirectory == true
30+
{
31+
// when we use the local runtime as deps, let's remove the dependency added above
32+
let indexToRemove = package.dependencies.firstIndex { dependency in
33+
if case .sourceControl(
34+
name: _,
35+
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
36+
requirement: _
37+
) = dependency.kind {
38+
return true
39+
}
40+
return false
41+
}
42+
if let indexToRemove {
43+
package.dependencies.remove(at: indexToRemove)
44+
}
45+
46+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
47+
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
48+
package.dependencies += [
49+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
50+
]
51+
}

Examples/Tutorial/Sources/main.swift

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
17+
// the data structure to represent the input parameter
18+
struct Request: Decodable {
19+
let text: String
20+
}
21+
22+
// the data structure to represent the response parameter
23+
struct Response: Encodable {
24+
let text: String
25+
let isPalindrome: Bool
26+
let message: String
27+
}
28+
29+
// the business function
30+
func isPalindrome(_ text: String) -> Bool {
31+
let cleanedText = text.lowercased().filter { $0.isLetter }
32+
return cleanedText == String(cleanedText.reversed())
33+
}
34+
35+
// the lambda handler function
36+
let runtime = LambdaRuntime {
37+
(event: Request, context: LambdaContext) -> Response in
38+
39+
let result = isPalindrome(event.text)
40+
return Response(
41+
text: event.text,
42+
isPalindrome: result,
43+
message: "Your text is \(result ? "a" : "not a") palindrome"
44+
)
45+
}
46+
47+
// start the runtime
48+
try await runtime.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
disable=all
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
# shellcheck disable=all
21
# Create a project directory
3-
mkdir SquareNumber && cd SquareNumber
2+
mkdir Palindrome && cd Palindrome
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# shellcheck disable=all
21
# Create a project directory
3-
mkdir SquareNumber && cd SquareNumber
2+
mkdir Palindrome && cd Palindrome
3+
44
# create a skeleton project
5-
swift package init --type executable
5+
swift package init --type executable
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# shellcheck disable=all
21
# Create a project directory
3-
mkdir SquareNumber && cd SquareNumber
2+
mkdir Palindrome && cd Palindrome
3+
44
# create a skeleton project
55
swift package init --type executable
6+
67
# open Xcode in the current directory
7-
xed .
8+
xed .
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# shellcheck disable=all
21
# Create a project directory
3-
mkdir SquareNumber && cd SquareNumber
2+
mkdir Palindrome && cd Palindrome
3+
44
# create a skeleton project
55
swift package init --type executable
6+
67
# open Xcode in the current directory
78
xed .
9+
810
# alternatively, you may open VSCode
911
code .
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "SquareNumberLambda"
7+
name: "Palindrome"
88
)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "SquareNumberLambda",
7+
name: "Palindrome",
88
platforms: [
9-
.macOS(.v12)
9+
.macOS(.v15)
1010
]
1111
)
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "SquareNumberLambda",
7+
name: "Palindrome",
88
platforms: [
9-
.macOS(.v12)
9+
.macOS(.v15)
1010
],
1111
dependencies: [
12-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha")
12+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
1313
]
1414
)
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "SquareNumberLambda",
7+
name: "Palindrome",
88
platforms: [
9-
.macOS(.v12)
9+
.macOS(.v15)
1010
],
1111
products: [
12-
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"])
12+
.executable(name: "PalindromeLambda", targets: ["PalindromeLambda"])
1313
],
1414
dependencies: [
15-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha")
15+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
1616
]
1717
)

Sources/AWSLambdaRuntimeCore/Documentation.docc/Resources/code/03-02-05-package.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
7-
name: "SquareNumberLambda",
7+
name: "Palindrome",
88
platforms: [
9-
.macOS(.v12)
9+
.macOS(.v15)
1010
],
1111
products: [
12-
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"])
12+
.executable(name: "PalindromeLambda", targets: ["PalindromeLambda"])
1313
],
1414
dependencies: [
15-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha")
15+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
1616
],
1717
targets: [
1818
.executableTarget(
19-
name: "SquareNumberLambda",
19+
name: "PalindromeLambda",
2020
dependencies: [
2121
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
2222
],
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
@main
2-
struct SquareNumberHandler: SimpleLambdaHandler {}
1+
// the data structure to represent the input parameter
2+
struct Request: Decodable {
3+
let text: String
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import AWSLambdaRuntime
1+
// the data structure to represent the input parameter
2+
struct Request: Decodable {
3+
let text: String
4+
}
25

3-
@main
4-
struct SquareNumberHandler: SimpleLambdaHandler {}
6+
// the data structure to represent the response parameter
7+
struct Response: Encodable {
8+
let text: String
9+
let isPalindrome: Bool
10+
let message: String
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import AWSLambdaRuntime
1+
// the data structure to represent the input parameter
2+
struct Request: Decodable {
3+
let text: String
4+
}
5+
6+
// the data structure to represent the response parameter
7+
struct Response: Encodable {
8+
let text: String
9+
let isPalindrome: Bool
10+
let message: String
11+
}
212

3-
@main
4-
struct SquareNumberHandler: SimpleLambdaHandler {
5-
func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
13+
// the business function
14+
func isPalindrome(_ text: String) -> Bool {
15+
let cleanedText = text.lowercased().filter { $0.isLetter }
16+
return cleanedText == String(cleanedText.reversed())
617
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import AWSLambdaRuntime
22

3-
struct Input: Codable {
4-
let number: Double
3+
// the data structure to represent the input parameter
4+
struct Request: Decodable {
5+
let text: String
56
}
67

7-
struct Number: Codable {
8-
let result: Double
8+
// the data structure to represent the response parameter
9+
struct Response: Encodable {
10+
let text: String
11+
let isPalindrome: Bool
12+
let message: String
913
}
1014

11-
@main
12-
struct SquareNumberHandler: SimpleLambdaHandler {
13-
func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
15+
// the business function
16+
func isPalindrome(_ text: String) -> Bool {
17+
let cleanedText = text.lowercased().filter { $0.isLetter }
18+
return cleanedText == String(cleanedText.reversed())
1419
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import AWSLambdaRuntime
22

3-
struct Input: Codable {
4-
let number: Double
3+
// the data structure to represent the input parameter
4+
struct Request: Decodable {
5+
let text: String
56
}
67

7-
struct Number: Codable {
8-
let result: Double
8+
// the data structure to represent the response parameter
9+
struct Response: Encodable {
10+
let text: String
11+
let isPalindrome: Bool
12+
let message: String
913
}
1014

11-
@main
12-
struct SquareNumberHandler: SimpleLambdaHandler {
13-
typealias Event = Input
14-
typealias Output = Number
15+
// the business function
16+
func isPalindrome(_ text: String) -> Bool {
17+
let cleanedText = text.lowercased().filter { $0.isLetter }
18+
return cleanedText == String(cleanedText.reversed())
19+
}
20+
21+
// the lambda handler function
22+
let runtime = LambdaRuntime {
23+
(event: Request, context: LambdaContext) -> Response in
1524

16-
func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
1725
}

0 commit comments

Comments
 (0)