From dc3daaffc7775b9c9a8663b39c2032bec5259087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 15:47:26 +0200 Subject: [PATCH 01/10] Restructure Examples folder --- .gitignore | 1 + Examples/APIGateway/.gitignore | 2 + Examples/APIGateway/Package.swift | 55 +++++++ Examples/APIGateway/README.md | 1 + Examples/APIGateway/Sources/main.swift | 34 ++++ Examples/APIGateway/template.yaml | 31 ++++ Examples/Benchmark/Package.swift | 34 ---- Examples/HelloWorld/.gitignore | 4 + Examples/HelloWorld/Package.swift | 53 +++++++ Examples/HelloWorld/README.md | 0 Examples/HelloWorld/Sources/main.swift | 23 +++ .../{ => v1}/Benchmark/BenchmarkHandler.swift | 0 Examples/v1/Benchmark/Package.swift | 53 +++++++ Examples/{ => v1}/Deployment/.dockerignore | 0 Examples/{ => v1}/Deployment/Dockerfile | 0 Examples/{ => v1}/Deployment/Package.swift | 2 +- Examples/{ => v1}/Deployment/README.md | 0 .../Sources/Benchmark/BenchmarkHandler.swift | 0 .../HelloWorld/HelloWorldHandler.swift | 0 .../scripts/SAM/Benchmark-template.yml | 0 .../scripts/SAM/HelloWorld-template.yml | 0 .../Deployment/scripts/build-and-package.sh | 0 .../{ => v1}/Deployment/scripts/config.sh | 0 .../{ => v1}/Deployment/scripts/deploy.sh | 0 .../{ => v1}/Deployment/scripts/package.sh | 0 .../{ => v1}/Deployment/scripts/sam-deploy.sh | 0 .../Deployment/scripts/serverless-deploy.sh | 0 .../Deployment/scripts/serverless-remove.sh | 0 .../scripts/serverless/Benchmark-template.yml | 0 .../serverless/HelloWorld-template.yml | 0 Examples/{ => v1}/Echo/Lambda.swift | 0 Examples/{ => v1}/Echo/Package.swift | 0 Examples/{ => v1}/ErrorHandling/Lambda.swift | 0 Examples/{ => v1}/ErrorHandling/Package.swift | 0 Examples/{ => v1}/Foundation/Lambda.swift | 0 Examples/{ => v1}/Foundation/Package.swift | 0 Examples/{ => v1}/JSON/Lambda.swift | 0 Examples/{ => v1}/JSON/Package.swift | 0 .../contents.xcworkspacedata | 0 .../xcshareddata/IDEWorkspaceChecks.plist | 0 .../xcshareddata/WorkspaceSettings.xcsettings | 0 .../MyApp/MyApp.xcodeproj/project.pbxproj | 0 .../AppIcon.appiconset/Contents.json | 0 .../MyApp/MyApp/Assets.xcassets/Contents.json | 0 .../MyApp/MyApp/ContentView.swift | 0 .../LocalDebugging/MyApp/MyApp/MyApp.swift | 0 .../Preview Assets.xcassets/Contents.json | 0 .../LocalDebugging/MyLambda/.dockerignore | 0 .../LocalDebugging/MyLambda/Dockerfile | 0 .../LocalDebugging/MyLambda/Lambda.swift | 0 .../LocalDebugging/MyLambda/Package.swift | 0 .../LocalDebugging/MyLambda/scripts/deploy.sh | 0 .../MyLambda/scripts/package.sh | 0 Examples/{ => v1}/LocalDebugging/README.md | 0 .../LocalDebugging/Shared/Package.swift | 0 .../Shared/Sources/Shared/Shared.swift | 0 .../{ => v1}/ResourcePackaging/Lambda.swift | 0 .../{ => v1}/ResourcePackaging/Package.swift | 0 Examples/{ => v1}/ResourcePackaging/hello.txt | 0 Examples/v1/SAM/notes.md | 148 ++++++++++++++++++ Examples/v1/SAM/samconfig.toml | 8 + Examples/v1/SAM/template.yaml | 68 ++++++++ Examples/{ => v1}/Testing/Package.swift | 0 .../{ => v1}/Testing/Sources/Lambda.swift | 0 .../{ => v1}/Testing/Tests/LambdaTests.swift | 0 65 files changed, 482 insertions(+), 35 deletions(-) create mode 100644 Examples/APIGateway/.gitignore create mode 100644 Examples/APIGateway/Package.swift create mode 100644 Examples/APIGateway/README.md create mode 100644 Examples/APIGateway/Sources/main.swift create mode 100644 Examples/APIGateway/template.yaml delete mode 100644 Examples/Benchmark/Package.swift create mode 100644 Examples/HelloWorld/.gitignore create mode 100644 Examples/HelloWorld/Package.swift create mode 100644 Examples/HelloWorld/README.md create mode 100644 Examples/HelloWorld/Sources/main.swift rename Examples/{ => v1}/Benchmark/BenchmarkHandler.swift (100%) create mode 100644 Examples/v1/Benchmark/Package.swift rename Examples/{ => v1}/Deployment/.dockerignore (100%) rename Examples/{ => v1}/Deployment/Dockerfile (100%) rename Examples/{ => v1}/Deployment/Package.swift (96%) rename Examples/{ => v1}/Deployment/README.md (100%) rename Examples/{ => v1}/Deployment/Sources/Benchmark/BenchmarkHandler.swift (100%) rename Examples/{ => v1}/Deployment/Sources/HelloWorld/HelloWorldHandler.swift (100%) rename Examples/{ => v1}/Deployment/scripts/SAM/Benchmark-template.yml (100%) rename Examples/{ => v1}/Deployment/scripts/SAM/HelloWorld-template.yml (100%) rename Examples/{ => v1}/Deployment/scripts/build-and-package.sh (100%) rename Examples/{ => v1}/Deployment/scripts/config.sh (100%) rename Examples/{ => v1}/Deployment/scripts/deploy.sh (100%) rename Examples/{ => v1}/Deployment/scripts/package.sh (100%) rename Examples/{ => v1}/Deployment/scripts/sam-deploy.sh (100%) rename Examples/{ => v1}/Deployment/scripts/serverless-deploy.sh (100%) rename Examples/{ => v1}/Deployment/scripts/serverless-remove.sh (100%) rename Examples/{ => v1}/Deployment/scripts/serverless/Benchmark-template.yml (100%) rename Examples/{ => v1}/Deployment/scripts/serverless/HelloWorld-template.yml (100%) rename Examples/{ => v1}/Echo/Lambda.swift (100%) rename Examples/{ => v1}/Echo/Package.swift (100%) rename Examples/{ => v1}/ErrorHandling/Lambda.swift (100%) rename Examples/{ => v1}/ErrorHandling/Package.swift (100%) rename Examples/{ => v1}/Foundation/Lambda.swift (100%) rename Examples/{ => v1}/Foundation/Package.swift (100%) rename Examples/{ => v1}/JSON/Lambda.swift (100%) rename Examples/{ => v1}/JSON/Package.swift (100%) rename Examples/{ => v1}/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata (100%) rename Examples/{ => v1}/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (100%) rename Examples/{ => v1}/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp/ContentView.swift (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp/MyApp.swift (100%) rename Examples/{ => v1}/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/.dockerignore (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/Dockerfile (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/Lambda.swift (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/Package.swift (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/scripts/deploy.sh (100%) rename Examples/{ => v1}/LocalDebugging/MyLambda/scripts/package.sh (100%) rename Examples/{ => v1}/LocalDebugging/README.md (100%) rename Examples/{ => v1}/LocalDebugging/Shared/Package.swift (100%) rename Examples/{ => v1}/LocalDebugging/Shared/Sources/Shared/Shared.swift (100%) rename Examples/{ => v1}/ResourcePackaging/Lambda.swift (100%) rename Examples/{ => v1}/ResourcePackaging/Package.swift (100%) rename Examples/{ => v1}/ResourcePackaging/hello.txt (100%) create mode 100644 Examples/v1/SAM/notes.md create mode 100644 Examples/v1/SAM/samconfig.toml create mode 100644 Examples/v1/SAM/template.yaml rename Examples/{ => v1}/Testing/Package.swift (100%) rename Examples/{ => v1}/Testing/Sources/Lambda.swift (100%) rename Examples/{ => v1}/Testing/Tests/LambdaTests.swift (100%) diff --git a/.gitignore b/.gitignore index 000f5669..a5baf2c5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ xcuserdata Package.resolved .serverless .vscode +Makefile diff --git a/Examples/APIGateway/.gitignore b/Examples/APIGateway/.gitignore new file mode 100644 index 00000000..e4044f6f --- /dev/null +++ b/Examples/APIGateway/.gitignore @@ -0,0 +1,2 @@ +samconfig.toml +Makefile diff --git a/Examples/APIGateway/Package.swift b/Examples/APIGateway/Package.swift new file mode 100644 index 00000000..a2df5b73 --- /dev/null +++ b/Examples/APIGateway/Package.swift @@ -0,0 +1,55 @@ +// swift-tools-version:6.0 + +import PackageDescription + +// needed for CI to test the local version of the library +import class Foundation.ProcessInfo +import struct Foundation.URL + +#if os(macOS) +let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] +#else +let platforms: [PackageDescription.SupportedPlatform]? = nil +#endif + +let package = Package( + name: "swift-aws-lambda-runtime-example", + platforms: platforms, + products: [ + .executable(name: "APIGAtewayLambda", targets: ["APIGAtewayLambda"]) + ], + dependencies: [ + // dependency on swift-aws-lambda-runtime is added dynamically below + // .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + + .package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main") + ], + targets: [ + .executableTarget( + name: "APIGAtewayLambda", + dependencies: [ + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), + .product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"), + ], + path: "." + ) + ] +) + +if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], + localDepsPath != "", + let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), + let _ = v.isDirectory +{ + print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies += [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] + +} else { + print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") + print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub") + package.dependencies += [ + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + ] +} diff --git a/Examples/APIGateway/README.md b/Examples/APIGateway/README.md new file mode 100644 index 00000000..f4fae2b3 --- /dev/null +++ b/Examples/APIGateway/README.md @@ -0,0 +1 @@ +TO BE DONE \ No newline at end of file diff --git a/Examples/APIGateway/Sources/main.swift b/Examples/APIGateway/Sources/main.swift new file mode 100644 index 00000000..425da83e --- /dev/null +++ b/Examples/APIGateway/Sources/main.swift @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2024 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 AWSLambdaEvents +import AWSLambdaRuntime +import class Foundation.JSONEncoder + +let runtime = LambdaRuntime { + (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in + + var header = HTTPHeaders() + context.logger.debug("HTTP API Message received") + + header["content-type"] = "application/json" + + // echo the request in the response + let data = try JSONEncoder().encode(event) + let response = String(data: data, encoding: .utf8) + + return APIGatewayV2Response(statusCode: .ok, headers: header, body: response) +} + +try await runtime.run() diff --git a/Examples/APIGateway/template.yaml b/Examples/APIGateway/template.yaml new file mode 100644 index 00000000..700c09b0 --- /dev/null +++ b/Examples/APIGateway/template.yaml @@ -0,0 +1,31 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: SAM Template for QuoteService + +Resources: + # Lambda function + APIGAtewayLambda: + Type: AWS::Serverless::Function + Properties: + CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGAtewayLambda/APIGAtewayLambda.zip + Timeout: 60 + Handler: swift.bootstrap + Runtime: provided.al2 + MemorySize: 512 + Architectures: + - arm64 + Environment: + Variables: + # by default, AWS Lambda runtime produces no log + # use `LOG_LEVEL: debug` for for lifecycle and event handling information + # use `LOG_LEVEL: trace` for detailed input event information + LOG_LEVEL: trace + Events: + HttpApiEvent: + Type: HttpApi + +Outputs: + # print API Gateway endpoint + APIGAtewayEndpoint: + Description: API Gateway endpoint UR" + Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com" diff --git a/Examples/Benchmark/Package.swift b/Examples/Benchmark/Package.swift deleted file mode 100644 index b1fd0840..00000000 --- a/Examples/Benchmark/Package.swift +++ /dev/null @@ -1,34 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/HelloWorld/.gitignore b/Examples/HelloWorld/.gitignore new file mode 100644 index 00000000..e41d0be5 --- /dev/null +++ b/Examples/HelloWorld/.gitignore @@ -0,0 +1,4 @@ +response.json +samconfig.toml +template.yaml +Makefile diff --git a/Examples/HelloWorld/Package.swift b/Examples/HelloWorld/Package.swift new file mode 100644 index 00000000..305d85e6 --- /dev/null +++ b/Examples/HelloWorld/Package.swift @@ -0,0 +1,53 @@ +// swift-tools-version:6.0 + +import PackageDescription + +#if os(macOS) +let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] +#else +let platforms: [PackageDescription.SupportedPlatform]? = nil +#endif + +// needed for CI to test the local version of the library +import class Foundation.ProcessInfo +import struct Foundation.URL + +let package = Package( + name: "swift-aws-lambda-runtime-example", + platforms: platforms, + products: [ + .executable(name: "MyLambda", targets: ["MyLambda"]) + ], + dependencies: [ + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + ], + targets: [ + .executableTarget( + name: "MyLambda", + dependencies: [ + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") + ], + path: "." + ) + ] +) + +if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" { + + print("++++++++ \(localDepsPath)") + + // check if directory exists + let u = URL(fileURLWithPath: localDepsPath) + if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! { + print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies = [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] + } else { + print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") + print("This project will compile against the main branch of the Lambda Runtime on GitHub") + } +} else { + print("++++++++ NO ENV VAR ") + +} diff --git a/Examples/HelloWorld/README.md b/Examples/HelloWorld/README.md new file mode 100644 index 00000000..e69de29b diff --git a/Examples/HelloWorld/Sources/main.swift b/Examples/HelloWorld/Sources/main.swift new file mode 100644 index 00000000..a2c92bc2 --- /dev/null +++ b/Examples/HelloWorld/Sources/main.swift @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2024 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 + +let runtime = LambdaRuntime { (event: String, context: LambdaContext) in + return "Hello \(event)" +} + +try await runtime.run() diff --git a/Examples/Benchmark/BenchmarkHandler.swift b/Examples/v1/Benchmark/BenchmarkHandler.swift similarity index 100% rename from Examples/Benchmark/BenchmarkHandler.swift rename to Examples/v1/Benchmark/BenchmarkHandler.swift diff --git a/Examples/v1/Benchmark/Package.swift b/Examples/v1/Benchmark/Package.swift new file mode 100644 index 00000000..53bb1f8a --- /dev/null +++ b/Examples/v1/Benchmark/Package.swift @@ -0,0 +1,53 @@ +// swift-tools-version:5.7 + +import PackageDescription + +// needed for CI to test the local version of the library +import class Foundation.ProcessInfo +import struct Foundation.URL + +let runtimeVersion = Version("1.0.0-alpha.3") + +let package = Package( + name: "swift-aws-lambda-runtime-example", + platforms: [ + .macOS(.v12) + ], + products: [ + .executable(name: "MyLambda", targets: ["MyLambda"]) + ], + dependencies: [ + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: runtimeVersion) + ], + targets: [ + .executableTarget( + name: "MyLambda", + dependencies: [ + .product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime") + ], + path: "." + ) + ] +) + +// for CI to test the local version of the library +// if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { +// print("LAMBDA_USE_LOCAL_DEPS is ignored for runtime v1 examples.") +// print("This project will compile against runtime version \(runtimeVersion)") +// } + +if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" { + + // check if directory exists + let u = URL(fileURLWithPath: localDepsPath) + if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! { + print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies = [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] + } else { + print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") + print("This project will compile against runtime version \(runtimeVersion)") + } + +} \ No newline at end of file diff --git a/Examples/Deployment/.dockerignore b/Examples/v1/Deployment/.dockerignore similarity index 100% rename from Examples/Deployment/.dockerignore rename to Examples/v1/Deployment/.dockerignore diff --git a/Examples/Deployment/Dockerfile b/Examples/v1/Deployment/Dockerfile similarity index 100% rename from Examples/Deployment/Dockerfile rename to Examples/v1/Deployment/Dockerfile diff --git a/Examples/Deployment/Package.swift b/Examples/v1/Deployment/Package.swift similarity index 96% rename from Examples/Deployment/Package.swift rename to Examples/v1/Deployment/Package.swift index b5351201..afcff025 100644 --- a/Examples/Deployment/Package.swift +++ b/Examples/v1/Deployment/Package.swift @@ -17,7 +17,7 @@ let package = Package( // demonstrate different types of error handling ], dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3") ], targets: [ .executableTarget( diff --git a/Examples/Deployment/README.md b/Examples/v1/Deployment/README.md similarity index 100% rename from Examples/Deployment/README.md rename to Examples/v1/Deployment/README.md diff --git a/Examples/Deployment/Sources/Benchmark/BenchmarkHandler.swift b/Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift similarity index 100% rename from Examples/Deployment/Sources/Benchmark/BenchmarkHandler.swift rename to Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift diff --git a/Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift similarity index 100% rename from Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift rename to Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift diff --git a/Examples/Deployment/scripts/SAM/Benchmark-template.yml b/Examples/v1/Deployment/scripts/SAM/Benchmark-template.yml similarity index 100% rename from Examples/Deployment/scripts/SAM/Benchmark-template.yml rename to Examples/v1/Deployment/scripts/SAM/Benchmark-template.yml diff --git a/Examples/Deployment/scripts/SAM/HelloWorld-template.yml b/Examples/v1/Deployment/scripts/SAM/HelloWorld-template.yml similarity index 100% rename from Examples/Deployment/scripts/SAM/HelloWorld-template.yml rename to Examples/v1/Deployment/scripts/SAM/HelloWorld-template.yml diff --git a/Examples/Deployment/scripts/build-and-package.sh b/Examples/v1/Deployment/scripts/build-and-package.sh similarity index 100% rename from Examples/Deployment/scripts/build-and-package.sh rename to Examples/v1/Deployment/scripts/build-and-package.sh diff --git a/Examples/Deployment/scripts/config.sh b/Examples/v1/Deployment/scripts/config.sh similarity index 100% rename from Examples/Deployment/scripts/config.sh rename to Examples/v1/Deployment/scripts/config.sh diff --git a/Examples/Deployment/scripts/deploy.sh b/Examples/v1/Deployment/scripts/deploy.sh similarity index 100% rename from Examples/Deployment/scripts/deploy.sh rename to Examples/v1/Deployment/scripts/deploy.sh diff --git a/Examples/Deployment/scripts/package.sh b/Examples/v1/Deployment/scripts/package.sh similarity index 100% rename from Examples/Deployment/scripts/package.sh rename to Examples/v1/Deployment/scripts/package.sh diff --git a/Examples/Deployment/scripts/sam-deploy.sh b/Examples/v1/Deployment/scripts/sam-deploy.sh similarity index 100% rename from Examples/Deployment/scripts/sam-deploy.sh rename to Examples/v1/Deployment/scripts/sam-deploy.sh diff --git a/Examples/Deployment/scripts/serverless-deploy.sh b/Examples/v1/Deployment/scripts/serverless-deploy.sh similarity index 100% rename from Examples/Deployment/scripts/serverless-deploy.sh rename to Examples/v1/Deployment/scripts/serverless-deploy.sh diff --git a/Examples/Deployment/scripts/serverless-remove.sh b/Examples/v1/Deployment/scripts/serverless-remove.sh similarity index 100% rename from Examples/Deployment/scripts/serverless-remove.sh rename to Examples/v1/Deployment/scripts/serverless-remove.sh diff --git a/Examples/Deployment/scripts/serverless/Benchmark-template.yml b/Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml similarity index 100% rename from Examples/Deployment/scripts/serverless/Benchmark-template.yml rename to Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml diff --git a/Examples/Deployment/scripts/serverless/HelloWorld-template.yml b/Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml similarity index 100% rename from Examples/Deployment/scripts/serverless/HelloWorld-template.yml rename to Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml diff --git a/Examples/Echo/Lambda.swift b/Examples/v1/Echo/Lambda.swift similarity index 100% rename from Examples/Echo/Lambda.swift rename to Examples/v1/Echo/Lambda.swift diff --git a/Examples/Echo/Package.swift b/Examples/v1/Echo/Package.swift similarity index 100% rename from Examples/Echo/Package.swift rename to Examples/v1/Echo/Package.swift diff --git a/Examples/ErrorHandling/Lambda.swift b/Examples/v1/ErrorHandling/Lambda.swift similarity index 100% rename from Examples/ErrorHandling/Lambda.swift rename to Examples/v1/ErrorHandling/Lambda.swift diff --git a/Examples/ErrorHandling/Package.swift b/Examples/v1/ErrorHandling/Package.swift similarity index 100% rename from Examples/ErrorHandling/Package.swift rename to Examples/v1/ErrorHandling/Package.swift diff --git a/Examples/Foundation/Lambda.swift b/Examples/v1/Foundation/Lambda.swift similarity index 100% rename from Examples/Foundation/Lambda.swift rename to Examples/v1/Foundation/Lambda.swift diff --git a/Examples/Foundation/Package.swift b/Examples/v1/Foundation/Package.swift similarity index 100% rename from Examples/Foundation/Package.swift rename to Examples/v1/Foundation/Package.swift diff --git a/Examples/JSON/Lambda.swift b/Examples/v1/JSON/Lambda.swift similarity index 100% rename from Examples/JSON/Lambda.swift rename to Examples/v1/JSON/Lambda.swift diff --git a/Examples/JSON/Package.swift b/Examples/v1/JSON/Package.swift similarity index 100% rename from Examples/JSON/Package.swift rename to Examples/v1/JSON/Package.swift diff --git a/Examples/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata b/Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata similarity index 100% rename from Examples/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata rename to Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata diff --git a/Examples/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from Examples/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Examples/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings similarity index 100% rename from Examples/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings rename to Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj b/Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj rename to Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj diff --git a/Examples/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json rename to Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json diff --git a/Examples/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json rename to Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json diff --git a/Examples/LocalDebugging/MyApp/MyApp/ContentView.swift b/Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp/ContentView.swift rename to Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift diff --git a/Examples/LocalDebugging/MyApp/MyApp/MyApp.swift b/Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp/MyApp.swift rename to Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift diff --git a/Examples/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json similarity index 100% rename from Examples/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json rename to Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json diff --git a/Examples/LocalDebugging/MyLambda/.dockerignore b/Examples/v1/LocalDebugging/MyLambda/.dockerignore similarity index 100% rename from Examples/LocalDebugging/MyLambda/.dockerignore rename to Examples/v1/LocalDebugging/MyLambda/.dockerignore diff --git a/Examples/LocalDebugging/MyLambda/Dockerfile b/Examples/v1/LocalDebugging/MyLambda/Dockerfile similarity index 100% rename from Examples/LocalDebugging/MyLambda/Dockerfile rename to Examples/v1/LocalDebugging/MyLambda/Dockerfile diff --git a/Examples/LocalDebugging/MyLambda/Lambda.swift b/Examples/v1/LocalDebugging/MyLambda/Lambda.swift similarity index 100% rename from Examples/LocalDebugging/MyLambda/Lambda.swift rename to Examples/v1/LocalDebugging/MyLambda/Lambda.swift diff --git a/Examples/LocalDebugging/MyLambda/Package.swift b/Examples/v1/LocalDebugging/MyLambda/Package.swift similarity index 100% rename from Examples/LocalDebugging/MyLambda/Package.swift rename to Examples/v1/LocalDebugging/MyLambda/Package.swift diff --git a/Examples/LocalDebugging/MyLambda/scripts/deploy.sh b/Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh similarity index 100% rename from Examples/LocalDebugging/MyLambda/scripts/deploy.sh rename to Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh diff --git a/Examples/LocalDebugging/MyLambda/scripts/package.sh b/Examples/v1/LocalDebugging/MyLambda/scripts/package.sh similarity index 100% rename from Examples/LocalDebugging/MyLambda/scripts/package.sh rename to Examples/v1/LocalDebugging/MyLambda/scripts/package.sh diff --git a/Examples/LocalDebugging/README.md b/Examples/v1/LocalDebugging/README.md similarity index 100% rename from Examples/LocalDebugging/README.md rename to Examples/v1/LocalDebugging/README.md diff --git a/Examples/LocalDebugging/Shared/Package.swift b/Examples/v1/LocalDebugging/Shared/Package.swift similarity index 100% rename from Examples/LocalDebugging/Shared/Package.swift rename to Examples/v1/LocalDebugging/Shared/Package.swift diff --git a/Examples/LocalDebugging/Shared/Sources/Shared/Shared.swift b/Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift similarity index 100% rename from Examples/LocalDebugging/Shared/Sources/Shared/Shared.swift rename to Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift diff --git a/Examples/ResourcePackaging/Lambda.swift b/Examples/v1/ResourcePackaging/Lambda.swift similarity index 100% rename from Examples/ResourcePackaging/Lambda.swift rename to Examples/v1/ResourcePackaging/Lambda.swift diff --git a/Examples/ResourcePackaging/Package.swift b/Examples/v1/ResourcePackaging/Package.swift similarity index 100% rename from Examples/ResourcePackaging/Package.swift rename to Examples/v1/ResourcePackaging/Package.swift diff --git a/Examples/ResourcePackaging/hello.txt b/Examples/v1/ResourcePackaging/hello.txt similarity index 100% rename from Examples/ResourcePackaging/hello.txt rename to Examples/v1/ResourcePackaging/hello.txt diff --git a/Examples/v1/SAM/notes.md b/Examples/v1/SAM/notes.md new file mode 100644 index 00000000..b99d614b --- /dev/null +++ b/Examples/v1/SAM/notes.md @@ -0,0 +1,148 @@ +swift package --disable-sandbox archive --products HttpApiLambda --output-path dist + +# change line 115 in archive plugin +# // FIXME: temporary change to be able to build from the mac command line +# arguments: ["run", "--rm", "-v", "\(packageDirectory.string)/../..:/workspace", "-w", "/workspace/Examples/SAM", baseImage, "bash", "-cl", buildCommand], + +https://www.hackingwithswift.com/swift/5.4/result-builders +https://github.com/carson-katri/awesome-result-builders#server-side +https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md +https://developer.apple.com/videos/play/wwdc2021/10253/ + +From +https://github.com/apple/swift-package-manager/compare/main...tomerd:swift-package-manager:feature/manifest2-poc + +https://forums.swift.org/t/pre-pitch-swiftpm-manifest-based-on-result-builders/53457 + +``` +public class Package: Codable { + public var modules: [Module] + public var dependencies: [Dependency] + public var minimumDeploymentTargets: [DeploymentTarget] + + public init() { + self.modules = [] + self.dependencies = [] + self.minimumDeploymentTargets = [] + // register for serialization + __packages.append(self) + // FIXME: get rid of atexit in favor of more reliable solution + atexit { + if CommandLine.arguments.first?.contains("-manifest") ?? false { + try! PackageSerializer.serialize(__packages.last!) + } + } + } +``` + +``` +#if canImport(Glibc) +@_implementationOnly import Glibc +#elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS) +@_implementationOnly import Darwin.C +#elseif os(Windows) +@_implementationOnly import ucrt +@_implementationOnly import struct WinSDK.HANDLE +#endif +import Foundation + +// Adds an main handler to cause the package's JSON representation +// to be written to a file when executed as a program. +// Emitting it to a separate file descriptor from stdout +// keeps any of the manifest's stdout output from interfering with it. +// +// Warning: The `-fileno` flag is a contract between PackageDescription +// and libSwiftPM, and since different versions of the two can be used +// together, it isn't safe to rename or remove it. +// +// Note: `-fileno` is not viable on Windows. Instead, we pass the file +// handle through the `-handle` option. + +struct PackageSerializer { + static func serialize(_ package: Package) throws { + #if os(Windows) + guard let index = CommandLine.arguments.firstIndex(of: "-handle"), let handleId = Int(CommandLine.arguments[index + 1], radix: 16) else { + throw PackageSerializerErrors.invalidArguments + } + // write serialized package to the file + guard let handle = HANDLE(bitPattern: handleId) else { + throw PackageSerializerErrors.invalidFileHandle + } + // NOTE: `_open_osfhandle` transfers ownership of the HANDLE to the file + // descriptor. DO NOT invoke `CloseHandle` on `hFile`. + let fd: CInt = _open_osfhandle(Int(bitPattern: handle), _O_APPEND) + // NOTE: `_fdopen` transfers ownership of the file descriptor to the + // `FILE *`. DO NOT invoke `_close` on the `fd`. + guard let filePipe = _fdopen(fileDescriptor, "w") else { + _close(fileDescriptor) + throw PackageSerializerErrors.failedOpeningFile + } + defer { + fclose(filePipe) + } + + fputs(try package.toJSON(), filePipe) + #else + guard let index = CommandLine.arguments.firstIndex(of: "-fileno"), let fileDescriptorId = Int32(CommandLine.arguments[index + 1]) else { + throw PackageSerializerErrors.invalidArguments + } + // write serialized package to the file + guard let fileDescriptor = fdopen(fileDescriptorId, "w") else { + throw PackageSerializerErrors.failedOpeningFile + } + defer { + fclose(fileDescriptor) + } + + fputs(try package.toJSON(), fileDescriptor) + #endif + } +} + +extension Package { + fileprivate func toJSON() throws -> String { + struct Output: Encodable { + let version = "1.0" // manifest JSON version + let package: Package + } + + let encoder = JSONEncoder() + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] + let data = try encoder.encode(Output(package: self)) + return String(data: data, encoding: .utf8)! + } +} + +private enum PackageSerializerErrors: Error { + case unknownPackage + case invalidArguments + case invalidFileHandle + case failedOpeningFile +} +``` + +Check https://forums.swift.org/t/pre-pitch-swiftpm-manifest-based-on-result-builders/53457/5 +to remove top level class + +``` +import PackageManifest + +Package { + ... +} +``` + +``` +// Within PackageManifest module + +public var package: Package? + +@resultBuilder +struct Package { + + @discardableResult + static func buildFinalResult(_ p: Package) -> Package { + package = p + } +} +``` \ No newline at end of file diff --git a/Examples/v1/SAM/samconfig.toml b/Examples/v1/SAM/samconfig.toml new file mode 100644 index 00000000..802bd0cf --- /dev/null +++ b/Examples/v1/SAM/samconfig.toml @@ -0,0 +1,8 @@ +version = 0.1 +[debug] +[debug.deploy] +[debug.deploy.parameters] +stack_name = "swift-aws-lambda-runtime-example" +region = "us-east-1" +capabilities = "CAPABILITY_IAM" +image_repositories = [] \ No newline at end of file diff --git a/Examples/v1/SAM/template.yaml b/Examples/v1/SAM/template.yaml new file mode 100644 index 00000000..f0165937 --- /dev/null +++ b/Examples/v1/SAM/template.yaml @@ -0,0 +1,68 @@ +Resources: + HttpApiLambda: + Type: AWS::Serverless::Function + Properties: + Events: + HttpApiEvent: + Type: HttpApi + Runtime: provided.al2 + Architectures: + - arm64 + Environment: + Variables: + NAME2: VALUE2 + LOG_LEVEL: debug + NAME1: VALUE1 + Handler: Provided + CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HttpApiLambda/HttpApiLambda.zip + Description: Description of this function + QueueSwiftLambdaQueueName: + Type: AWS::SQS::Queue + Properties: + QueueName: swift-lambda-queue-name + UrlLambda: + Type: AWS::Serverless::Function + Properties: + FunctionUrlConfig: + AuthType: AWS_IAM + Architectures: + - arm64 + Description: A Lambda function that is directly exposed as an URL, with IAM authentication + CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/UrlLambda/UrlLambda.zip + Runtime: provided.al2 + Handler: Provided + TopLevelQueueResource: + Type: AWS::SQS::Queue + Properties: + QueueName: swift-lambda-top-level-queue + SQSLambda: + Type: AWS::Serverless::Function + Properties: + Runtime: provided.al2 + Environment: + Variables: + LOG_LEVEL: debug + Architectures: + - arm64 + Events: + SQSEvent: + Type: SQS + Properties: + Queue: + Fn::GetAtt: + - QueueSwiftLambdaQueueName + - Arn + Enabled: true + BatchSize: 10 + Handler: Provided + CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SQSLambda/SQSLambda.zip + SwiftLambdaTable: + Type: AWS::Serverless::SimpleTable + Properties: + TableName: swift-lambda-table + PrimaryKey: + Type: String + Name: id +Transform: AWS::Serverless-2016-10-31 +AWSTemplateFormatVersion: 2010-09-09 +Description: Description of this deployment descriptor diff --git a/Examples/Testing/Package.swift b/Examples/v1/Testing/Package.swift similarity index 100% rename from Examples/Testing/Package.swift rename to Examples/v1/Testing/Package.swift diff --git a/Examples/Testing/Sources/Lambda.swift b/Examples/v1/Testing/Sources/Lambda.swift similarity index 100% rename from Examples/Testing/Sources/Lambda.swift rename to Examples/v1/Testing/Sources/Lambda.swift diff --git a/Examples/Testing/Tests/LambdaTests.swift b/Examples/v1/Testing/Tests/LambdaTests.swift similarity index 100% rename from Examples/Testing/Tests/LambdaTests.swift rename to Examples/v1/Testing/Tests/LambdaTests.swift From 499dbfd9666c7aa31a2f1bade2b4dfd62972104a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 16:24:44 +0200 Subject: [PATCH 02/10] fix hello world Package --- Examples/HelloWorld/Package.swift | 33 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Examples/HelloWorld/Package.swift b/Examples/HelloWorld/Package.swift index 305d85e6..b1eeaeae 100644 --- a/Examples/HelloWorld/Package.swift +++ b/Examples/HelloWorld/Package.swift @@ -19,7 +19,8 @@ let package = Package( .executable(name: "MyLambda", targets: ["MyLambda"]) ], dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + // dependency on swift-aws-lambda-runtime is added dynamically below + // .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") ], targets: [ .executableTarget( @@ -32,22 +33,20 @@ let package = Package( ] ) -if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" { - - print("++++++++ \(localDepsPath)") +if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], + localDepsPath != "", + let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), + let _ = v.isDirectory +{ + print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies += [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] - // check if directory exists - let u = URL(fileURLWithPath: localDepsPath) - if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! { - print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: localDepsPath) - ] - } else { - print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") - print("This project will compile against the main branch of the Lambda Runtime on GitHub") - } } else { - print("++++++++ NO ENV VAR ") - + print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") + print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub") + package.dependencies += [ + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + ] } From 0d3903bac48cdaefe75ffef7c61618c7eb199809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 16:29:56 +0200 Subject: [PATCH 03/10] add README --- Examples/HelloWorld/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Examples/HelloWorld/README.md b/Examples/HelloWorld/README.md index e69de29b..f4fae2b3 100644 --- a/Examples/HelloWorld/README.md +++ b/Examples/HelloWorld/README.md @@ -0,0 +1 @@ +TO BE DONE \ No newline at end of file From f32927f69a3087750416baac6bb73e7501567645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 17:29:38 +0200 Subject: [PATCH 04/10] externalize JSONENcoder() --- Examples/APIGateway/Sources/main.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Examples/APIGateway/Sources/main.swift b/Examples/APIGateway/Sources/main.swift index 425da83e..7c378366 100644 --- a/Examples/APIGateway/Sources/main.swift +++ b/Examples/APIGateway/Sources/main.swift @@ -16,6 +16,7 @@ import AWSLambdaEvents import AWSLambdaRuntime import class Foundation.JSONEncoder +let encoder = JSONEncoder() let runtime = LambdaRuntime { (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in @@ -25,8 +26,8 @@ let runtime = LambdaRuntime { header["content-type"] = "application/json" // echo the request in the response - let data = try JSONEncoder().encode(event) - let response = String(data: data, encoding: .utf8) + let data = try encoder.encode(event) + let response = String(decoding: data, as: Unicode.UTF8.self) return APIGatewayV2Response(statusCode: .ok, headers: header, body: response) } From a93eff23a2ee59326b88df59945b03004f7e85f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 17:30:47 +0200 Subject: [PATCH 05/10] use FoundationEssentials --- Examples/APIGateway/Sources/main.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Examples/APIGateway/Sources/main.swift b/Examples/APIGateway/Sources/main.swift index 7c378366..74084006 100644 --- a/Examples/APIGateway/Sources/main.swift +++ b/Examples/APIGateway/Sources/main.swift @@ -14,8 +14,11 @@ import AWSLambdaEvents import AWSLambdaRuntime -import class Foundation.JSONEncoder - +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif let encoder = JSONEncoder() let runtime = LambdaRuntime { (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in From 29fc6a4fc4edc2961ee9cea9540de3cd22bde55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 17:31:20 +0200 Subject: [PATCH 06/10] add one line space --- Examples/APIGateway/Sources/main.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Examples/APIGateway/Sources/main.swift b/Examples/APIGateway/Sources/main.swift index 74084006..0988a7d2 100644 --- a/Examples/APIGateway/Sources/main.swift +++ b/Examples/APIGateway/Sources/main.swift @@ -19,6 +19,7 @@ import FoundationEssentials #else import Foundation #endif + let encoder = JSONEncoder() let runtime = LambdaRuntime { (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in From d70f081391e3d9d91be8fb3c79ad97c4fcce2df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 21:37:31 +0200 Subject: [PATCH 07/10] add readme + swift-format --- Examples/HelloWorld/Package.swift | 8 +-- Examples/HelloWorld/README.md | 67 +++++++++++++++++++++++++- Examples/HelloWorld/Sources/main.swift | 5 +- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Examples/HelloWorld/Package.swift b/Examples/HelloWorld/Package.swift index b1eeaeae..6468fb88 100644 --- a/Examples/HelloWorld/Package.swift +++ b/Examples/HelloWorld/Package.swift @@ -2,16 +2,16 @@ import PackageDescription +// needed for CI to test the local version of the library +import class Foundation.ProcessInfo +import struct Foundation.URL + #if os(macOS) let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] #else let platforms: [PackageDescription.SupportedPlatform]? = nil #endif -// needed for CI to test the local version of the library -import class Foundation.ProcessInfo -import struct Foundation.URL - let package = Package( name: "swift-aws-lambda-runtime-example", platforms: platforms, diff --git a/Examples/HelloWorld/README.md b/Examples/HelloWorld/README.md index f4fae2b3..da3d8e71 100644 --- a/Examples/HelloWorld/README.md +++ b/Examples/HelloWorld/README.md @@ -1 +1,66 @@ -TO BE DONE \ No newline at end of file +# Hello World + +This is a simple example of an AWS Lambda function that takes a String as input parameter and returns a String as response. + +## Code + +The code creates a `LambdaRuntime` struct. In it's simplest form, it takes a function as argument. The function is the Lambda handler that will be invoked when an event triggers the Lambda function. + +The handler is `(event: String, context: LambdaContext) -> String`. The function takes two arguments: +- the event argument is a `String`. It is the parameter passed when invoking the function. +- the context argument is a `Lambda Context`. It is a description of the runtime context. + +The function must return a String. + +## Build & Package + +To build & archive the package, type the following commands. + +```bash +swift build +swift package archive --disable-sandbox +``` + +If there is no error, there is a ZIP file ready to deploy. +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` + +## Deploy + +Here is how to deploy using the `aws` command line. + +```bash +aws lambda create-function \ +--function-name MyLambda \ +--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip \ +--runtime provided.al2 \ +--handler provided \ +--architectures arm64 \ +--role arn:aws:iam:::role/lambda_basic_execution +``` + +The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`. + +Be sure to replace with your actual AWS account ID (for example: 012345678901). + +## Invoke your Lambda function + +To invoke the Lambda function, use this `aws` command line. + +```bash +aws lambda invoke \ +--function-name MyLambda \ +--payload $(echo \"Seb\" | base64) \ +out.txt && cat out.txt && rm out.txt +``` + +Note that the payload is expected to be a valid JSON strings, hence the surroundings quotes (`"`). + +This should print + +``` +{ + "StatusCode": 200, + "ExecutedVersion": "$LATEST" +} +"Hello Seb" +``` diff --git a/Examples/HelloWorld/Sources/main.swift b/Examples/HelloWorld/Sources/main.swift index a2c92bc2..5aab1a79 100644 --- a/Examples/HelloWorld/Sources/main.swift +++ b/Examples/HelloWorld/Sources/main.swift @@ -16,8 +16,9 @@ import AWSLambdaRuntime // in this example we are receiving and responding with strings -let runtime = LambdaRuntime { (event: String, context: LambdaContext) in - return "Hello \(event)" +let runtime = LambdaRuntime { + (event: String, context: LambdaContext) in + "Hello \(event)" } try await runtime.run() From 7d11bf573eb88dd24a656dcc950393eac67a8379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Mon, 30 Sep 2024 22:13:44 +0200 Subject: [PATCH 08/10] more details in example's readme --- Examples/APIGateway/README.md | 125 +++++++++++++++++++++++++++++++++- Examples/HelloWorld/README.md | 20 ++++-- 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/Examples/APIGateway/README.md b/Examples/APIGateway/README.md index f4fae2b3..ca731ec6 100644 --- a/Examples/APIGateway/README.md +++ b/Examples/APIGateway/README.md @@ -1 +1,124 @@ -TO BE DONE \ No newline at end of file +# API Gateway + +This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway. + +## Code + +The Lambda function takes all HTTP headers it receives as input and returns them as output. + +The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when the API Gateway receives an HTTP request. + +The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments: +- the event argument is a `APIGatewayV2Request`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data. +- the context argument is a `Lambda Context`. It is a description of the runtime context. + +The function must return a `APIGatewayV2Response`. + +`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library. + +## Build & Package + +To build the package, type the following commands. + +```bash +swift build +swift package archive --disable-sandbox +``` + +If there is no error, there is a ZIP file ready to deploy. +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` + +## Deploy + +The deployment must include the Lambda function and the API Gateway. We use the [Serverless Application Model (SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) to deploy the infrastructure. + +**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) + +The example directory contains a file named `template.yaml` that describes the deployment. + +To actually deploy your Lambda function and create the infrastructure, type the following `sam` command. + +```bash +sam deploy \ +----resolve-s3 \ +--template-file template.yaml \ +--stack-name MyLambda \ +--capabilities CAPABILITY_IAM +``` + +At the end of the deployment, the script lists the API Gateway endpoint. +The output is similar to this one. + +``` +----------------------------------------------------------------------------------------------------------------------------- +Outputs +----------------------------------------------------------------------------------------------------------------------------- +Key APIGAtewayEndpoint +Description API Gateway endpoint UR" +Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com +----------------------------------------------------------------------------------------------------------------------------- +``` + +## Invoke your Lambda function + +To invoke the Lambda function, use this `curl` command line. + +```bash +curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com +``` + +Be sure to replace the URL with the API Gateway endpoint returned in the previous step. + +This should print a JSON similar to + +```bash +{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"} +``` + +If you have `jq` installed, you can use it to pretty print the output. + +```bash +curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq +{ + "version": "2.0", + "rawPath": "/", + "requestContext": { + "domainPrefix": "a5q74es3k2", + "stage": "$default", + "timeEpoch": 1727726558220, + "http": { + "protocol": "HTTP/1.1", + "method": "GET", + "userAgent": "curl/8.7.1", + "path": "/", + "sourceIp": "81.0.0.43" + }, + "apiId": "a5q74es3k2", + "accountId": "012345678901", + "requestId": "e72KxgsRoAMEMSA=", + "domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", + "time": "30/Sep/2024:20:02:38 +0000" + }, + "rawQueryString": "", + "routeKey": "$default", + "headers": { + "x-forwarded-for": "81.0.0.43", + "user-agent": "curl/8.7.1", + "host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com", + "accept": "*/*", + "x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb", + "content-length": "0", + "x-forwarded-proto": "https", + "x-forwarded-port": "443" + }, + "isBase64Encoded": false +} +``` + +## Undeploy + +When done testing, you can delete the infrastructure with this command. + +```bash +sam delete +``` \ No newline at end of file diff --git a/Examples/HelloWorld/README.md b/Examples/HelloWorld/README.md index da3d8e71..e3e2e08b 100644 --- a/Examples/HelloWorld/README.md +++ b/Examples/HelloWorld/README.md @@ -1,16 +1,16 @@ # Hello World -This is a simple example of an AWS Lambda function that takes a String as input parameter and returns a String as response. +This is a simple example of an AWS Lambda function that takes a `String` as input parameter and returns a `String` as response. ## Code -The code creates a `LambdaRuntime` struct. In it's simplest form, it takes a function as argument. The function is the Lambda handler that will be invoked when an event triggers the Lambda function. +The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when an event triggers the Lambda function. -The handler is `(event: String, context: LambdaContext) -> String`. The function takes two arguments: +The handler is `(event: String, context: LambdaContext)`. The function takes two arguments: - the event argument is a `String`. It is the parameter passed when invoking the function. - the context argument is a `Lambda Context`. It is a description of the runtime context. -The function must return a String. +The function return value will be encoded as your Lambda function response. ## Build & Package @@ -53,9 +53,9 @@ aws lambda invoke \ out.txt && cat out.txt && rm out.txt ``` -Note that the payload is expected to be a valid JSON strings, hence the surroundings quotes (`"`). +Note that the payload is expected to be a valid JSON string, hence the surroundings quotes (`"`). -This should print +This should output the following result. ``` { @@ -64,3 +64,11 @@ This should print } "Hello Seb" ``` + +## Undeploy + +When done testing, you can delete the Lambda function with this command. + +```bash +aws lambda delete-function --function-name MyLambda +``` \ No newline at end of file From 4622359b6367ba73241fa3c49daa43ad420d8644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Tue, 8 Oct 2024 10:47:54 +0200 Subject: [PATCH 09/10] remove v1 examples --- Examples/v1/Benchmark/BenchmarkHandler.swift | 32 -- Examples/v1/Benchmark/Package.swift | 53 --- Examples/v1/Deployment/.dockerignore | 1 - Examples/v1/Deployment/Dockerfile | 3 - Examples/v1/Deployment/Package.swift | 43 -- Examples/v1/Deployment/README.md | 184 --------- .../Sources/Benchmark/BenchmarkHandler.swift | 32 -- .../HelloWorld/HelloWorldHandler.swift | 23 -- .../scripts/SAM/Benchmark-template.yml | 14 - .../scripts/SAM/HelloWorld-template.yml | 14 - .../Deployment/scripts/build-and-package.sh | 39 -- Examples/v1/Deployment/scripts/config.sh | 38 -- Examples/v1/Deployment/scripts/deploy.sh | 43 -- Examples/v1/Deployment/scripts/package.sh | 28 -- Examples/v1/Deployment/scripts/sam-deploy.sh | 27 -- .../Deployment/scripts/serverless-deploy.sh | 29 -- .../Deployment/scripts/serverless-remove.sh | 27 -- .../scripts/serverless/Benchmark-template.yml | 20 - .../serverless/HelloWorld-template.yml | 20 - Examples/v1/Echo/Lambda.swift | 25 -- Examples/v1/Echo/Package.swift | 34 -- Examples/v1/ErrorHandling/Lambda.swift | 104 ----- Examples/v1/ErrorHandling/Package.swift | 34 -- Examples/v1/Foundation/Lambda.swift | 286 -------------- Examples/v1/Foundation/Package.swift | 34 -- Examples/v1/JSON/Lambda.swift | 34 -- Examples/v1/JSON/Package.swift | 34 -- .../contents.xcworkspacedata | 16 - .../xcshareddata/IDEWorkspaceChecks.plist | 8 - .../xcshareddata/WorkspaceSettings.xcsettings | 8 - .../MyApp/MyApp.xcodeproj/project.pbxproj | 366 ------------------ .../AppIcon.appiconset/Contents.json | 98 ----- .../MyApp/MyApp/Assets.xcassets/Contents.json | 6 - .../MyApp/MyApp/ContentView.swift | 95 ----- .../v1/LocalDebugging/MyApp/MyApp/MyApp.swift | 24 -- .../Preview Assets.xcassets/Contents.json | 6 - .../v1/LocalDebugging/MyLambda/.dockerignore | 1 - .../v1/LocalDebugging/MyLambda/Dockerfile | 3 - .../v1/LocalDebugging/MyLambda/Lambda.swift | 27 -- .../v1/LocalDebugging/MyLambda/Package.swift | 38 -- .../LocalDebugging/MyLambda/scripts/deploy.sh | 53 --- .../MyLambda/scripts/package.sh | 28 -- Examples/v1/LocalDebugging/README.md | 35 -- .../v1/LocalDebugging/Shared/Package.swift | 15 - .../Shared/Sources/Shared/Shared.swift | 35 -- Examples/v1/ResourcePackaging/Lambda.swift | 28 -- Examples/v1/ResourcePackaging/Package.swift | 37 -- Examples/v1/ResourcePackaging/hello.txt | 1 - Examples/v1/SAM/notes.md | 148 ------- Examples/v1/SAM/samconfig.toml | 8 - Examples/v1/SAM/template.yaml | 68 ---- Examples/v1/Testing/Package.swift | 36 -- Examples/v1/Testing/Sources/Lambda.swift | 25 -- Examples/v1/Testing/Tests/LambdaTests.swift | 27 -- 54 files changed, 2495 deletions(-) delete mode 100644 Examples/v1/Benchmark/BenchmarkHandler.swift delete mode 100644 Examples/v1/Benchmark/Package.swift delete mode 100644 Examples/v1/Deployment/.dockerignore delete mode 100644 Examples/v1/Deployment/Dockerfile delete mode 100644 Examples/v1/Deployment/Package.swift delete mode 100644 Examples/v1/Deployment/README.md delete mode 100644 Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift delete mode 100644 Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift delete mode 100644 Examples/v1/Deployment/scripts/SAM/Benchmark-template.yml delete mode 100644 Examples/v1/Deployment/scripts/SAM/HelloWorld-template.yml delete mode 100755 Examples/v1/Deployment/scripts/build-and-package.sh delete mode 100755 Examples/v1/Deployment/scripts/config.sh delete mode 100755 Examples/v1/Deployment/scripts/deploy.sh delete mode 100755 Examples/v1/Deployment/scripts/package.sh delete mode 100755 Examples/v1/Deployment/scripts/sam-deploy.sh delete mode 100755 Examples/v1/Deployment/scripts/serverless-deploy.sh delete mode 100755 Examples/v1/Deployment/scripts/serverless-remove.sh delete mode 100644 Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml delete mode 100644 Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml delete mode 100644 Examples/v1/Echo/Lambda.swift delete mode 100644 Examples/v1/Echo/Package.swift delete mode 100644 Examples/v1/ErrorHandling/Lambda.swift delete mode 100644 Examples/v1/ErrorHandling/Package.swift delete mode 100644 Examples/v1/Foundation/Lambda.swift delete mode 100644 Examples/v1/Foundation/Package.swift delete mode 100644 Examples/v1/JSON/Lambda.swift delete mode 100644 Examples/v1/JSON/Package.swift delete mode 100644 Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata delete mode 100644 Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist delete mode 100644 Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift delete mode 100644 Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json delete mode 100644 Examples/v1/LocalDebugging/MyLambda/.dockerignore delete mode 100644 Examples/v1/LocalDebugging/MyLambda/Dockerfile delete mode 100644 Examples/v1/LocalDebugging/MyLambda/Lambda.swift delete mode 100644 Examples/v1/LocalDebugging/MyLambda/Package.swift delete mode 100755 Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh delete mode 100755 Examples/v1/LocalDebugging/MyLambda/scripts/package.sh delete mode 100644 Examples/v1/LocalDebugging/README.md delete mode 100644 Examples/v1/LocalDebugging/Shared/Package.swift delete mode 100644 Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift delete mode 100644 Examples/v1/ResourcePackaging/Lambda.swift delete mode 100644 Examples/v1/ResourcePackaging/Package.swift delete mode 100644 Examples/v1/ResourcePackaging/hello.txt delete mode 100644 Examples/v1/SAM/notes.md delete mode 100644 Examples/v1/SAM/samconfig.toml delete mode 100644 Examples/v1/SAM/template.yaml delete mode 100644 Examples/v1/Testing/Package.swift delete mode 100644 Examples/v1/Testing/Sources/Lambda.swift delete mode 100644 Examples/v1/Testing/Tests/LambdaTests.swift diff --git a/Examples/v1/Benchmark/BenchmarkHandler.swift b/Examples/v1/Benchmark/BenchmarkHandler.swift deleted file mode 100644 index d9cfce6e..00000000 --- a/Examples/v1/Benchmark/BenchmarkHandler.swift +++ /dev/null @@ -1,32 +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 -// -//===----------------------------------------------------------------------===// - -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. - -@main -struct BenchmarkHandler: EventLoopLambdaHandler { - static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture { - context.eventLoop.makeSucceededFuture(BenchmarkHandler()) - } - - func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { - context.eventLoop.makeSucceededFuture("hello, world!") - } -} diff --git a/Examples/v1/Benchmark/Package.swift b/Examples/v1/Benchmark/Package.swift deleted file mode 100644 index 53bb1f8a..00000000 --- a/Examples/v1/Benchmark/Package.swift +++ /dev/null @@ -1,53 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -// needed for CI to test the local version of the library -import class Foundation.ProcessInfo -import struct Foundation.URL - -let runtimeVersion = Version("1.0.0-alpha.3") - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: runtimeVersion) - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -// if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { -// print("LAMBDA_USE_LOCAL_DEPS is ignored for runtime v1 examples.") -// print("This project will compile against runtime version \(runtimeVersion)") -// } - -if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" { - - // check if directory exists - let u = URL(fileURLWithPath: localDepsPath) - if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! { - print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: localDepsPath) - ] - } else { - print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code") - print("This project will compile against runtime version \(runtimeVersion)") - } - -} \ No newline at end of file diff --git a/Examples/v1/Deployment/.dockerignore b/Examples/v1/Deployment/.dockerignore deleted file mode 100644 index 24e5b0a1..00000000 --- a/Examples/v1/Deployment/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -.build diff --git a/Examples/v1/Deployment/Dockerfile b/Examples/v1/Deployment/Dockerfile deleted file mode 100644 index 32962859..00000000 --- a/Examples/v1/Deployment/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM swift:5.5-amazonlinux2 - -RUN yum -y install zip diff --git a/Examples/v1/Deployment/Package.swift b/Examples/v1/Deployment/Package.swift deleted file mode 100644 index afcff025..00000000 --- a/Examples/v1/Deployment/Package.swift +++ /dev/null @@ -1,43 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-samples", - platforms: [ - .macOS(.v12) - ], - products: [ - // introductory example - .executable(name: "HelloWorld", targets: ["HelloWorld"]), - // good for benchmarking - .executable(name: "Benchmark", targets: ["Benchmark"]), - // demonstrate different types of error handling - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3") - ], - targets: [ - .executableTarget( - name: "Benchmark", - dependencies: [ - .product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime") - ] - ), - .executableTarget( - name: "HelloWorld", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ] - ), - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/Deployment/README.md b/Examples/v1/Deployment/README.md deleted file mode 100644 index aca3e391..00000000 --- a/Examples/v1/Deployment/README.md +++ /dev/null @@ -1,184 +0,0 @@ -# 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 `Deployment` folder. - -``` -git clone https://github.com/swift-server/swift-aws-lambda-runtime.git -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. - -## Mac M1 Considerations - -Lambdas will run on an x86 processor by default. Building a Lambda with an M1 will create an arm-based executable which will not run on an x86 processor. Here are a few options for building Swift Lambdas on an M1: - -1. Configure the Lambda to run on the [Graviton2](https://aws.amazon.com/blogs/aws/aws-lambda-functions-powered-by-aws-graviton2-processor-run-your-functions-on-arm-and-get-up-to-34-better-price-performance/) Arm-based processor. -2. Build with the x86 architecture by specifying `--platform linux/amd64` in all Docker 'build' and 'run' commands in `build-and-package.sh`. - -## Deployment instructions using AWS CLI - -Steps to deploy this sample to AWS Lambda using the AWS CLI: - -1. Login to AWS Console and create an AWS Lambda with the following settings: - * Runtime: Custom runtime - * Handler: Can be any string, does not matter in this case - -2. Build, package and deploy the Lambda - - ``` - ./scripts/deploy.sh - ``` - - 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) - - Both lambda function and S3 bucket must exist before deploying for the first time. - -### Deployment instructions using AWS SAM (Serverless Application Model) - -AWS [Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM) is an open-source framework for building serverless applications. This framework allows you to easily deploy other AWS resources and more complex deployment mechanisms such a CI pipelines. - -***Note:*** Deploying using SAM will automatically create resources within your AWS account. Charges may apply for these resources. - -To use SAM to deploy this sample to AWS: - -1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html). - -2. Install SAM CLI by following the [instructions](https://aws.amazon.com/serverless/sam/). - -3. Build, package and deploy the Lambda - - ``` - ./scripts/sam-deploy.sh --guided - ``` - -The script will ask you which sample Lambda you wish to deploy. It will then guide you through the SAM setup process. - - ``` - Setting default arguments for 'sam deploy' - ========================================= - Stack Name [sam-app]: swift-aws-lambda-runtime-sample - AWS Region [us-east-1]: - #Shows you resources changes to be deployed and require a 'Y' to initiate deploy - Confirm changes before deploy [y/N]: Y - #SAM needs permission to be able to create roles to connect to the resources in your template - Allow SAM CLI IAM role creation [Y/n]: Y - Save arguments to samconfig.toml [Y/n]: Y - ``` - -If you said yes to confirm changes, SAM will ask you to accept changes to the infrastructure you are setting up. For more on this, see [Cloud Formation](https://aws.amazon.com/cloudformation/). - -The `sam-deploy` script passes through any parameters to the SAM deploy command. - -4. Subsequent deploys can just use the command minus the `guided` parameter: - - ``` - ./scripts/sam-deploy.sh - ``` - -The script will ask you which sample Lambda you wish to deploy. If you are deploying a different sample lambda, the deploy process will pull down the previous Lambda. - -SAM will still ask you to confirm changes if you said yes to that initially. - -5. Testing - -For the API Gateway sample: - -The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` which you can use to test the Lambda. For example: - - ``` - curl <> - ``` - -***Warning:*** This SAM template is only intended as a sample and creates a publicly accessible HTTP endpoint. - -For all other samples use the AWS Lambda console. - -### Deployment instructions using Serverless Framework (serverless.com) - -[Serverless framework](https://www.serverless.com/open-source/) (Serverless) is a provider agnostic, open-source framework for building serverless applications. This framework allows you to easily deploy other AWS resources and more complex deployment mechanisms such a CI pipelines. Serverless Framework offers solutions for not only deploying but also testing, monitoring, alerting, and security and is widely adopted by the industry and offers along the open-source version a paid one. - -***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources. - -To use Serverless to deploy this sample to AWS: - -1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html). - -2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/). -If you already have installed be sure you have the latest version. -The examples have been tested with the version 1.72.0. - -``` -Serverless --version -Framework Core: 1.72.0 (standalone) -Plugin: 3.6.13 -SDK: 2.3.1 -Components: 2.30.12 -``` - -3. Build, package and deploy the Lambda - - ``` - ./scripts/serverless-deploy.sh - ``` - -The script will ask you which sample Lambda you wish to deploy. - -The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command. - -4. Testing - -For the APIGateway sample: - -The Serverless template will provide an endpoint which you can use to test the Lambda. - -Outuput example: - -``` -... -... -Serverless: Stack update finished... -Service Information -service: apigateway-swift-aws -stage: dev -region: us-east-1 -stack: apigateway-swift-aws-dev -resources: 12 -api keys: - None -endpoints: - GET - https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api -functions: - httpGet: apigateway-swift-aws-dev-httpGet -layers: - None - -Stack Outputs -HttpGetLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:XXXXXXXXX:function:apigateway-swift-aws-dev-httpGet:1 -ServerlessDeploymentBucketName: apigateway-swift-aws-dev-serverlessdeploymentbuck-ud51msgcrj1e -HttpApiUrl: https://r39lvhfng3.execute-api.us-east-1.amazonaws.com -``` - -For example: - - ``` - curl https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api - ``` - -***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint. - -For all other samples use the AWS Lambda console. - -4. Remove - - ``` - ./scripts/serverless-remove.sh - ``` - -The script will ask you which sample Lambda you wish to remove from the previous deployment. diff --git a/Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift b/Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift deleted file mode 100644 index d9cfce6e..00000000 --- a/Examples/v1/Deployment/Sources/Benchmark/BenchmarkHandler.swift +++ /dev/null @@ -1,32 +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 -// -//===----------------------------------------------------------------------===// - -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. - -@main -struct BenchmarkHandler: EventLoopLambdaHandler { - static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture { - context.eventLoop.makeSucceededFuture(BenchmarkHandler()) - } - - func handle(_ event: String, context: LambdaContext) -> EventLoopFuture { - context.eventLoop.makeSucceededFuture("hello, world!") - } -} diff --git a/Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift deleted file mode 100644 index 9f4a16f3..00000000 --- a/Examples/v1/Deployment/Sources/HelloWorld/HelloWorldHandler.swift +++ /dev/null @@ -1,23 +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 -// -//===----------------------------------------------------------------------===// - -import AWSLambdaRuntime - -// introductory example, the obligatory "hello, world!" -@main -struct HelloWorldHandler: SimpleLambdaHandler { - func handle(_ event: String, context: LambdaContext) async throws -> String { - "hello, world" - } -} diff --git a/Examples/v1/Deployment/scripts/SAM/Benchmark-template.yml b/Examples/v1/Deployment/scripts/SAM/Benchmark-template.yml deleted file mode 100644 index 55100d12..00000000 --- a/Examples/v1/Deployment/scripts/SAM/Benchmark-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: -# Benchmark Function - benchmarkFunction: - Type: AWS::Serverless::Function - Properties: - Handler: Provided - Runtime: provided - CodeUri: ../../.build/lambda/Benchmark/lambda.zip -# Instructs new versions to be published to an alias named "live". - AutoPublishAlias: live diff --git a/Examples/v1/Deployment/scripts/SAM/HelloWorld-template.yml b/Examples/v1/Deployment/scripts/SAM/HelloWorld-template.yml deleted file mode 100644 index 22b09df7..00000000 --- a/Examples/v1/Deployment/scripts/SAM/HelloWorld-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: -# HelloWorld Function - helloWorldFunction: - Type: AWS::Serverless::Function - Properties: - Handler: Provided - Runtime: provided - CodeUri: ../../.build/lambda/HelloWorld/lambda.zip -# Instructs new versions to be published to an alias named "live". - AutoPublishAlias: live diff --git a/Examples/v1/Deployment/scripts/build-and-package.sh b/Examples/v1/Deployment/scripts/build-and-package.sh deleted file mode 100755 index f1e0a922..00000000 --- a/Examples/v1/Deployment/scripts/build-and-package.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -executable=$1 -workspace="$(pwd)/../.." - -echo "-------------------------------------------------------------------------" -echo "preparing docker build image" -echo "-------------------------------------------------------------------------" -docker build . -t builder -echo "done" - -echo "-------------------------------------------------------------------------" -echo "building \"$executable\" lambda" -echo "-------------------------------------------------------------------------" -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/Deployment builder \ - bash -cl "./scripts/package.sh $executable" -echo "done" diff --git a/Examples/v1/Deployment/scripts/config.sh b/Examples/v1/Deployment/scripts/config.sh deleted file mode 100755 index d4ab9f6f..00000000 --- a/Examples/v1/Deployment/scripts/config.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2017-2018 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 -## -##===----------------------------------------------------------------------===## - -DIR="$(cd "$(dirname "$0")" && pwd)" -executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) - -if [[ ${#executables[@]} = 0 ]]; then - echo "no executables found" - exit 1 -elif [[ ${#executables[@]} = 1 ]]; then - executable=${executables[0]} -elif [[ ${#executables[@]} > 1 ]]; then - echo "multiple executables found:" - for executable in ${executables[@]}; do - echo " * $executable" - done - echo "" - read -p "select which executables to deploy: " executable -fi - -echo "-------------------------------------------------------------------------" -echo "configuration" -echo "-------------------------------------------------------------------------" -echo "current dir: $DIR" -echo "executable: $executable" -echo "-------------------------------------------------------------------------" diff --git a/Examples/v1/Deployment/scripts/deploy.sh b/Examples/v1/Deployment/scripts/deploy.sh deleted file mode 100755 index 3720b4d0..00000000 --- a/Examples/v1/Deployment/scripts/deploy.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -DIR="$(cd "$(dirname "$0")" && pwd)" -source $DIR/config.sh - -workspace="$DIR/../.." - -echo -e "\ndeploying $executable" - -$DIR/build-and-package.sh "$executable" - -echo "-------------------------------------------------------------------------" -echo "uploading \"$executable\" lambda to AWS S3" -echo "-------------------------------------------------------------------------" - -read -p "S3 bucket name to upload zip file (must exist in AWS S3): " s3_bucket -s3_bucket=${s3_bucket:-swift-lambda-test} # default for easy testing - -aws s3 cp ".build/lambda/$executable/lambda.zip" "s3://$s3_bucket/" - -echo "-------------------------------------------------------------------------" -echo "updating AWS Lambda to use \"$executable\"" -echo "-------------------------------------------------------------------------" - -read -p "Lambda Function name (must exist in AWS Lambda): " lambda_name -lambda_name=${lambda_name:-SwiftSample} # default for easy testing - -aws lambda update-function-code --function "$lambda_name" --s3-bucket "$s3_bucket" --s3-key lambda.zip diff --git a/Examples/v1/Deployment/scripts/package.sh b/Examples/v1/Deployment/scripts/package.sh deleted file mode 100755 index 17d5853b..00000000 --- a/Examples/v1/Deployment/scripts/package.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -executable=$1 - -target=".build/lambda/$executable" -rm -rf "$target" -mkdir -p "$target" -cp ".build/release/$executable" "$target/" -# add the target deps based on ldd -ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target" -cd "$target" -ln -s "$executable" "bootstrap" -zip --symlinks lambda.zip * diff --git a/Examples/v1/Deployment/scripts/sam-deploy.sh b/Examples/v1/Deployment/scripts/sam-deploy.sh deleted file mode 100755 index d87d966d..00000000 --- a/Examples/v1/Deployment/scripts/sam-deploy.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2017-2018 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 -## -##===----------------------------------------------------------------------===## - -DIR="$(cd "$(dirname "$0")" && pwd)" -source $DIR/config.sh - -echo -e "\ndeploying $executable" - -$DIR/build-and-package.sh "$executable" - -echo "-------------------------------------------------------------------------" -echo "deploying using SAM" -echo "-------------------------------------------------------------------------" - -sam deploy --template "./scripts/SAM/$executable-template.yml" $@ diff --git a/Examples/v1/Deployment/scripts/serverless-deploy.sh b/Examples/v1/Deployment/scripts/serverless-deploy.sh deleted file mode 100755 index 241ee7bf..00000000 --- a/Examples/v1/Deployment/scripts/serverless-deploy.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2017-2018 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -DIR="$(cd "$(dirname "$0")" && pwd)" -source $DIR/config.sh - -echo -e "\ndeploying $executable" - -$DIR/build-and-package.sh "$executable" - -echo "-------------------------------------------------------------------------" -echo "deploying using Serverless" -echo "-------------------------------------------------------------------------" - -serverless deploy --config "./scripts/serverless/$executable-template.yml" --stage dev -v diff --git a/Examples/v1/Deployment/scripts/serverless-remove.sh b/Examples/v1/Deployment/scripts/serverless-remove.sh deleted file mode 100755 index 262c07cb..00000000 --- a/Examples/v1/Deployment/scripts/serverless-remove.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2017-2018 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -DIR="$(cd "$(dirname "$0")" && pwd)" -source $DIR/config.sh - -echo -e "\nremoving $executable" - -echo "-------------------------------------------------------------------------" -echo "removing using Serverless" -echo "-------------------------------------------------------------------------" - -serverless remove --config "./scripts/serverless/$executable-template.yml" --stage dev -v diff --git a/Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml b/Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml deleted file mode 100644 index 1b2b1940..00000000 --- a/Examples/v1/Deployment/scripts/serverless/Benchmark-template.yml +++ /dev/null @@ -1,20 +0,0 @@ -service: benchmark-swift-aws - -package: - artifact: .build/lambda/Benchmark/lambda.zip - -provider: - name: aws - runtime: provided - iamRoleStatements: - - Effect: Allow - Action: - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Resource: "*" - -functions: - benchmarkFunction: - handler: Benchmark - memorySize: 128 \ No newline at end of file diff --git a/Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml b/Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml deleted file mode 100644 index 8d12bb74..00000000 --- a/Examples/v1/Deployment/scripts/serverless/HelloWorld-template.yml +++ /dev/null @@ -1,20 +0,0 @@ -service: helloworld-swift-aws - -package: - artifact: .build/lambda/HelloWorld/lambda.zip - -provider: - name: aws - runtime: provided - iamRoleStatements: - - Effect: Allow - Action: - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Resource: "*" - -functions: - hello: - handler: HelloWorld - memorySize: 128 \ No newline at end of file diff --git a/Examples/v1/Echo/Lambda.swift b/Examples/v1/Echo/Lambda.swift deleted file mode 100644 index 00c0a5e5..00000000 --- a/Examples/v1/Echo/Lambda.swift +++ /dev/null @@ -1,25 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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: SimpleLambdaHandler { - func handle(_ input: String, context: LambdaContext) async throws -> String { - // as an example, respond with the input's reversed - String(input.reversed()) - } -} diff --git a/Examples/v1/Echo/Package.swift b/Examples/v1/Echo/Package.swift deleted file mode 100644 index 5ae207a8..00000000 --- a/Examples/v1/Echo/Package.swift +++ /dev/null @@ -1,34 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/ErrorHandling/Lambda.swift b/Examples/v1/ErrorHandling/Lambda.swift deleted file mode 100644 index d8e560aa..00000000 --- a/Examples/v1/ErrorHandling/Lambda.swift +++ /dev/null @@ -1,104 +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 -// -//===----------------------------------------------------------------------===// - -import AWSLambdaRuntime - -// MARK: - Run Lambda - -@main -struct MyLambda: SimpleLambdaHandler { - func handle(_ request: Request, context: LambdaContext) async throws -> Response { - // switch over the error type "requested" by the request, and trigger such error accordingly - switch request.error { - // no error here! - case .none: - return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .ok) - // trigger a "managed" error - domain specific business logic failure - case .managed: - return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .error) - // trigger an "unmanaged" error - an unexpected Swift Error triggered while processing the request - case .unmanaged(let error): - throw UnmanagedError(description: error) - // trigger a "fatal" error - a panic type error which will crash the process - case .fatal: - fatalError("crash!") - } - } -} - -// MARK: - Request and Response - -struct Request: Codable { - let requestID: String - let error: Error - - public init(requestID: String, error: Error? = nil) { - self.requestID = requestID - self.error = error ?? .none - } - - public enum Error: Codable, RawRepresentable { - case none - case managed - case unmanaged(String) - case fatal - - public init?(rawValue: String) { - switch rawValue { - case "none": - self = .none - case "managed": - self = .managed - case "fatal": - self = .fatal - default: - self = .unmanaged(rawValue) - } - } - - public var rawValue: String { - switch self { - case .none: - return "none" - case .managed: - return "managed" - case .fatal: - return "fatal" - case .unmanaged(let error): - return error - } - } - } -} - -struct Response: Codable { - let awsRequestID: String - let requestID: String - let status: Status - - public init(awsRequestID: String, requestID: String, status: Status) { - self.awsRequestID = awsRequestID - self.requestID = requestID - self.status = status - } - - public enum Status: Int, Codable { - case ok - case error - } -} - -struct UnmanagedError: Error { - let description: String -} diff --git a/Examples/v1/ErrorHandling/Package.swift b/Examples/v1/ErrorHandling/Package.swift deleted file mode 100644 index 5ae207a8..00000000 --- a/Examples/v1/ErrorHandling/Package.swift +++ /dev/null @@ -1,34 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/Foundation/Lambda.swift b/Examples/v1/Foundation/Lambda.swift deleted file mode 100644 index f58863b9..00000000 --- a/Examples/v1/Foundation/Lambda.swift +++ /dev/null @@ -1,286 +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 -// -//===----------------------------------------------------------------------===// - -import AWSLambdaRuntime -import Dispatch -import Foundation -import Logging - -#if canImport(FoundationNetworking) && canImport(FoundationXML) -import FoundationNetworking -import FoundationXML -#endif - -// MARK: - Run Lambda - -@main -struct MyLambda: LambdaHandler { - let calculator: ExchangeRatesCalculator - - init(context: LambdaInitializationContext) async throws { - // the ExchangeRatesCalculator() can be reused over and over - self.calculator = ExchangeRatesCalculator() - } - - func handle(_ event: Request, context: LambdaContext) async throws -> [Exchange] { - try await withCheckedThrowingContinuation { continuation in - self.calculator.run(logger: context.logger) { result in - switch result { - case .success(let exchanges): - continuation.resume(returning: exchanges) - case .failure(let error): - continuation.resume(throwing: error) - } - } - } - } -} - -// MARK: - Business Logic - -// This is a contrived example performing currency exchange rate lookup and conversion using URLSession and XML parsing -struct ExchangeRatesCalculator { - static let currencies = ["EUR", "USD", "JPY"] - static let currenciesEmojies = [ - "EUR": "💶", - "JPY": "💴", - "USD": "💵", - ] - - let locale: Locale - let calendar: Calendar - - init() { - // This is data from HMRC, the UK tax authority. Therefore we want to use their locale when interpreting data from the server. - self.locale = Locale(identifier: "en_GB") - // Use the UK calendar, not the system one. - var calendar = self.locale.calendar - calendar.timeZone = TimeZone(identifier: "UTC")! - self.calendar = calendar - } - - func run(logger: Logger, callback: @escaping (Result<[Exchange], Swift.Error>) -> Void) { - let startDate = Date() - let months = (1...12).map { - self.calendar.date(byAdding: DateComponents(month: -$0), to: startDate)! - } - - self.download( - logger: logger, - months: months, - monthIndex: months.startIndex, - currencies: Self.currencies, - state: [:] - ) { result in - - switch result { - case .failure(let error): - return callback(.failure(error)) - case .success(let downloadedDataByMonth): - logger.debug("Downloads complete") - - var result = [Exchange]() - var previousData: [String: Decimal?] = [:] - for (_, exchangeRateData) in downloadedDataByMonth.filter({ $1.period != nil }).sorted(by: { - $0.key < $1.key - }) { - for (currencyCode, rate) in exchangeRateData.ratesByCurrencyCode.sorted(by: { $0.key < $1.key }) { - if let rate = rate, let currencyEmoji = Self.currenciesEmojies[currencyCode] { - let change: Exchange.Change - switch previousData[currencyCode] { - case .some(.some(let previousRate)) where rate > previousRate: - change = .up - case .some(.some(let previousRate)) where rate < previousRate: - change = .down - case .some(.some(let previousRate)) where rate == previousRate: - change = .none - default: - change = .unknown - } - result.append( - Exchange( - date: exchangeRateData.period!.start, - from: .init(symbol: "GBP", emoji: "💷"), - to: .init(symbol: currencyCode, emoji: currencyEmoji), - rate: rate, - change: change - ) - ) - } - } - previousData = exchangeRateData.ratesByCurrencyCode - } - callback(.success(result)) - } - } - } - - private func download( - logger: Logger, - months: [Date], - monthIndex: Array.Index, - currencies: [String], - state: [Date: ExchangeRates], - callback: @escaping ((Result<[Date: ExchangeRates], Swift.Error>) -> Void) - ) { - if monthIndex == months.count { - return callback(.success(state)) - } - - var newState = state - - let month = months[monthIndex] - let url = self.exchangeRatesURL(forMonthContaining: month) - logger.debug("requesting exchange rate from \(url)") - let dataTask = URLSession.shared.dataTask(with: url) { data, _, error in - do { - guard let data = data else { - throw error! - } - let exchangeRates = try self.parse(data: data, currencyCodes: Set(currencies)) - newState[month] = exchangeRates - logger.debug("Finished downloading month: \(month)") - if let period = exchangeRates.period { - logger.debug("Got data covering period: \(period)") - } - } catch { - return callback(.failure(error)) - } - self.download( - logger: logger, - months: months, - monthIndex: monthIndex.advanced(by: 1), - currencies: currencies, - state: newState, - callback: callback - ) - } - dataTask.resume() - } - - private func parse(data: Data, currencyCodes: Set) throws -> ExchangeRates { - let document = try XMLDocument(data: data) - let dateFormatter = DateFormatter() - dateFormatter.timeZone = TimeZone(identifier: "Etc/UTC")! - dateFormatter.dateFormat = "dd/MMM/yy" - let interval: DateInterval? - if let period = try document.nodes(forXPath: "/exchangeRateMonthList/@Period").first?.stringValue, - period.count == 26 - { - // "01/Sep/2018 to 30/Sep/2018" - let startString = period[period.startIndex.. DateFormatter { - let utcTimeZone = TimeZone(identifier: "UTC")! - let result = DateFormatter() - result.locale = Locale(identifier: "en_US_POSIX") - result.timeZone = utcTimeZone - result.dateFormat = dateFormat - return result - } - - private func exchangeRatesURL(forMonthContaining date: Date) -> URL { - let exchangeRatesBaseURL = URL(string: "https://www.hmrc.gov.uk/softwaredevelopers/rates")! - let dateFormatter = self.makeUTCDateFormatter(dateFormat: "MMyy") - return exchangeRatesBaseURL.appendingPathComponent("exrates-monthly-\(dateFormatter.string(from: date)).xml") - } - - private typealias ExchangeRates = (period: DateInterval?, ratesByCurrencyCode: [String: Decimal?]) - - private struct Error: Swift.Error, CustomStringConvertible { - let description: String - } -} - -// MARK: - Request and Response - -struct Request: Decodable {} - -struct Exchange: Encodable { - @DateCoding - var date: Date - let from: Currency - let to: Currency - let rate: Decimal - let change: Change - - struct Currency: Encodable { - let symbol: String - let emoji: String - } - - enum Change: String, Encodable { - case up - case down - case none - case unknown - } - - @propertyWrapper - public struct DateCoding: Encodable { - public let wrappedValue: Date - - public init(wrappedValue: Date) { - self.wrappedValue = wrappedValue - } - - func encode(to encoder: Encoder) throws { - let string = Self.dateFormatter.string(from: self.wrappedValue) - var container = encoder.singleValueContainer() - try container.encode(string) - } - - private static var dateFormatter: ISO8601DateFormatter { - let dateFormatter = ISO8601DateFormatter() - dateFormatter.timeZone = TimeZone(identifier: "UTC")! - dateFormatter.formatOptions = [.withYear, .withMonth, .withDashSeparatorInDate] - return dateFormatter - } - } -} diff --git a/Examples/v1/Foundation/Package.swift b/Examples/v1/Foundation/Package.swift deleted file mode 100644 index 5ae207a8..00000000 --- a/Examples/v1/Foundation/Package.swift +++ /dev/null @@ -1,34 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/JSON/Lambda.swift b/Examples/v1/JSON/Lambda.swift deleted file mode 100644 index cad3b650..00000000 --- a/Examples/v1/JSON/Lambda.swift +++ /dev/null @@ -1,34 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 - -struct Request: Codable { - let body: String -} - -struct Response: Codable { - let body: String -} - -// in this example we are receiving and responding with codables. Request and Response above are examples of how to use -// codables to model your request and response objects - -@main -struct MyLambda: SimpleLambdaHandler { - func handle(_ event: Request, context: LambdaContext) async throws -> Response { - // as an example, respond with the input event's reversed body - Response(body: String(event.body.reversed())) - } -} diff --git a/Examples/v1/JSON/Package.swift b/Examples/v1/JSON/Package.swift deleted file mode 100644 index 5ae207a8..00000000 --- a/Examples/v1/JSON/Package.swift +++ /dev/null @@ -1,34 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ], - path: "." - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata b/Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index e42d285c..00000000 --- a/Examples/v1/LocalDebugging/Example.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - diff --git a/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist deleted file mode 100644 index 18d98100..00000000 --- a/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +++ /dev/null @@ -1,8 +0,0 @@ - - - - - IDEDidComputeMac32BitWarning - - - diff --git a/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings deleted file mode 100644 index f9b0d7c5..00000000 --- a/Examples/v1/LocalDebugging/Example.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +++ /dev/null @@ -1,8 +0,0 @@ - - - - - PreviewsEnabled - - - diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj b/Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj deleted file mode 100644 index 0ac2539a..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj +++ /dev/null @@ -1,366 +0,0 @@ -// !$*UTF8*$! -{ - archiveVersion = 1; - classes = { - }; - objectVersion = 55; - objects = { - -/* Begin PBXBuildFile section */ - 7CD1174B26FE468F007DD17A /* MyApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD1174A26FE468F007DD17A /* MyApp.swift */; }; - 7CD1174D26FE468F007DD17A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD1174C26FE468F007DD17A /* ContentView.swift */; }; - 7CD1174F26FE4692007DD17A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7CD1174E26FE4692007DD17A /* Assets.xcassets */; }; - 7CD1175226FE4692007DD17A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7CD1175126FE4692007DD17A /* Preview Assets.xcassets */; }; - 7CD1175A26FE4F44007DD17A /* Shared in Frameworks */ = {isa = PBXBuildFile; productRef = 7CD1175926FE4F44007DD17A /* Shared */; }; -/* End PBXBuildFile section */ - -/* Begin PBXFileReference section */ - 7CD1174726FE468F007DD17A /* MyApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MyApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; - 7CD1174A26FE468F007DD17A /* MyApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyApp.swift; sourceTree = ""; }; - 7CD1174C26FE468F007DD17A /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; - 7CD1174E26FE4692007DD17A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; - 7CD1175126FE4692007DD17A /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - 7CD1174426FE468F007DD17A /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 7CD1175A26FE4F44007DD17A /* Shared in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - 7CD1173E26FE468F007DD17A = { - isa = PBXGroup; - children = ( - 7CD1174926FE468F007DD17A /* MyApp */, - 7CD1174826FE468F007DD17A /* Products */, - 7CD1175826FE4F44007DD17A /* Frameworks */, - ); - sourceTree = ""; - }; - 7CD1174826FE468F007DD17A /* Products */ = { - isa = PBXGroup; - children = ( - 7CD1174726FE468F007DD17A /* MyApp.app */, - ); - name = Products; - sourceTree = ""; - }; - 7CD1174926FE468F007DD17A /* MyApp */ = { - isa = PBXGroup; - children = ( - 7CD1174A26FE468F007DD17A /* MyApp.swift */, - 7CD1174C26FE468F007DD17A /* ContentView.swift */, - 7CD1174E26FE4692007DD17A /* Assets.xcassets */, - 7CD1175026FE4692007DD17A /* Preview Content */, - ); - path = MyApp; - sourceTree = ""; - }; - 7CD1175026FE4692007DD17A /* Preview Content */ = { - isa = PBXGroup; - children = ( - 7CD1175126FE4692007DD17A /* Preview Assets.xcassets */, - ); - path = "Preview Content"; - sourceTree = ""; - }; - 7CD1175826FE4F44007DD17A /* Frameworks */ = { - isa = PBXGroup; - children = ( - ); - name = Frameworks; - sourceTree = ""; - }; -/* End PBXGroup section */ - -/* Begin PBXNativeTarget section */ - 7CD1174626FE468F007DD17A /* MyApp */ = { - isa = PBXNativeTarget; - buildConfigurationList = 7CD1175526FE4692007DD17A /* Build configuration list for PBXNativeTarget "MyApp" */; - buildPhases = ( - 7CD1174326FE468F007DD17A /* Sources */, - 7CD1174426FE468F007DD17A /* Frameworks */, - 7CD1174526FE468F007DD17A /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = MyApp; - packageProductDependencies = ( - 7CD1175926FE4F44007DD17A /* Shared */, - ); - productName = MyApp; - productReference = 7CD1174726FE468F007DD17A /* MyApp.app */; - productType = "com.apple.product-type.application"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - 7CD1173F26FE468F007DD17A /* Project object */ = { - isa = PBXProject; - attributes = { - BuildIndependentTargetsInParallel = 1; - LastSwiftUpdateCheck = 1300; - LastUpgradeCheck = 1300; - TargetAttributes = { - 7CD1174626FE468F007DD17A = { - CreatedOnToolsVersion = 13.0; - }; - }; - }; - buildConfigurationList = 7CD1174226FE468F007DD17A /* Build configuration list for PBXProject "MyApp" */; - compatibilityVersion = "Xcode 13.0"; - developmentRegion = en; - hasScannedForEncodings = 0; - knownRegions = ( - en, - Base, - ); - mainGroup = 7CD1173E26FE468F007DD17A; - productRefGroup = 7CD1174826FE468F007DD17A /* Products */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - 7CD1174626FE468F007DD17A /* MyApp */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXResourcesBuildPhase section */ - 7CD1174526FE468F007DD17A /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 7CD1175226FE4692007DD17A /* Preview Assets.xcassets in Resources */, - 7CD1174F26FE4692007DD17A /* Assets.xcassets in Resources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXResourcesBuildPhase section */ - -/* Begin PBXSourcesBuildPhase section */ - 7CD1174326FE468F007DD17A /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 7CD1174D26FE468F007DD17A /* ContentView.swift in Sources */, - 7CD1174B26FE468F007DD17A /* MyApp.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin XCBuildConfiguration section */ - 7CD1175326FE4692007DD17A /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_ENABLE_OBJC_WEAK = YES; - CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_COMMA = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; - CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; - CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; - CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; - CLANG_WARN_STRICT_PROTOTYPES = YES; - CLANG_WARN_SUSPICIOUS_MOVE = YES; - CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = dwarf; - ENABLE_STRICT_OBJC_MSGSEND = YES; - ENABLE_TESTABILITY = YES; - GCC_C_LANGUAGE_STANDARD = gnu11; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; - MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; - MTL_FAST_MATH = YES; - ONLY_ACTIVE_ARCH = YES; - SDKROOT = iphoneos; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - }; - name = Debug; - }; - 7CD1175426FE4692007DD17A /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_ENABLE_OBJC_WEAK = YES; - CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_COMMA = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; - CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; - CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; - CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; - CLANG_WARN_STRICT_PROTOTYPES = YES; - CLANG_WARN_SUSPICIOUS_MOVE = YES; - CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu11; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; - MTL_ENABLE_DEBUG_INFO = NO; - MTL_FAST_MATH = YES; - SDKROOT = iphoneos; - SWIFT_COMPILATION_MODE = wholemodule; - SWIFT_OPTIMIZATION_LEVEL = "-O"; - VALIDATE_PRODUCT = YES; - }; - name = Release; - }; - 7CD1175626FE4692007DD17A /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; - CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_ASSET_PATHS = "\"MyApp/Preview Content\""; - ENABLE_PREVIEWS = YES; - GENERATE_INFOPLIST_FILE = YES; - INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; - INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; - INFOPLIST_KEY_UILaunchScreen_Generation = YES; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - ); - MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = com.apple.swift.MyApp; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Debug; - }; - 7CD1175726FE4692007DD17A /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; - CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_ASSET_PATHS = "\"MyApp/Preview Content\""; - ENABLE_PREVIEWS = YES; - GENERATE_INFOPLIST_FILE = YES; - INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; - INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; - INFOPLIST_KEY_UILaunchScreen_Generation = YES; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/Frameworks", - ); - MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = com.apple.swift.MyApp; - PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - 7CD1174226FE468F007DD17A /* Build configuration list for PBXProject "MyApp" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 7CD1175326FE4692007DD17A /* Debug */, - 7CD1175426FE4692007DD17A /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 7CD1175526FE4692007DD17A /* Build configuration list for PBXNativeTarget "MyApp" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 7CD1175626FE4692007DD17A /* Debug */, - 7CD1175726FE4692007DD17A /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - -/* Begin XCSwiftPackageProductDependency section */ - 7CD1175926FE4F44007DD17A /* Shared */ = { - isa = XCSwiftPackageProductDependency; - productName = Shared; - }; -/* End XCSwiftPackageProductDependency section */ - }; - rootObject = 7CD1173F26FE468F007DD17A /* Project object */; -} diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json deleted file mode 100644 index 9221b9bb..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/AppIcon.appiconset/Contents.json +++ /dev/null @@ -1,98 +0,0 @@ -{ - "images" : [ - { - "idiom" : "iphone", - "scale" : "2x", - "size" : "20x20" - }, - { - "idiom" : "iphone", - "scale" : "3x", - "size" : "20x20" - }, - { - "idiom" : "iphone", - "scale" : "2x", - "size" : "29x29" - }, - { - "idiom" : "iphone", - "scale" : "3x", - "size" : "29x29" - }, - { - "idiom" : "iphone", - "scale" : "2x", - "size" : "40x40" - }, - { - "idiom" : "iphone", - "scale" : "3x", - "size" : "40x40" - }, - { - "idiom" : "iphone", - "scale" : "2x", - "size" : "60x60" - }, - { - "idiom" : "iphone", - "scale" : "3x", - "size" : "60x60" - }, - { - "idiom" : "ipad", - "scale" : "1x", - "size" : "20x20" - }, - { - "idiom" : "ipad", - "scale" : "2x", - "size" : "20x20" - }, - { - "idiom" : "ipad", - "scale" : "1x", - "size" : "29x29" - }, - { - "idiom" : "ipad", - "scale" : "2x", - "size" : "29x29" - }, - { - "idiom" : "ipad", - "scale" : "1x", - "size" : "40x40" - }, - { - "idiom" : "ipad", - "scale" : "2x", - "size" : "40x40" - }, - { - "idiom" : "ipad", - "scale" : "1x", - "size" : "76x76" - }, - { - "idiom" : "ipad", - "scale" : "2x", - "size" : "76x76" - }, - { - "idiom" : "ipad", - "scale" : "2x", - "size" : "83.5x83.5" - }, - { - "idiom" : "ios-marketing", - "scale" : "1x", - "size" : "1024x1024" - } - ], - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json deleted file mode 100644 index 73c00596..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp/Assets.xcassets/Contents.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift b/Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift deleted file mode 100644 index 46f9d5a7..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp/ContentView.swift +++ /dev/null @@ -1,95 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftAWSLambdaRuntime open source project -// -// Copyright (c) 2020-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 Shared -import SwiftUI - -struct ContentView: View { - @State var name: String = "" - @State var password: String = "" - @State var response: String = "" - @State private var isLoading: Bool = false - - var body: some View { - VStack(alignment: .leading, spacing: 20) { - TextField("Username", text: self.$name) - SecureField("Password", text: self.$password) - let inputIncomplete = self.name.isEmpty || self.password.isEmpty - Button { - Task { - self.isLoading = true - do { - self.response = try await self.register() - } catch { - self.response = error.localizedDescription - } - self.isLoading = false - } - } label: { - Text("Register") - .padding() - .foregroundColor(.white) - .background(.black) - .border(.black, width: 2) - .opacity(self.isLoading ? 0 : 1) - .overlay { - if self.isLoading { - ProgressView() - } - } - } - .disabled(inputIncomplete || self.isLoading) - .opacity(inputIncomplete ? 0.5 : 1) - Text(self.response) - }.padding(100) - } - - func register() async throws -> String { - guard let url = URL(string: "http://127.0.0.1:7000/invoke") else { - fatalError("invalid url") - } - var request = URLRequest(url: url) - request.httpMethod = "POST" - - guard let jsonRequest = try? JSONEncoder().encode(Request(name: self.name, password: self.password)) else { - fatalError("encoding error") - } - request.httpBody = jsonRequest - - let (data, response) = try await URLSession.shared.data(for: request) - - guard let httpResponse = response as? HTTPURLResponse else { - throw CommunicationError(reason: "Invalid response, expected HTTPURLResponse.") - } - guard httpResponse.statusCode == 200 else { - throw CommunicationError(reason: "Invalid response code: \(httpResponse.statusCode)") - } - - let jsonResponse = try JSONDecoder().decode(Response.self, from: data) - return jsonResponse.message - } -} - -struct ContentView_Previews: PreviewProvider { - static var previews: some View { - ContentView() - } -} - -struct CommunicationError: LocalizedError { - let reason: String - var errorDescription: String? { - self.reason - } -} diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift b/Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift deleted file mode 100644 index 8b4ae638..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp/MyApp.swift +++ /dev/null @@ -1,24 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 SwiftUI - -@main -struct MyApp: App { - var body: some Scene { - WindowGroup { - ContentView() - } - } -} diff --git a/Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json b/Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json deleted file mode 100644 index 73c00596..00000000 --- a/Examples/v1/LocalDebugging/MyApp/MyApp/Preview Content/Preview Assets.xcassets/Contents.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/Examples/v1/LocalDebugging/MyLambda/.dockerignore b/Examples/v1/LocalDebugging/MyLambda/.dockerignore deleted file mode 100644 index 24e5b0a1..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -.build diff --git a/Examples/v1/LocalDebugging/MyLambda/Dockerfile b/Examples/v1/LocalDebugging/MyLambda/Dockerfile deleted file mode 100644 index 32962859..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM swift:5.5-amazonlinux2 - -RUN yum -y install zip diff --git a/Examples/v1/LocalDebugging/MyLambda/Lambda.swift b/Examples/v1/LocalDebugging/MyLambda/Lambda.swift deleted file mode 100644 index 397ece30..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/Lambda.swift +++ /dev/null @@ -1,27 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftAWSLambdaRuntime open source project -// -// Copyright (c) 2020-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 -import Shared - -// set LOCAL_LAMBDA_SERVER_ENABLED env variable to "true" to start -// a local server simulator which will allow local debugging - -@main -struct MyLambda: SimpleLambdaHandler { - func handle(_ request: Request, context: LambdaContext) async throws -> Response { - // TODO: something useful - Response(message: "Hello, \(request.name)!") - } -} diff --git a/Examples/v1/LocalDebugging/MyLambda/Package.swift b/Examples/v1/LocalDebugging/MyLambda/Package.swift deleted file mode 100644 index facef277..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/Package.swift +++ /dev/null @@ -1,38 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "MyLambda", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"), - .package(name: "Shared", path: "../Shared"), - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), - .product(name: "Shared", package: "Shared"), - ], - path: ".", - exclude: ["scripts/", "Dockerfile"] - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../../.."), - .package(name: "Shared", path: "../Shared"), - ] -} diff --git a/Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh b/Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh deleted file mode 100755 index 75be0ceb..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/scripts/deploy.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -executable=MyLambda -lambda_name=SwiftSample -s3_bucket=swift-lambda-test - -echo -e "\ndeploying $executable" - -echo "-------------------------------------------------------------------------" -echo "preparing docker build image" -echo "-------------------------------------------------------------------------" -docker build . -t builder -echo "done" - -echo "-------------------------------------------------------------------------" -echo "building \"$executable\" lambda" -echo "-------------------------------------------------------------------------" -docker run --rm -v `pwd`/../../..:/workspace -w /workspace/Examples/LocalDebugging/MyLambda builder \ - bash -cl "swift build --product $executable -c release" -echo "done" - -echo "-------------------------------------------------------------------------" -echo "packaging \"$executable\" lambda" -echo "-------------------------------------------------------------------------" -docker run --rm -v `pwd`:/workspace -w /workspace builder \ - bash -cl "./scripts/package.sh $executable" -echo "done" - -echo "-------------------------------------------------------------------------" -echo "uploading \"$executable\" lambda to s3" -echo "-------------------------------------------------------------------------" - -aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/ - -echo "-------------------------------------------------------------------------" -echo "updating \"$lambda_name\" to latest \"$executable\"" -echo "-------------------------------------------------------------------------" -aws lambda update-function-code --function $lambda_name --s3-bucket $s3_bucket --s3-key lambda.zip diff --git a/Examples/v1/LocalDebugging/MyLambda/scripts/package.sh b/Examples/v1/LocalDebugging/MyLambda/scripts/package.sh deleted file mode 100755 index 17d5853b..00000000 --- a/Examples/v1/LocalDebugging/MyLambda/scripts/package.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## 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 -## -##===----------------------------------------------------------------------===## - -set -eu - -executable=$1 - -target=".build/lambda/$executable" -rm -rf "$target" -mkdir -p "$target" -cp ".build/release/$executable" "$target/" -# add the target deps based on ldd -ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target" -cd "$target" -ln -s "$executable" "bootstrap" -zip --symlinks lambda.zip * diff --git a/Examples/v1/LocalDebugging/README.md b/Examples/v1/LocalDebugging/README.md deleted file mode 100644 index 25ee92ba..00000000 --- a/Examples/v1/LocalDebugging/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# Local Debugging Example - -This sample project demonstrates how to write a simple Lambda function in Swift, -and how to use local debugging techniques that simulate how the Lambda function -would be invoked by the AWS Lambda Runtime engine. - -The example includes an Xcode workspace with three modules: - -1. [MyApp](MyApp) is a SwiftUI iOS application that calls the Lambda function. -2. [MyLambda](MyLambda) is a SwiftPM executable package for the Lambda function. -3. [Shared](Shared) is a SwiftPM library package used for shared code between the iOS application and the Lambda function, -such as the Request and Response model objects. - -The local debugging experience is achieved by running the Lambda function in the context of the -debug-only local lambda engine simulator which starts a local HTTP server enabling the communication -between the iOS application and the Lambda function over HTTP. - -To try out this example, open the workspace in Xcode and "run" the two targets, -using the relevant `MyLambda` and `MyApp` Xcode schemes. - -Start with running the `MyLambda` target. -* Switch to the `MyLambda` scheme and select the "My Mac" destination -* Set the `LOCAL_LAMBDA_SERVER_ENABLED` environment variable to `true` by editing the `MyLambda` scheme Run/Arguments options. -* Hit `Run` -* Once it is up you should see a log message in the Xcode console saying -`LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke` -which means the local emulator is up and receiving traffic on port `7000` and expecting events on the `/invoke` endpoint. - -Continue to run the `MyApp` target -* Switch to the `MyApp` scheme and select a simulator destination. -* Hit `Run` -* Once up, the application's UI should appear in the simulator allowing you -to interact with it. - -Once both targets are running, set up breakpoints in the iOS application or Lambda function to observe the system behavior. diff --git a/Examples/v1/LocalDebugging/Shared/Package.swift b/Examples/v1/LocalDebugging/Shared/Package.swift deleted file mode 100644 index 2ac36e03..00000000 --- a/Examples/v1/LocalDebugging/Shared/Package.swift +++ /dev/null @@ -1,15 +0,0 @@ -// swift-tools-version:5.5 -// The swift-tools-version declares the minimum version of Swift required to build this package. - -import PackageDescription - -let package = Package( - name: "Shared", - products: [ - .library(name: "Shared", targets: ["Shared"]) - ], - dependencies: [], - targets: [ - .target(name: "Shared", dependencies: []) - ] -) diff --git a/Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift b/Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift deleted file mode 100644 index 8189eac3..00000000 --- a/Examples/v1/LocalDebugging/Shared/Sources/Shared/Shared.swift +++ /dev/null @@ -1,35 +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 -// -//===----------------------------------------------------------------------===// - -public struct Request: Codable, CustomStringConvertible { - public let name: String - public let password: String - - public init(name: String, password: String) { - self.name = name - self.password = password - } - - public var description: String { - "name: \(self.name), password: ***" - } -} - -public struct Response: Codable { - public let message: String - - public init(message: String) { - self.message = message - } -} diff --git a/Examples/v1/ResourcePackaging/Lambda.swift b/Examples/v1/ResourcePackaging/Lambda.swift deleted file mode 100644 index 5e71a688..00000000 --- a/Examples/v1/ResourcePackaging/Lambda.swift +++ /dev/null @@ -1,28 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -import Foundation - -// in this example we are reading from a bundled resource and responding with the contents - -@main -struct MyLambda: SimpleLambdaHandler { - func handle(_ input: String, context: LambdaContext) async throws -> String { - guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else { - fatalError("no file url") - } - return try String(contentsOf: fileURL) - } -} diff --git a/Examples/v1/ResourcePackaging/Package.swift b/Examples/v1/ResourcePackaging/Package.swift deleted file mode 100644 index b2af8c02..00000000 --- a/Examples/v1/ResourcePackaging/Package.swift +++ /dev/null @@ -1,37 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - targets: [ - .executableTarget( - name: "MyLambda", - dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") - ], - path: ".", - resources: [ - .process("hello.txt") - ] - ) - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/ResourcePackaging/hello.txt b/Examples/v1/ResourcePackaging/hello.txt deleted file mode 100644 index b45ef6fe..00000000 --- a/Examples/v1/ResourcePackaging/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello, World! \ No newline at end of file diff --git a/Examples/v1/SAM/notes.md b/Examples/v1/SAM/notes.md deleted file mode 100644 index b99d614b..00000000 --- a/Examples/v1/SAM/notes.md +++ /dev/null @@ -1,148 +0,0 @@ -swift package --disable-sandbox archive --products HttpApiLambda --output-path dist - -# change line 115 in archive plugin -# // FIXME: temporary change to be able to build from the mac command line -# arguments: ["run", "--rm", "-v", "\(packageDirectory.string)/../..:/workspace", "-w", "/workspace/Examples/SAM", baseImage, "bash", "-cl", buildCommand], - -https://www.hackingwithswift.com/swift/5.4/result-builders -https://github.com/carson-katri/awesome-result-builders#server-side -https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md -https://developer.apple.com/videos/play/wwdc2021/10253/ - -From -https://github.com/apple/swift-package-manager/compare/main...tomerd:swift-package-manager:feature/manifest2-poc - -https://forums.swift.org/t/pre-pitch-swiftpm-manifest-based-on-result-builders/53457 - -``` -public class Package: Codable { - public var modules: [Module] - public var dependencies: [Dependency] - public var minimumDeploymentTargets: [DeploymentTarget] - - public init() { - self.modules = [] - self.dependencies = [] - self.minimumDeploymentTargets = [] - // register for serialization - __packages.append(self) - // FIXME: get rid of atexit in favor of more reliable solution - atexit { - if CommandLine.arguments.first?.contains("-manifest") ?? false { - try! PackageSerializer.serialize(__packages.last!) - } - } - } -``` - -``` -#if canImport(Glibc) -@_implementationOnly import Glibc -#elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS) -@_implementationOnly import Darwin.C -#elseif os(Windows) -@_implementationOnly import ucrt -@_implementationOnly import struct WinSDK.HANDLE -#endif -import Foundation - -// Adds an main handler to cause the package's JSON representation -// to be written to a file when executed as a program. -// Emitting it to a separate file descriptor from stdout -// keeps any of the manifest's stdout output from interfering with it. -// -// Warning: The `-fileno` flag is a contract between PackageDescription -// and libSwiftPM, and since different versions of the two can be used -// together, it isn't safe to rename or remove it. -// -// Note: `-fileno` is not viable on Windows. Instead, we pass the file -// handle through the `-handle` option. - -struct PackageSerializer { - static func serialize(_ package: Package) throws { - #if os(Windows) - guard let index = CommandLine.arguments.firstIndex(of: "-handle"), let handleId = Int(CommandLine.arguments[index + 1], radix: 16) else { - throw PackageSerializerErrors.invalidArguments - } - // write serialized package to the file - guard let handle = HANDLE(bitPattern: handleId) else { - throw PackageSerializerErrors.invalidFileHandle - } - // NOTE: `_open_osfhandle` transfers ownership of the HANDLE to the file - // descriptor. DO NOT invoke `CloseHandle` on `hFile`. - let fd: CInt = _open_osfhandle(Int(bitPattern: handle), _O_APPEND) - // NOTE: `_fdopen` transfers ownership of the file descriptor to the - // `FILE *`. DO NOT invoke `_close` on the `fd`. - guard let filePipe = _fdopen(fileDescriptor, "w") else { - _close(fileDescriptor) - throw PackageSerializerErrors.failedOpeningFile - } - defer { - fclose(filePipe) - } - - fputs(try package.toJSON(), filePipe) - #else - guard let index = CommandLine.arguments.firstIndex(of: "-fileno"), let fileDescriptorId = Int32(CommandLine.arguments[index + 1]) else { - throw PackageSerializerErrors.invalidArguments - } - // write serialized package to the file - guard let fileDescriptor = fdopen(fileDescriptorId, "w") else { - throw PackageSerializerErrors.failedOpeningFile - } - defer { - fclose(fileDescriptor) - } - - fputs(try package.toJSON(), fileDescriptor) - #endif - } -} - -extension Package { - fileprivate func toJSON() throws -> String { - struct Output: Encodable { - let version = "1.0" // manifest JSON version - let package: Package - } - - let encoder = JSONEncoder() - encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] - let data = try encoder.encode(Output(package: self)) - return String(data: data, encoding: .utf8)! - } -} - -private enum PackageSerializerErrors: Error { - case unknownPackage - case invalidArguments - case invalidFileHandle - case failedOpeningFile -} -``` - -Check https://forums.swift.org/t/pre-pitch-swiftpm-manifest-based-on-result-builders/53457/5 -to remove top level class - -``` -import PackageManifest - -Package { - ... -} -``` - -``` -// Within PackageManifest module - -public var package: Package? - -@resultBuilder -struct Package { - - @discardableResult - static func buildFinalResult(_ p: Package) -> Package { - package = p - } -} -``` \ No newline at end of file diff --git a/Examples/v1/SAM/samconfig.toml b/Examples/v1/SAM/samconfig.toml deleted file mode 100644 index 802bd0cf..00000000 --- a/Examples/v1/SAM/samconfig.toml +++ /dev/null @@ -1,8 +0,0 @@ -version = 0.1 -[debug] -[debug.deploy] -[debug.deploy.parameters] -stack_name = "swift-aws-lambda-runtime-example" -region = "us-east-1" -capabilities = "CAPABILITY_IAM" -image_repositories = [] \ No newline at end of file diff --git a/Examples/v1/SAM/template.yaml b/Examples/v1/SAM/template.yaml deleted file mode 100644 index f0165937..00000000 --- a/Examples/v1/SAM/template.yaml +++ /dev/null @@ -1,68 +0,0 @@ -Resources: - HttpApiLambda: - Type: AWS::Serverless::Function - Properties: - Events: - HttpApiEvent: - Type: HttpApi - Runtime: provided.al2 - Architectures: - - arm64 - Environment: - Variables: - NAME2: VALUE2 - LOG_LEVEL: debug - NAME1: VALUE1 - Handler: Provided - CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HttpApiLambda/HttpApiLambda.zip - Description: Description of this function - QueueSwiftLambdaQueueName: - Type: AWS::SQS::Queue - Properties: - QueueName: swift-lambda-queue-name - UrlLambda: - Type: AWS::Serverless::Function - Properties: - FunctionUrlConfig: - AuthType: AWS_IAM - Architectures: - - arm64 - Description: A Lambda function that is directly exposed as an URL, with IAM authentication - CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/UrlLambda/UrlLambda.zip - Runtime: provided.al2 - Handler: Provided - TopLevelQueueResource: - Type: AWS::SQS::Queue - Properties: - QueueName: swift-lambda-top-level-queue - SQSLambda: - Type: AWS::Serverless::Function - Properties: - Runtime: provided.al2 - Environment: - Variables: - LOG_LEVEL: debug - Architectures: - - arm64 - Events: - SQSEvent: - Type: SQS - Properties: - Queue: - Fn::GetAtt: - - QueueSwiftLambdaQueueName - - Arn - Enabled: true - BatchSize: 10 - Handler: Provided - CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SQSLambda/SQSLambda.zip - SwiftLambdaTable: - Type: AWS::Serverless::SimpleTable - Properties: - TableName: swift-lambda-table - PrimaryKey: - Type: String - Name: id -Transform: AWS::Serverless-2016-10-31 -AWSTemplateFormatVersion: 2010-09-09 -Description: Description of this deployment descriptor diff --git a/Examples/v1/Testing/Package.swift b/Examples/v1/Testing/Package.swift deleted file mode 100644 index 587c9279..00000000 --- a/Examples/v1/Testing/Package.swift +++ /dev/null @@ -1,36 +0,0 @@ -// swift-tools-version:5.7 - -import PackageDescription - -import class Foundation.ProcessInfo // needed for CI to test the local version of the library - -let package = Package( - name: "swift-aws-lambda-runtime-example", - platforms: [ - .macOS(.v12) - ], - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]) - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha") - ], - 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"), - ] -) - -// for CI to test the local version of the library -if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil { - package.dependencies = [ - .package(name: "swift-aws-lambda-runtime", path: "../..") - ] -} diff --git a/Examples/v1/Testing/Sources/Lambda.swift b/Examples/v1/Testing/Sources/Lambda.swift deleted file mode 100644 index 568710e4..00000000 --- a/Examples/v1/Testing/Sources/Lambda.swift +++ /dev/null @@ -1,25 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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: SimpleLambdaHandler { - func handle(_ event: String, context: LambdaContext) async throws -> String { - // as an example, respond with the event's reversed body - String(event.reversed()) - } -} diff --git a/Examples/v1/Testing/Tests/LambdaTests.swift b/Examples/v1/Testing/Tests/LambdaTests.swift deleted file mode 100644 index cce0ad34..00000000 --- a/Examples/v1/Testing/Tests/LambdaTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -import AWSLambdaTesting -import XCTest - -@testable import MyLambda - -class LambdaTest: XCTestCase { - func testIt() async throws { - let input = UUID().uuidString - let result = try await Lambda.test(MyLambda.self, with: input) - XCTAssertEqual(result, String(input.reversed())) - } -} From dadc879c89d38fae7db3818cc164c39f11a21e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Tue, 8 Oct 2024 10:52:55 +0200 Subject: [PATCH 10/10] swift format --- Examples/APIGateway/Sources/main.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Examples/APIGateway/Sources/main.swift b/Examples/APIGateway/Sources/main.swift index 0988a7d2..2d5707d6 100644 --- a/Examples/APIGateway/Sources/main.swift +++ b/Examples/APIGateway/Sources/main.swift @@ -14,6 +14,7 @@ import AWSLambdaEvents import AWSLambdaRuntime + #if canImport(FoundationEssentials) import FoundationEssentials #else