Skip to content

Commit 32005a2

Browse files
sebstofabianfett
andcommitted
Initial Commit for getting started documentation
Co-authored-by: Fabian Fett <fabianfett@apple.com>
1 parent 50c4ec9 commit 32005a2

File tree

77 files changed

+999
-0
lines changed

Some content is hidden

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

77 files changed

+999
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
xcuserdata
99
Package.resolved
1010
.serverless
11+
.vscode
12+
Examples/SAM
13+
Makefile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ``AWSLambdaRuntimeCore``
2+
3+
An AWS Lambda runtime for the Swift programming language
4+
5+
## Overview
6+
7+
Many modern systems have client components like iOS, macOS or watchOS applications as well as server components that those clients interact with. Serverless functions are often the easiest and most efficient way for client application developers to extend their applications into the cloud.
8+
9+
Serverless functions are increasingly becoming a popular choice for running event-driven or otherwise ad-hoc compute tasks in the cloud. They power mission critical microservices and data intensive workloads. In many cases, serverless functions allow developers to more easily scale and control compute costs given their on-demand nature.
10+
11+
When using serverless functions, attention must be given to resource utilization as it directly impacts the costs of the system. This is where Swift shines! With its low memory footprint, deterministic performance, and quick start time, Swift is a fantastic match for the serverless functions architecture.
12+
13+
Combine this with Swift's developer friendliness, expressiveness, and emphasis on safety, and we have a solution that is great for developers at all skill levels, scalable, and cost effective.
14+
15+
Swift AWS Lambda Runtime was designed to make building Lambda functions in Swift simple and safe. The library is an implementation of the [AWS Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html) and uses an embedded asynchronous HTTP client based on [SwiftNIO](https://github.com/apple/swift-nio) that is fine-tuned for performance in the AWS Lambda Runtime context. The library provides a multi-tier API that allows building a range of Lambda functions: from quick and simple closures to complex, performance-sensitive event handlers.
16+
17+
## Topics
18+
19+
### Essentials
20+
21+
- <doc:quick-setup>
22+
- <doc:/tutorials/table-of-content>
23+
- ``LambdaHandler``
24+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Create a project directory
2+
mkdir SquareNumber && cd SquareNumber
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Create a project directory
2+
mkdir SquareNumber && cd SquareNumber
3+
# create a skeleton project
4+
swift package init --type executable
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Create a project directory
2+
mkdir SquareNumber && cd SquareNumber
3+
# create a skeleton project
4+
swift package init --type executable
5+
# open Xcode in the current directory
6+
xed .
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create a project directory
2+
mkdir SquareNumber && cd SquareNumber
3+
# create a skeleton project
4+
swift package init --type executable
5+
# open Xcode in the current directory
6+
xed .
7+
# alternatively, you may open VSCode
8+
code .
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// swift-tools-version:5.8
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SquareNumberLambda",
8+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// swift-tools-version:5.8
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SquareNumberLambda",
8+
platforms: [
9+
.macOS(.v12),
10+
],
11+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// swift-tools-version:5.8
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SquareNumberLambda",
8+
platforms: [
9+
.macOS(.v12),
10+
],
11+
dependencies: [
12+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", .upToNextMajor(from:"1.0.0")),
13+
],
14+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// swift-tools-version:5.8
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SquareNumberLambda",
8+
platforms: [
9+
.macOS(.v12),
10+
],
11+
products: [
12+
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"]),
13+
],
14+
dependencies: [
15+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", .upToNextMajor(from:"1.0.0")),
16+
],
17+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.8
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SquareNumberLambda",
8+
platforms: [
9+
.macOS(.v12),
10+
],
11+
products: [
12+
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"]),
13+
],
14+
dependencies: [
15+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", .upToNextMajor(from:"1.0.0")),
16+
],
17+
targets: [
18+
.executableTarget(
19+
name: "SquareNumberLambda",
20+
dependencies: [
21+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
22+
]
23+
),
24+
]
25+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
@main
3+
struct SquareNumberHandler: SimpleLambdaHandler {
4+
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import AWSLambdaRuntime
2+
3+
@main
4+
struct SquareNumberHandler: SimpleLambdaHandler {
5+
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import AWSLambdaRuntime
2+
3+
@main
4+
struct SquareNumberHandler: SimpleLambdaHandler {
5+
6+
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
7+
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import AWSLambdaRuntime
2+
3+
struct Input: Codable {
4+
let number: Double
5+
}
6+
7+
struct Number: Codable {
8+
let result: Double
9+
}
10+
11+
@main
12+
struct SquareNumberHandler: SimpleLambdaHandler {
13+
14+
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
15+
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import AWSLambdaRuntime
2+
3+
struct Input: Codable {
4+
let number: Double
5+
}
6+
7+
struct Number: Codable {
8+
let result: Double
9+
}
10+
11+
@main
12+
struct SquareNumberHandler: SimpleLambdaHandler {
13+
typealias Event = Input
14+
typealias Output = Number
15+
16+
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
17+
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import AWSLambdaRuntime
2+
3+
struct Input: Codable {
4+
let number: Double
5+
}
6+
7+
struct Number: Codable {
8+
let result: Double
9+
}
10+
11+
@main
12+
struct SquareNumberHandler: SimpleLambdaHandler {
13+
typealias Event = Input
14+
typealias Output = Number
15+
16+
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
17+
return Number(result: event.number * event.number)
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import AWSLambdaRuntime
2+
3+
struct Input: Codable {
4+
let number: Double
5+
}
6+
7+
struct Number: Codable {
8+
let result: Double
9+
}
10+
11+
@main
12+
struct SquareNumberHandler: SimpleLambdaHandler {
13+
typealias Event = Input
14+
typealias Output = Number
15+
16+
func handle(_ event: Input, context: LambdaContext) async throws -> Number {
17+
Number(result: event.number * event.number)
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2023-04-14T11:42:21+0200 info LocalLambdaServer : [AWSLambdaRuntimeCore] LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke
2+
2023-04-14T11:42:21+0200 info Lambda : [AWSLambdaRuntimeCore] lambda runtime starting with LambdaConfiguration
3+
General(logLevel: info))
4+
Lifecycle(id: 104957691689708, maxTimes: 0, stopSignal: TERM)
5+
RuntimeEngine(ip: 127.0.0.1, port: 7000, requestTimeout: nil
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl --header "Content-Type: application/json" \
2+
--request POST \
3+
--data '{"number": 3}' \
4+
http://localhost:7000/invoke
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
curl --header "Content-Type: application/json" \
2+
--request POST \
3+
--data '{"number": 3}' \
4+
http://localhost:7000/invoke
5+
6+
{"result":9}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export LOCAL_LAMBDA_SERVER_ENABLED=true
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export LOCAL_LAMBDA_SERVER_ENABLED=true
2+
swift run
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export LOCAL_LAMBDA_SERVER_ENABLED=true
2+
swift run
3+
4+
Building for debugging...
5+
Build complete! (0.20s)
6+
2023-04-14T10:52:25+0200 info LocalLambdaServer : [AWSLambdaRuntimeCore] LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke
7+
2023-04-14T10:52:25+0200 info Lambda : [AWSLambdaRuntimeCore] lambda runtime starting with LambdaConfiguration
8+
General(logLevel: info))
9+
Lifecycle(id: 102943961260250, maxTimes: 0, stopSignal: TERM)
10+
RuntimeEngine(ip: 127.0.0.1, port: 7000, requestTimeout: nil
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
swift package --disable-sandbox plugin archive
2+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
swift package --disable-sandbox plugin archive
2+
3+
-------------------------------------------------------------------------
4+
building "squarenumberlambda" in docker
5+
-------------------------------------------------------------------------
6+
updating "swift:amazonlinux2" docker image
7+
amazonlinux2: Pulling from library/swift
8+
Digest: sha256:5b0cbe56e35210fa90365ba3a4db9cd2b284a5b74d959fc1ee56a13e9c35b378
9+
Status: Image is up to date for swift:amazonlinux2
10+
docker.io/library/swift:amazonlinux2
11+
building "SquareNumberLambda"
12+
Building for production...
13+
...
14+
-------------------------------------------------------------------------
15+
archiving "SquareNumberLambda"
16+
-------------------------------------------------------------------------
17+
1 archive created
18+
* SquareNumberLambda at /Users/YourUserName/SquareNumberLambda/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip
19+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
swift package --disable-sandbox plugin archive
2+
3+
-------------------------------------------------------------------------
4+
building "squarenumberlambda" in docker
5+
-------------------------------------------------------------------------
6+
updating "swift:amazonlinux2" docker image
7+
amazonlinux2: Pulling from library/swift
8+
Digest: sha256:5b0cbe56e35210fa90365ba3a4db9cd2b284a5b74d959fc1ee56a13e9c35b378
9+
Status: Image is up to date for swift:amazonlinux2
10+
docker.io/library/swift:amazonlinux2
11+
building "SquareNumberLambda"
12+
Building for production...
13+
...
14+
-------------------------------------------------------------------------
15+
archiving "SquareNumberLambda"
16+
-------------------------------------------------------------------------
17+
1 archive created
18+
* SquareNumberLambda at /Users/YourUserName/SquareNumberLambda/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip
19+
20+
21+
cp .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip ~/Desktop
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aws --version
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# --region the AWS Region to send the command
3+
# --function-name the name of your function
4+
# --cli-binary-format tells the cli to use raw data as input (default is base64)
5+
# --payload the payload to pass to your function code
6+
# result.json the name of the file to store the response from the function
7+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# --region the AWS Region to send the command
3+
# --function-name the name of your function
4+
# --cli-binary-format tells the cli to use raw data as input (default is base64)
5+
# --payload the payload to pass to your function code
6+
# result.json the name of the file to store the response from the function
7+
8+
aws lambda invoke \
9+
--region us-west-2 \
10+
--function-name SquaredNumberLambda \
11+
--cli-binary-format raw-in-base64-out \
12+
--payload '{"number":3}' \
13+
result.json
14+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# --region the AWS Region to send the command
3+
# --function-name the name of your function
4+
# --cli-binary-format tells the cli to use raw data as input (default is base64)
5+
# --payload the payload to pass to your function code
6+
# result.json the name of the file to store the response from the function
7+
8+
aws lambda invoke \
9+
--region us-west-2 \
10+
--function-name SquaredNumberLambda \
11+
--cli-binary-format raw-in-base64-out \
12+
--payload '{"number":3}' \
13+
result.json
14+
15+
{
16+
"StatusCode": 200,
17+
"ExecutedVersion": "$LATEST"
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# --region the AWS Region to send the command
3+
# --function-name the name of your function
4+
# --cli-binary-format tells the cli to use raw data as input (default is base64)
5+
# --payload the payload to pass to your function code
6+
# result.json the name of the file to store the response from the function
7+
8+
aws lambda invoke \
9+
--region us-west-2 \
10+
--function-name SquaredNumberLambda \
11+
--cli-binary-format raw-in-base64-out \
12+
--payload '{"number":3}' \
13+
result.json
14+
15+
{
16+
"StatusCode": 200,
17+
"ExecutedVersion": "$LATEST"
18+
}
19+
20+
cat result.json

0 commit comments

Comments
 (0)