Skip to content

[doc] Getting Started documentation and tutorial #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
xcuserdata
Package.resolved
.serverless
.vscode
24 changes: 24 additions & 0 deletions Sources/AWSLambdaRuntimeCore/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ``AWSLambdaRuntimeCore``

An AWS Lambda runtime for the Swift programming language

## Overview

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.

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.

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.

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.

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.

## Topics

### Essentials

- <doc:quick-setup>
- <doc:/tutorials/table-of-content>
- ``LambdaHandler``

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Create a project directory
mkdir SquareNumber && cd SquareNumber
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Create a project directory
mkdir SquareNumber && cd SquareNumber
# create a skeleton project
swift package init --type executable
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Create a project directory
mkdir SquareNumber && cd SquareNumber
# create a skeleton project
swift package init --type executable
# open Xcode in the current directory
xed .
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Create a project directory
mkdir SquareNumber && cd SquareNumber
# create a skeleton project
swift package init --type executable
# open Xcode in the current directory
xed .
# alternatively, you may open VSCode
code .
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SquareNumberLambda",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SquareNumberLambda",
platforms: [
.macOS(.v12),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SquareNumberLambda",
platforms: [
.macOS(.v12),
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SquareNumberLambda",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"]),
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SquareNumberLambda",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "SquareNumberLambda", targets: ["SquareNumberLambda"]),
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
],
targets: [
.executableTarget(
name: "SquareNumberLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

@main
struct SquareNumberHandler: SimpleLambdaHandler {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AWSLambdaRuntime

@main
struct SquareNumberHandler: SimpleLambdaHandler {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import AWSLambdaRuntime

@main
struct SquareNumberHandler: SimpleLambdaHandler {
func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AWSLambdaRuntime

struct Input: Codable {
let number: Double
}

struct Number: Codable {
let result: Double
}

@main
struct SquareNumberHandler: SimpleLambdaHandler {
func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import AWSLambdaRuntime

struct Input: Codable {
let number: Double
}

struct Number: Codable {
let result: Double
}

@main
struct SquareNumberHandler: SimpleLambdaHandler {
typealias Event = Input
typealias Output = Number

func handle(_ event: Event, context: LambdaContext) async throws -> Output {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import AWSLambdaRuntime

struct Input: Codable {
let number: Double
}

struct Number: Codable {
let result: Double
}

@main
struct SquareNumberHandler: SimpleLambdaHandler {
typealias Event = Input
typealias Output = Number

func handle(_ event: Event, context: LambdaContext) async throws -> Output {
Number(result: event.number * event.number)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2023-04-14T11:42:21+0200 info LocalLambdaServer : [AWSLambdaRuntimeCore] LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke
2023-04-14T11:42:21+0200 info Lambda : [AWSLambdaRuntimeCore] lambda runtime starting with LambdaConfiguration
General(logLevel: info))
Lifecycle(id: 104957691689708, maxTimes: 0, stopSignal: TERM)
RuntimeEngine(ip: 127.0.0.1, port: 7000, requestTimeout: nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl --header "Content-Type: application/json" \
--request POST \
--data '{"number": 3}' \
http://localhost:7000/invoke

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
curl --header "Content-Type: application/json" \
--request POST \
--data '{"number": 3}' \
http://localhost:7000/invoke

{"result":9}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export LOCAL_LAMBDA_SERVER_ENABLED=true

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export LOCAL_LAMBDA_SERVER_ENABLED=true
swift run
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export LOCAL_LAMBDA_SERVER_ENABLED=true
swift run

Building for debugging...
Build complete! (0.20s)
2023-04-14T10:52:25+0200 info LocalLambdaServer : [AWSLambdaRuntimeCore] LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke
2023-04-14T10:52:25+0200 info Lambda : [AWSLambdaRuntimeCore] lambda runtime starting with LambdaConfiguration
General(logLevel: info))
Lifecycle(id: 102943961260250, maxTimes: 0, stopSignal: TERM)
RuntimeEngine(ip: 127.0.0.1, port: 7000, requestTimeout: nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
swift package --disable-sandbox plugin archive

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
swift package --disable-sandbox plugin archive

-------------------------------------------------------------------------
building "squarenumberlambda" in docker
-------------------------------------------------------------------------
updating "swift:amazonlinux2" docker image
amazonlinux2: Pulling from library/swift
Digest: sha256:5b0cbe56e35210fa90365ba3a4db9cd2b284a5b74d959fc1ee56a13e9c35b378
Status: Image is up to date for swift:amazonlinux2
docker.io/library/swift:amazonlinux2
building "SquareNumberLambda"
Building for production...
...
-------------------------------------------------------------------------
archiving "SquareNumberLambda"
-------------------------------------------------------------------------
1 archive created
* SquareNumberLambda at /Users/YourUserName/SquareNumberLambda/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
swift package --disable-sandbox plugin archive

-------------------------------------------------------------------------
building "squarenumberlambda" in docker
-------------------------------------------------------------------------
updating "swift:amazonlinux2" docker image
amazonlinux2: Pulling from library/swift
Digest: sha256:5b0cbe56e35210fa90365ba3a4db9cd2b284a5b74d959fc1ee56a13e9c35b378
Status: Image is up to date for swift:amazonlinux2
docker.io/library/swift:amazonlinux2
building "SquareNumberLambda"
Building for production...
...
-------------------------------------------------------------------------
archiving "SquareNumberLambda"
-------------------------------------------------------------------------
1 archive created
* SquareNumberLambda at /Users/YourUserName/SquareNumberLambda/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip


cp .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip ~/Desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws --version
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# --region the AWS Region to send the command
# --function-name the name of your function
# --cli-binary-format tells the cli to use raw data as input (default is base64)
# --payload the payload to pass to your function code
# result.json the name of the file to store the response from the function

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# --region the AWS Region to send the command
# --function-name the name of your function
# --cli-binary-format tells the cli to use raw data as input (default is base64)
# --payload the payload to pass to your function code
# result.json the name of the file to store the response from the function

aws lambda invoke \
--region us-west-2 \
--function-name SquaredNumberLambda \
--cli-binary-format raw-in-base64-out \
--payload '{"number":3}' \
result.json

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# --region the AWS Region to send the command
# --function-name the name of your function
# --cli-binary-format tells the cli to use raw data as input (default is base64)
# --payload the payload to pass to your function code
# result.json the name of the file to store the response from the function

aws lambda invoke \
--region us-west-2 \
--function-name SquaredNumberLambda \
--cli-binary-format raw-in-base64-out \
--payload '{"number":3}' \
result.json

{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# --region the AWS Region to send the command
# --function-name the name of your function
# --cli-binary-format tells the cli to use raw data as input (default is base64)
# --payload the payload to pass to your function code
# result.json the name of the file to store the response from the function

aws lambda invoke \
--region us-west-2 \
--function-name SquaredNumberLambda \
--cli-binary-format raw-in-base64-out \
--payload '{"number":3}' \
result.json

{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}

cat result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# --region the AWS Region to send the command
# --function-name the name of your function
# --cli-binary-format tells the cli to use raw data as input (default is base64)
# --payload the payload to pass to your function code
# result.json the name of the file to store the response from the function

aws lambda invoke \
--region us-west-2 \
--function-name SquaredNumberLambda \
--cli-binary-format raw-in-base64-out \
--payload '{"number":3}' \
result.json

{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}

cat result.json
{"result":9}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading