diff --git a/Examples/Benchmark/Package.swift b/Examples/Benchmark/Package.swift new file mode 100644 index 00000000..ddadbae8 --- /dev/null +++ b/Examples/Benchmark/Package.swift @@ -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: "." + ), + ] +) diff --git a/Examples/Benchmark/main.swift b/Examples/Benchmark/main.swift new file mode 100644 index 00000000..421d0cdc --- /dev/null +++ b/Examples/Benchmark/main.swift @@ -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 { + context.eventLoop.makeSucceededFuture("hello, world!") + } +} + +Lambda.run { $0.eventLoop.makeSucceededFuture(MyLambda()) } diff --git a/Examples/LambdaFunctions/.dockerignore b/Examples/Deployment/.dockerignore similarity index 100% rename from Examples/LambdaFunctions/.dockerignore rename to Examples/Deployment/.dockerignore diff --git a/Examples/Deployment/Dockerfile b/Examples/Deployment/Dockerfile new file mode 100644 index 00000000..32962859 --- /dev/null +++ b/Examples/Deployment/Dockerfile @@ -0,0 +1,3 @@ +FROM swift:5.5-amazonlinux2 + +RUN yum -y install zip diff --git a/Examples/LambdaFunctions/Package.swift b/Examples/Deployment/Package.swift similarity index 66% rename from Examples/LambdaFunctions/Package.swift rename to Examples/Deployment/Package.swift index 52a21b98..a34069b9 100644 --- a/Examples/LambdaFunctions/Package.swift +++ b/Examples/Deployment/Package.swift @@ -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 @@ -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"), - ]), ] ) diff --git a/Examples/LambdaFunctions/README.md b/Examples/Deployment/README.md similarity index 96% rename from Examples/LambdaFunctions/README.md rename to Examples/Deployment/README.md index 3c46a092..6066383d 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/Deployment/README.md @@ -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. @@ -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) @@ -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: @@ -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. \ No newline at end of file +The script will ask you which sample Lambda you wish to remove from the previous deployment. diff --git a/Examples/LambdaFunctions/Sources/Benchmark/main.swift b/Examples/Deployment/Sources/Benchmark/main.swift similarity index 99% rename from Examples/LambdaFunctions/Sources/Benchmark/main.swift rename to Examples/Deployment/Sources/Benchmark/main.swift index d0d54706..ce6d3e0d 100644 --- a/Examples/LambdaFunctions/Sources/Benchmark/main.swift +++ b/Examples/Deployment/Sources/Benchmark/main.swift @@ -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 @@ -29,3 +28,5 @@ struct BenchmarkHandler: EventLoopLambdaHandler { context.eventLoop.makeSucceededFuture("hello, world!") } } + +Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) } diff --git a/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift similarity index 100% rename from Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift rename to Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift diff --git a/Examples/LambdaFunctions/scripts/SAM/APIGateway-template.yml b/Examples/Deployment/scripts/SAM/APIGateway-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/SAM/APIGateway-template.yml rename to Examples/Deployment/scripts/SAM/APIGateway-template.yml diff --git a/Examples/LambdaFunctions/scripts/SAM/Benchmark-template.yml b/Examples/Deployment/scripts/SAM/Benchmark-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/SAM/Benchmark-template.yml rename to Examples/Deployment/scripts/SAM/Benchmark-template.yml diff --git a/Examples/LambdaFunctions/scripts/SAM/HelloWorld-template.yml b/Examples/Deployment/scripts/SAM/HelloWorld-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/SAM/HelloWorld-template.yml rename to Examples/Deployment/scripts/SAM/HelloWorld-template.yml diff --git a/Examples/LambdaFunctions/scripts/build-and-package.sh b/Examples/Deployment/scripts/build-and-package.sh similarity index 96% rename from Examples/LambdaFunctions/scripts/build-and-package.sh rename to Examples/Deployment/scripts/build-and-package.sh index 4e45c486..f1e0a922 100755 --- a/Examples/LambdaFunctions/scripts/build-and-package.sh +++ b/Examples/Deployment/scripts/build-and-package.sh @@ -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" diff --git a/Examples/LambdaFunctions/scripts/config.sh b/Examples/Deployment/scripts/config.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/config.sh rename to Examples/Deployment/scripts/config.sh diff --git a/Examples/LambdaFunctions/scripts/deploy.sh b/Examples/Deployment/scripts/deploy.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/deploy.sh rename to Examples/Deployment/scripts/deploy.sh diff --git a/Examples/LambdaFunctions/scripts/package.sh b/Examples/Deployment/scripts/package.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/package.sh rename to Examples/Deployment/scripts/package.sh diff --git a/Examples/LambdaFunctions/scripts/sam-deploy.sh b/Examples/Deployment/scripts/sam-deploy.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/sam-deploy.sh rename to Examples/Deployment/scripts/sam-deploy.sh diff --git a/Examples/LambdaFunctions/scripts/serverless-deploy.sh b/Examples/Deployment/scripts/serverless-deploy.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/serverless-deploy.sh rename to Examples/Deployment/scripts/serverless-deploy.sh diff --git a/Examples/LambdaFunctions/scripts/serverless-remove.sh b/Examples/Deployment/scripts/serverless-remove.sh similarity index 100% rename from Examples/LambdaFunctions/scripts/serverless-remove.sh rename to Examples/Deployment/scripts/serverless-remove.sh diff --git a/Examples/LambdaFunctions/scripts/serverless/APIGateway-template.yml b/Examples/Deployment/scripts/serverless/APIGateway-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/serverless/APIGateway-template.yml rename to Examples/Deployment/scripts/serverless/APIGateway-template.yml diff --git a/Examples/LambdaFunctions/scripts/serverless/Benchmark-template.yml b/Examples/Deployment/scripts/serverless/Benchmark-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/serverless/Benchmark-template.yml rename to Examples/Deployment/scripts/serverless/Benchmark-template.yml diff --git a/Examples/LambdaFunctions/scripts/serverless/HelloWorld-template.yml b/Examples/Deployment/scripts/serverless/HelloWorld-template.yml similarity index 100% rename from Examples/LambdaFunctions/scripts/serverless/HelloWorld-template.yml rename to Examples/Deployment/scripts/serverless/HelloWorld-template.yml diff --git a/Examples/Echo/Lambda.swift b/Examples/Echo/Lambda.swift new file mode 100644 index 00000000..684ed45d --- /dev/null +++ b/Examples/Echo/Lambda.swift @@ -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()) + } +} diff --git a/Examples/Echo/Package.swift b/Examples/Echo/Package.swift new file mode 100644 index 00000000..caae8f03 --- /dev/null +++ b/Examples/Echo/Package.swift @@ -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: "." + ), + ] +) diff --git a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift b/Examples/ErrorHandling/Lambda.swift similarity index 98% rename from Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift rename to Examples/ErrorHandling/Lambda.swift index f7d47dd6..e9b30e9f 100644 --- a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift +++ b/Examples/ErrorHandling/Lambda.swift @@ -17,7 +17,7 @@ import AWSLambdaRuntime // MARK: - Run Lambda @main -struct ErrorsHappenHandler: LambdaHandler { +struct MyLambda: LambdaHandler { typealias Event = Request typealias Output = Response diff --git a/Examples/ErrorHandling/Package.swift b/Examples/ErrorHandling/Package.swift new file mode 100644 index 00000000..caae8f03 --- /dev/null +++ b/Examples/ErrorHandling/Package.swift @@ -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: "." + ), + ] +) diff --git a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift b/Examples/Foundation/Lambda.swift similarity index 99% rename from Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift rename to Examples/Foundation/Lambda.swift index 1db53573..0921ac88 100644 --- a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift +++ b/Examples/Foundation/Lambda.swift @@ -24,7 +24,7 @@ import Logging // MARK: - Run Lambda @main -struct CurrencyExchangeHandler: LambdaHandler { +struct MyLambda: LambdaHandler { typealias Event = Request typealias Output = [Exchange] diff --git a/Examples/Foundation/Package.swift b/Examples/Foundation/Package.swift new file mode 100644 index 00000000..caae8f03 --- /dev/null +++ b/Examples/Foundation/Package.swift @@ -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: "." + ), + ] +) diff --git a/Sources/CodableSample/main.swift b/Examples/JSON/Lambda.swift similarity index 57% rename from Sources/CodableSample/main.swift rename to Examples/JSON/Lambda.swift index dae2b39e..d009861d 100644 --- a/Sources/CodableSample/main.swift +++ b/Examples/JSON/Lambda.swift @@ -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 @@ -13,7 +13,6 @@ //===----------------------------------------------------------------------===// import AWSLambdaRuntime -import NIOCore struct Request: Codable { let body: String @@ -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 { + 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())))) - } - */ diff --git a/Examples/JSON/Package.swift b/Examples/JSON/Package.swift new file mode 100644 index 00000000..caae8f03 --- /dev/null +++ b/Examples/JSON/Package.swift @@ -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: "." + ), + ] +) diff --git a/Examples/LambdaFunctions/Dockerfile b/Examples/LambdaFunctions/Dockerfile deleted file mode 100644 index d5315703..00000000 --- a/Examples/LambdaFunctions/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM swift:5.2-amazonlinux2 - -RUN yum -y install zip diff --git a/Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml b/Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml deleted file mode 100644 index b7b4f250..00000000 --- a/Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml +++ /dev/null @@ -1,15 +0,0 @@ -AWSTemplateFormatVersion : '2010-09-09' -Transform: AWS::Serverless-2016-10-31 -Description: A sample SAM template for deploying Lambda functions. - -Resources: -# CurrencyExchange Function - currencyExchangeFunction: - Type: AWS::Serverless::Function - Properties: - Handler: Provided - Runtime: provided - CodeUri: ../../.build/lambda/CurrencyExchange/lambda.zip - Timeout: 300 -# Instructs new versions to be published to an alias named "live". - AutoPublishAlias: live diff --git a/Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml b/Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml deleted file mode 100644 index c277ec72..00000000 --- a/Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml +++ /dev/null @@ -1,14 +0,0 @@ -AWSTemplateFormatVersion : '2010-09-09' -Transform: AWS::Serverless-2016-10-31 -Description: A sample SAM template for deploying Lambda functions. - -Resources: -# ErrorHandling Function - errorHandlingFunction: - Type: AWS::Serverless::Function - Properties: - Handler: Provided - Runtime: provided - CodeUri: ../../.build/lambda/ErrorHandling/lambda.zip -# Instructs new versions to be published to an alias named "live". - AutoPublishAlias: live diff --git a/Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml b/Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml deleted file mode 100644 index 7e5c6b09..00000000 --- a/Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml +++ /dev/null @@ -1,20 +0,0 @@ -service: currency-swift-aws - -package: - artifact: .build/lambda/CurrencyExchange/lambda.zip - -provider: - name: aws - runtime: provided - iamRoleStatements: - - Effect: Allow - Action: - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Resource: "*" - -functions: - currencyExchangeFunction: - handler: CurrencyExchange - memorySize: 128 \ No newline at end of file diff --git a/Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml b/Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml deleted file mode 100644 index 367be490..00000000 --- a/Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml +++ /dev/null @@ -1,20 +0,0 @@ -service: errorhandling-swift-aws - -package: - artifact: .build/lambda/ErrorHandling/lambda.zip - -provider: - name: aws - runtime: provided - iamRoleStatements: - - Effect: Allow - Action: - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Resource: "*" - -functions: - errorHandlingFunction: - handler: ErrorHandling - memorySize: 128 \ No newline at end of file diff --git a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift b/Examples/LocalDebugging/MyLambda/Lambda.swift similarity index 96% rename from Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift rename to Examples/LocalDebugging/MyLambda/Lambda.swift index 3d4c88d3..b6a5d865 100644 --- a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift +++ b/Examples/LocalDebugging/MyLambda/Lambda.swift @@ -17,8 +17,9 @@ import Shared // set LOCAL_LAMBDA_SERVER_ENABLED env variable to "true" to start // a local server simulator which will allow local debugging + @main -struct MyLambdaHandler: LambdaHandler { +struct MyLambda: LambdaHandler { typealias Event = Request typealias Output = Response diff --git a/Examples/LocalDebugging/MyLambda/Package.swift b/Examples/LocalDebugging/MyLambda/Package.swift index 17de9dcb..2b8860de 100644 --- a/Examples/LocalDebugging/MyLambda/Package.swift +++ b/Examples/LocalDebugging/MyLambda/Package.swift @@ -19,10 +19,12 @@ let package = Package( ], targets: [ .executableTarget( - name: "MyLambda", dependencies: [ + name: "MyLambda", + dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), .product(name: "Shared", package: "Shared"), - ] + ], + path: "." ), ] ) diff --git a/Examples/LocalDebugging/MyLambda/Tests/LinuxMain.swift b/Examples/LocalDebugging/MyLambda/Tests/LinuxMain.swift deleted file mode 100644 index c46de763..00000000 --- a/Examples/LocalDebugging/MyLambda/Tests/LinuxMain.swift +++ /dev/null @@ -1,15 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -preconditionFailure("use `swift test --enable-test-discovery`") diff --git a/Examples/LocalDebugging/Shared/Tests/LinuxMain.swift b/Examples/LocalDebugging/Shared/Tests/LinuxMain.swift deleted file mode 100644 index c46de763..00000000 --- a/Examples/LocalDebugging/Shared/Tests/LinuxMain.swift +++ /dev/null @@ -1,15 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -preconditionFailure("use `swift test --enable-test-discovery`") diff --git a/Examples/Testing/Package.swift b/Examples/Testing/Package.swift new file mode 100644 index 00000000..5f7cb131 --- /dev/null +++ b/Examples/Testing/Package.swift @@ -0,0 +1,30 @@ +// 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"), + .product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"), + ], + path: "Sources" + ), + .testTarget(name: "MyLambdaTests", dependencies: ["MyLambda"], path: "Tests"), + ] +) diff --git a/Sources/StringSample/main.swift b/Examples/Testing/Sources/Lambda.swift similarity index 61% rename from Sources/StringSample/main.swift rename to Examples/Testing/Sources/Lambda.swift index d80820f8..20d62aac 100644 --- a/Sources/StringSample/main.swift +++ b/Examples/Testing/Sources/Lambda.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftAWSLambdaRuntime open source project // -// Copyright (c) 2017-2018 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 @@ -12,18 +12,21 @@ // //===----------------------------------------------------------------------===// -import AWSLambdaRuntimeCore -import NIOCore +import AWSLambdaRuntime // in this example we are receiving and responding with strings -struct Handler: EventLoopLambdaHandler { + +@main +struct MyLambda: LambdaHandler { typealias Event = String typealias Output = String - func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { + init(context: Lambda.InitializationContext) async throws { + // setup your resources that you want to reuse for every invocation here. + } + + func handle(_ event: String, context: Lambda.Context) async throws -> String { // as an example, respond with the event's reversed body - context.eventLoop.makeSucceededFuture(String(event.reversed())) + String(event.reversed()) } } - -Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) } diff --git a/Examples/LambdaFunctions/Tests/LinuxMain.swift b/Examples/Testing/Tests/LambdaTests.swift similarity index 55% rename from Examples/LambdaFunctions/Tests/LinuxMain.swift rename to Examples/Testing/Tests/LambdaTests.swift index c46de763..26d6ea38 100644 --- a/Examples/LambdaFunctions/Tests/LinuxMain.swift +++ b/Examples/Testing/Tests/LambdaTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftAWSLambdaRuntime open source project // -// Copyright (c) 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 @@ -12,4 +12,15 @@ // //===----------------------------------------------------------------------===// -preconditionFailure("use `swift test --enable-test-discovery`") +import AWSLambdaRuntime +import AWSLambdaTesting +@testable import MyLambda +import XCTest + +class LambdaTest: XCTestCase { + func testIt() throws { + let input = UUID().uuidString + let result = try Lambda.test(MyLambda.self, with: input) + XCTAssertEqual(result, String(input.reversed())) + } +} diff --git a/Package.swift b/Package.swift index 270d9a82..ca0db60e 100644 --- a/Package.swift +++ b/Package.swift @@ -51,7 +51,5 @@ let package = Package( .product(name: "NIOHTTP1", package: "swift-nio"), .product(name: "NIO", package: "swift-nio"), ]), - .target(name: "StringSample", dependencies: ["AWSLambdaRuntime"]), - .target(name: "CodableSample", dependencies: ["AWSLambdaRuntime"]), ] ) diff --git a/Sources/AWSLambdaTesting/Lambda+Testing.swift b/Sources/AWSLambdaTesting/Lambda+Testing.swift index bd6e7c51..d703cb31 100644 --- a/Sources/AWSLambdaTesting/Lambda+Testing.swift +++ b/Sources/AWSLambdaTesting/Lambda+Testing.swift @@ -13,18 +13,16 @@ //===----------------------------------------------------------------------===// // This functionality is designed to help with Lambda unit testing with XCTest -// #if filter required for release builds which do not support @testable import -// @testable is used to access of internal functions -// For exmaple: +// For example: // // func test() { // struct MyLambda: LambdaHandler { -// typealias In = String -// typealias Out = String +// typealias Event = String +// typealias Output = String // // init(context: Lambda.InitializationContext) {} // -// func handle(event: String, context: Lambda.Context) async throws -> String { +// func handle(_ event: String, context: Lambda.Context) async throws -> String { // "echo" + event // } // } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift index 67c12ae0..e2ba66f2 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift @@ -16,8 +16,8 @@ import AWSLambdaRuntimeCore import NIOCore struct EchoHandler: EventLoopLambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) @@ -25,8 +25,8 @@ struct EchoHandler: EventLoopLambdaHandler { } struct FailedHandler: EventLoopLambdaHandler { - typealias In = String - typealias Out = Void + typealias Event = String + typealias Output = Void private let reason: String diff --git a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift index 770f1efb..6471ca1b 100644 --- a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift +++ b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift @@ -145,7 +145,7 @@ class CodableLambdaTest: XCTestCase { } #endif - // convencience method + // convenience method func newContext() -> Lambda.Context { Lambda.Context(requestID: UUID().uuidString, traceID: "abc123", diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift deleted file mode 100644 index 58f2ccfd..00000000 --- a/Tests/LinuxMain.swift +++ /dev/null @@ -1,15 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftAWSLambdaRuntime open source project -// -// Copyright (c) 2017-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 -// -//===----------------------------------------------------------------------===// - -preconditionFailure("use `swift test --enable-test-discovery`") diff --git a/docker/docker-compose.al2.main.yaml b/docker/docker-compose.al2.main.yaml index 741c8a43..c63a13e2 100644 --- a/docker/docker-compose.al2.main.yaml +++ b/docker/docker-compose.al2.main.yaml @@ -10,14 +10,9 @@ services: test: image: swift-aws-lambda:al2-main - command: /bin/bash -cl "swift test --enable-test-discovery -Xswiftc -warnings-as-errors $${SANITIZER_ARG-} -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency" test-samples: image: swift-aws-lambda:al2-main - command: >- - /bin/bash -clx " - swift build -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency --package-path Examples/LambdaFunctions && - swift build -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency --package-path Examples/LocalDebugging/MyLambda" shell: image: swift-aws-lambda:al2-main diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 122ccd9d..48004fc6 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -1,6 +1,6 @@ # this file is not designed to be run directly # instead, use the docker-compose.. files -# eg docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.al2.52.yaml run test +# eg docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.al2.55.yaml run test version: "3" services: @@ -34,8 +34,15 @@ services: <<: *common command: >- /bin/bash -clx " - swift build --package-path Examples/LambdaFunctions && - swift build --package-path Examples/LocalDebugging/MyLambda" + swift build --package-path Examples/Benchmark && + swift build --package-path Examples/Deployment && + swift build --package-path Examples/Echo && + swift build --package-path Examples/ErrorHandling && + swift build --package-path Examples/Foundation && + swift build --package-path Examples/JSON && + swift build --package-path Examples/LocalDebugging/MyLambda && + swift test --package-path Examples/Testing + " # util diff --git a/scripts/soundness.sh b/scripts/soundness.sh index 71d43e44..4fb39549 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -19,7 +19,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" function replace_acceptable_years() { # this needs to replace all acceptable forms with 'YEARS' - sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' + sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/' } printf "=> Checking for unacceptable language... "