Skip to content

update and add examples to new APIs #228

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 3 commits into from
Sep 25, 2021
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
28 changes: 28 additions & 0 deletions Examples/Benchmark/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
32 changes: 32 additions & 0 deletions Examples/Benchmark/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import AWSLambdaRuntimeCore
import NIOCore

// If you would like to benchmark Swift's Lambda Runtime,
// use this example which is more performant.
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
// while the closure-based handlers do.

struct MyLambda: EventLoopLambdaHandler {
typealias Event = String
typealias Output = String

func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}

Lambda.run { $0.eventLoop.makeSucceededFuture(MyLambda()) }
3 changes: 3 additions & 0 deletions Examples/Deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM swift:5.5-amazonlinux2

RUN yum -y install zip
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ let package = Package(
// good for benchmarking
.executable(name: "Benchmark", targets: ["Benchmark"]),
// demonstrate different types of error handling
.executable(name: "ErrorHandling", targets: ["ErrorHandling"]),
// fully featured example with domain specific business logic
.executable(name: "CurrencyExchange", targets: ["CurrencyExchange"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
Expand All @@ -30,11 +27,5 @@ let package = Package(
.executableTarget(name: "HelloWorld", dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
]),
.executableTarget(name: "ErrorHandling", dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
]),
.executableTarget(name: "CurrencyExchange", dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
]),
]
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Lambda Functions Examples
# Deployment Examples

This sample project is a collection of Lambda functions that demonstrates
how to write a simple Lambda function in Swift, and how to package and deploy it
to the AWS Lambda platform.

The scripts are prepared to work from the `LambdaFunctions` folder.
The scripts are prepared to work from the `Deployment` folder.

```
git clone https://github.com/swift-server/swift-aws-lambda-runtime.git
cd swift-aws-lambda-runtime/Examples/LambdaFunctions
cd swift-aws-lambda-runtime/Examples/Deployment
```

Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.
Expand All @@ -27,7 +27,7 @@ Steps to deploy this sample to AWS Lambda using the AWS CLI:
./scripts/deploy.sh
```

Notes:
Notes:
- This script assumes you have AWS CLI installed and credentials setup in `~/.aws/credentials`.
- The default lambda function name is `SwiftSample`. You can specify a different one updating `lambda_name` in `deploy.sh`
- Update `s3_bucket=swift-lambda-test` in `deploy.sh` before running (AWS S3 buckets require a unique global name)
Expand Down Expand Up @@ -129,7 +129,7 @@ The `serverless-deploy.sh` script passes through any parameters to the Serverles

For the APIGateway sample:

The Serverless template will provide an endpoint which you can use to test the Lambda.
The Serverless template will provide an endpoint which you can use to test the Lambda.

Outuput example:

Expand Down Expand Up @@ -174,4 +174,4 @@ For all other samples use the AWS Lambda console.
./scripts/serverless-remove.sh
```

The script will ask you which sample Lambda you wish to remove from the previous depolyment.
The script will ask you which sample Lambda you wish to remove from the previous deployment.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import NIO
// use this example which is more performant.
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
// while the closure-based handlers do.
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }

struct BenchmarkHandler: EventLoopLambdaHandler {
typealias Event = String
Expand All @@ -29,3 +28,5 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}

Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ echo "done"
echo "-------------------------------------------------------------------------"
echo "building \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \
bash -cl "swift build --product $executable -c release"
echo "done"

echo "-------------------------------------------------------------------------"
echo "packaging \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \
bash -cl "./scripts/package.sh $executable"
echo "done"
32 changes: 32 additions & 0 deletions Examples/Echo/Lambda.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime

// in this example we are receiving and responding with strings

@main
struct MyLambda: LambdaHandler {
typealias Event = String
typealias Output = String

init(context: Lambda.InitializationContext) async throws {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ input: String, context: Lambda.Context) async throws -> String {
// as an example, respond with the input's reversed
String(input.reversed())
}
}
28 changes: 28 additions & 0 deletions Examples/Echo/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import AWSLambdaRuntime
// MARK: - Run Lambda

@main
struct ErrorsHappenHandler: LambdaHandler {
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = Response

Expand Down
28 changes: 28 additions & 0 deletions Examples/ErrorHandling/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Logging
// MARK: - Run Lambda

@main
struct CurrencyExchangeHandler: LambdaHandler {
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = [Exchange]

Expand Down
28 changes: 28 additions & 0 deletions Examples/Foundation/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
27 changes: 11 additions & 16 deletions Sources/CodableSample/main.swift → Examples/JSON/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime
import NIOCore

struct Request: Codable {
let body: String
Expand All @@ -24,23 +23,19 @@ struct Response: Codable {
}

// in this example we are receiving and responding with codables. Request and Response above are examples of how to use
// codables to model your reqeuest and response objects
struct Handler: EventLoopLambdaHandler {
// codables to model your request and response objects

@main
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = Response

func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
init(context: Lambda.InitializationContext) async throws {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
// as an example, respond with the input event's reversed body
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
Response(body: String(event.body.reversed()))
}
}

Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }

// MARK: - this can also be expressed as a closure:

/*
Lambda.run { (_, request: Request, callback) in
callback(.success(Response(body: String(request.body.reversed()))))
}
*/
28 changes: 28 additions & 0 deletions Examples/JSON/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
3 changes: 0 additions & 3 deletions Examples/LambdaFunctions/Dockerfile

This file was deleted.

15 changes: 0 additions & 15 deletions Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml

This file was deleted.

Loading