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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 51 additions & 0 deletions
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

Lines changed: 48 additions & 0 deletions
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()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
disable=all
Lines changed: 1 addition & 2 deletions
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
Lines changed: 3 additions & 3 deletions
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
Lines changed: 4 additions & 3 deletions
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 .
Lines changed: 4 additions & 2 deletions
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 .
Lines changed: 2 additions & 2 deletions
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
)

0 commit comments

Comments
 (0)