diff --git a/.gitignore b/.gitignore index aae9255..a1eee32 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ lambda.zip swift-shared-libs default.profraw +.swiftpm diff --git a/.travis.yml b/.travis.yml index 91623ab..afa063f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,48 +24,34 @@ addons: before_install: - ./scripts/aws_travis_credentials.sh +global: + - DOCKER_OS=amazonlinux2 + - SWIFT_VERSION=5.2.4-amazonlinux2 + - LAYER_VERSION=5.2.4-amazonlinux2 + install: - make --version - make docker_build - -script: - docker image ls - make package_layer - make package_lambda - - make swift_test - - make swift_test SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make swift_test SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make build_lambda_local SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld - - make start_docker_compose_env SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld - - make invoke_lambda_local SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld - - make stop_docker_compose_env SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld - - make test_lambda_local_output SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld - - make build_lambda_local SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make start_docker_compose_env SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make invoke_lambda_local SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make stop_docker_compose_env SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make test_lambda_local_output SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps - - make build_lambda_local SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make start_docker_compose_env SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make invoke_lambda_local SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make stop_docker_compose_env SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make test_lambda_local_output SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject - - make build_lambda_local SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet - - make start_docker_compose_env SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet - - make invoke_lambda_local SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet - - make stop_docker_compose_env SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet - - make test_lambda_local_output SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet - - make build_lambda_local SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query - - make start_docker_compose_env SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query - - make invoke_lambda_local SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query - - make stop_docker_compose_env SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query - - make test_lambda_local_output SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query +env: + - SWIFT_EXECUTABLE=HelloWorld SWIFT_PROJECT_PATH=Examples/HelloWorld LAMBDA_FUNCTION_NAME=HelloWorld LAMBDA_HANDLER=HelloWorld.helloWorld + - SWIFT_EXECUTABLE=HTTPSRequest SWIFT_PROJECT_PATH=Examples/HTTPSRequest LAMBDA_FUNCTION_NAME=HTTPSRequest LAMBDA_HANDLER=HTTPSRequest.getHttps + - SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject + - SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet + - SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query + +script: + - make swift_test + - make run_test_example + deploy: provider: releases api_key: secure: Ddgpz/LsTWeTTr8Xm0br+Vs3Is58V2bOHthwLOt5a1TbyhWAgU+tyc6lwvd51mhQxeNNVPSOWLKC4QgJEp2KI2ZCaw0K+Czb8lqMzWF5eJggDmJ0+wOZIHVEWMO7smLTwDc1jWtdnQA3qkPtXgSw0ERcJ9z4/haz1Q8v7Jj5qfoBJiNxnYYvw/79ElqROIjtQ0asX/i6xhZSlBFO1dXo5QPzX7XeNREQmh2dCkjfoeYSFSCah4Tnn5akxX36Pef1DJvWdFvc623pWAUoCpeswIMW/htby5tJQ6+C9PK+2yzqomKfU9gTK9iv7Nt3X1LN3KsXpk6VHgtljiUkEsecXjYB5WVBT00mY/4bnTPyY6RjI/V6C0aGnTE+0Pj78kDofJKj5uNevFi4vlBI2g4s4G0mRASFRp+sk3ASmzl62Lohkl1LfNiPYKRKRA2jT81s79zBnYv/MQ7t3fY+yd7iP+NrJfDFR/XG0vxBOQrNRpq6LR0M7DeHgTD/d9an2WEqJlX9pp+Ji/ywwepW7b/NLv42xeN99SnHbDmJJ/eZ4P1IC37JCOT5CjJ9wIyP8c+Q89JT30nlvON9xPM8MOx/MrVAGCeqC6FW1Zjc6oakTRZdngMqN1acH5OosLX83cOjjXo8ivRt5Sm2tfX3ZuOgL98kdaYsIBPIYbYhZDODdvM= - file: ".build/layer/swift-lambda-runtime-5-1-5.zip" + file: ".build/layer/swift-lambda-runtime-${TRAVIS_TAG}-@{DOCKER_OS}.zip" skip_cleanup: true on: tags: true \ No newline at end of file diff --git a/Examples/HTTPSRequest/docker-compose.yml b/Examples/HTTPSRequest/docker-compose.yml index ad9a8ae..c1206e7 100644 --- a/Examples/HTTPSRequest/docker-compose.yml +++ b/Examples/HTTPSRequest/docker-compose.yml @@ -4,8 +4,8 @@ services: image: "lambci/lambda:provided" volumes: - ../../.build/local:/var/task:ro,delegated - - ../../bootstrap:/opt/bootstrap:ro,delegated - - ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated + - ../../swift-shared-libs/bootstrap:/opt/bootstrap:ro,delegated + - ../../swift-shared-libs:/opt/:ro,delegated environment: - DOCKER_LAMBDA_STAY_OPEN=1 - AWS_LAMBDA_FUNCTION_TIMEOUT=10 diff --git a/Examples/HelloWorld/docker-compose.yml b/Examples/HelloWorld/docker-compose.yml index 67ea35b..dfd1478 100644 --- a/Examples/HelloWorld/docker-compose.yml +++ b/Examples/HelloWorld/docker-compose.yml @@ -4,8 +4,8 @@ services: image: "lambci/lambda:provided" volumes: - ../../.build/local:/var/task:ro,delegated - - ../../bootstrap:/opt/bootstrap:ro,delegated - - ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated + - ../../swift-shared-libs/bootstrap:/opt/bootstrap:ro,delegated + - ../../swift-shared-libs:/opt/:ro,delegated environment: - DOCKER_LAMBDA_STAY_OPEN=1 - AWS_LAMBDA_FUNCTION_TIMEOUT=10 diff --git a/Examples/PostgreSQLDemo/Package.resolved b/Examples/PostgreSQLDemo/Package.resolved new file mode 100644 index 0000000..6b466b0 --- /dev/null +++ b/Examples/PostgreSQLDemo/Package.resolved @@ -0,0 +1,97 @@ +{ + "object": { + "pins": [ + { + "package": "async-http-client", + "repositoryURL": "https://github.com/swift-server/async-http-client.git", + "state": { + "branch": null, + "revision": "037b70291941fe43de668066eb6fb802c5e181d2", + "version": "1.1.1" + } + }, + { + "package": "LambdaSwiftSprinter", + "repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core", + "state": { + "branch": null, + "revision": "6941e41194bde0e57fb0deb23be620f2b8dd626d", + "version": "1.0.0" + } + }, + { + "package": "LambdaSwiftSprinterNioPlugin", + "repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", + "state": { + "branch": null, + "revision": "35d3ab84bf83fc881844b5f7483cb2a1d942a23f", + "version": "1.0.1" + } + }, + { + "package": "postgres-nio", + "repositoryURL": "https://github.com/vapor/postgres-nio.git", + "state": { + "branch": null, + "revision": "a528171a580b56fc38fd505bdb7d5de73e68a69a", + "version": "1.0.0-rc.2" + } + }, + { + "package": "swift-crypto", + "repositoryURL": "https://github.com/apple/swift-crypto.git", + "state": { + "branch": null, + "revision": "d67ac68d09a95443303e9d6e37b34e7ba101d5f1", + "version": "1.0.1" + } + }, + { + "package": "swift-log", + "repositoryURL": "https://github.com/apple/swift-log.git", + "state": { + "branch": null, + "revision": "74d7b91ceebc85daf387ebb206003f78813f71aa", + "version": "1.2.0" + } + }, + { + "package": "swift-metrics", + "repositoryURL": "https://github.com/apple/swift-metrics.git", + "state": { + "branch": null, + "revision": "708b960b4605abb20bc55d65abf6bad607252200", + "version": "2.0.0" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio.git", + "state": { + "branch": null, + "revision": "e876fb37410e0036b98b5361bb18e6854739572b", + "version": "2.16.0" + } + }, + { + "package": "swift-nio-extras", + "repositoryURL": "https://github.com/apple/swift-nio-extras.git", + "state": { + "branch": null, + "revision": "b4dbfacff47fb8d0f9e0a422d8d37935a9f10570", + "version": "1.4.0" + } + }, + { + "package": "swift-nio-ssl", + "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", + "state": { + "branch": null, + "revision": "ae213938e151964aa691f0e902462fbe06baeeb6", + "version": "2.7.1" + } + } + ] + }, + "version": 1 +} diff --git a/Examples/PostgreSQLDemo/Tests/PostgreSQLDemoTests/PostgreSQLDemoTests.swift b/Examples/PostgreSQLDemo/Tests/PostgreSQLDemoTests/PostgreSQLDemoTests.swift index 67fa1aa..cb3cdcf 100644 --- a/Examples/PostgreSQLDemo/Tests/PostgreSQLDemoTests/PostgreSQLDemoTests.swift +++ b/Examples/PostgreSQLDemo/Tests/PostgreSQLDemoTests/PostgreSQLDemoTests.swift @@ -25,8 +25,9 @@ final class PostgreSQLDemoTests: XCTestCase { let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8) + let isError = output?.contains("unknown(host: \"postgres\", port: 5432)\n") - XCTAssertEqual(output, "Hello, world!\n") + XCTAssertEqual(isError, true) } /// Returns path to the built products directory. diff --git a/Examples/PostgreSQLDemo/docker-compose.yml b/Examples/PostgreSQLDemo/docker-compose.yml index aa81eef..b4136a8 100644 --- a/Examples/PostgreSQLDemo/docker-compose.yml +++ b/Examples/PostgreSQLDemo/docker-compose.yml @@ -4,8 +4,8 @@ services: image: "lambci/lambda:provided" volumes: - ../../.build/local:/var/task:ro,delegated - - ../../bootstrap:/opt/bootstrap:ro,delegated - - ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated + - ../../swift-shared-libs/bootstrap:/opt/bootstrap:ro,delegated + - ../../swift-shared-libs:/opt/:ro,delegated environment: - DOCKER_LAMBDA_STAY_OPEN=1 - AWS_LAMBDA_FUNCTION_TIMEOUT=10 diff --git a/Examples/RedisDemo/Tests/RedisDemoTests/RedisDemoTests.swift b/Examples/RedisDemo/Tests/RedisDemoTests/RedisDemoTests.swift index 9d14b65..946b705 100644 --- a/Examples/RedisDemo/Tests/RedisDemoTests/RedisDemoTests.swift +++ b/Examples/RedisDemo/Tests/RedisDemoTests/RedisDemoTests.swift @@ -12,7 +12,7 @@ final class RedisDemoTests: XCTestCase { return } - let fooBinary = productsDirectory.appendingPathComponent("aws-lambda-swift-sprinter-redis") + let fooBinary = productsDirectory.appendingPathComponent("RedisDemo") let process = Process() process.executableURL = fooBinary @@ -26,7 +26,9 @@ final class RedisDemoTests: XCTestCase { let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8) - XCTAssertEqual(output, "Hello, world!\n") + let isError = output?.contains("missingEnvironmentVariables(LambdaSwiftSprinter.Context.AWSEnvironmentKey.lambdaRuntimeApi)") + + XCTAssertEqual(isError, true) } /// Returns path to the built products directory. diff --git a/Examples/RedisDemo/docker-compose.yml b/Examples/RedisDemo/docker-compose.yml index 075ddde..5ab9ce7 100644 --- a/Examples/RedisDemo/docker-compose.yml +++ b/Examples/RedisDemo/docker-compose.yml @@ -4,8 +4,8 @@ services: image: "lambci/lambda:provided" volumes: - ../../.build/local:/var/task:ro,delegated - - ../../bootstrap:/opt/bootstrap:ro,delegated - - ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated + - ../../swift-shared-libs/bootstrap:/opt/bootstrap:ro,delegated + - ../../swift-shared-libs:/opt/:ro,delegated environment: - DOCKER_LAMBDA_STAY_OPEN=1 - AWS_LAMBDA_FUNCTION_TIMEOUT=10 diff --git a/Examples/S3Test/Package.resolved b/Examples/S3Test/Package.resolved new file mode 100644 index 0000000..6e6f42c --- /dev/null +++ b/Examples/S3Test/Package.resolved @@ -0,0 +1,106 @@ +{ + "object": { + "pins": [ + { + "package": "async-http-client", + "repositoryURL": "https://github.com/swift-server/async-http-client.git", + "state": { + "branch": null, + "revision": "037b70291941fe43de668066eb6fb802c5e181d2", + "version": "1.1.1" + } + }, + { + "package": "LambdaSwiftSprinter", + "repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core", + "state": { + "branch": null, + "revision": "6941e41194bde0e57fb0deb23be620f2b8dd626d", + "version": "1.0.0" + } + }, + { + "package": "LambdaSwiftSprinterNioPlugin", + "repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", + "state": { + "branch": null, + "revision": "35d3ab84bf83fc881844b5f7483cb2a1d942a23f", + "version": "1.0.1" + } + }, + { + "package": "aws-sdk-swift", + "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git", + "state": { + "branch": null, + "revision": "eeb30fac3d95a377d8c5bb4b33efed932061f42d", + "version": "5.0.0-alpha.3" + } + }, + { + "package": "aws-sdk-swift-core", + "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift-core.git", + "state": { + "branch": null, + "revision": "ccea4b98758e9e8bb907a4dc9bb15d01d8a3c736", + "version": "5.0.0-alpha.3.2" + } + }, + { + "package": "swift-crypto", + "repositoryURL": "https://github.com/apple/swift-crypto.git", + "state": { + "branch": null, + "revision": "d67ac68d09a95443303e9d6e37b34e7ba101d5f1", + "version": "1.0.1" + } + }, + { + "package": "swift-log", + "repositoryURL": "https://github.com/apple/swift-log.git", + "state": { + "branch": null, + "revision": "74d7b91ceebc85daf387ebb206003f78813f71aa", + "version": "1.2.0" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio.git", + "state": { + "branch": null, + "revision": "40bdad80882d307abe2c0bb36cf3bd4d3e03fe04", + "version": "2.16.1" + } + }, + { + "package": "swift-nio-extras", + "repositoryURL": "https://github.com/apple/swift-nio-extras.git", + "state": { + "branch": null, + "revision": "e0e76733600a2806b3dc4feae8cfebace858c1a2", + "version": "1.4.1" + } + }, + { + "package": "swift-nio-ssl", + "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", + "state": { + "branch": null, + "revision": "ae213938e151964aa691f0e902462fbe06baeeb6", + "version": "2.7.1" + } + }, + { + "package": "swift-nio-transport-services", + "repositoryURL": "https://github.com/apple/swift-nio-transport-services.git", + "state": { + "branch": null, + "revision": "85a67aea7caf5396ed599543dd23cffeb6dbbf96", + "version": "1.5.1" + } + } + ] + }, + "version": 1 +} diff --git a/Examples/S3Test/Package.swift b/Examples/S3Test/Package.swift index 89abe23..81163c1 100644 --- a/Examples/S3Test/Package.swift +++ b/Examples/S3Test/Package.swift @@ -1,9 +1,12 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.1 import PackageDescription let package = Package( name: "S3Test", + products: [ + .executable(name: "S3Test", targets: ["S3Test"]) + ], dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), @@ -11,7 +14,7 @@ let package = Package( //.package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core", from: "1.0.0-alpha.3"), //.package(path: "../../../aws-lambda-swift-sprinter-nio-plugin"), .package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", from: "1.0.0"), - .package(url: "https://github.com/swift-aws/aws-sdk-swift.git", from: "4.0.0"), + .package(url: "https://github.com/swift-aws/aws-sdk-swift.git", from: "5.0.0-alpha.3"), .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"), ], targets: [ @@ -19,7 +22,11 @@ let package = Package( // Targets can depend on other targets in this package, and on products in packages which this package depends on. .target( name: "S3Test", - dependencies: ["LambdaSwiftSprinterNioPlugin", "S3", "Logging"] + dependencies: [ + "LambdaSwiftSprinterNioPlugin", + .product(name: "AWSS3", package: "aws-sdk-swift"), + "Logging" + ] ), .testTarget( name: "S3TestTests", diff --git a/Examples/S3Test/README.md b/Examples/S3Test/README.md index 0ace07d..3e38d27 100644 --- a/Examples/S3Test/README.md +++ b/Examples/S3Test/README.md @@ -22,9 +22,10 @@ import Foundation #endif import LambdaSwiftSprinterNioPlugin import Logging -import S3 +import AWSS3 import NIO import NIOFoundationCompat +import AWSSDKSwiftCore struct Bucket: Codable { let name: String @@ -39,7 +40,10 @@ struct Response: Codable { Change the region with the region of your S3 bucket. ```swift -let s3 = S3(region: .euwest1) +guard let awsClient: AWSHTTPClient = httpClient as? AWSHTTPClient else { + preconditionFailure() +} +let s3 = S3(region: .euwest1, httpClientProvider: .shared(awsClient)) ``` add a logger: diff --git a/Examples/S3Test/Sources/S3Test/main.swift b/Examples/S3Test/Sources/S3Test/main.swift index 7d9aff9..66a90c2 100644 --- a/Examples/S3Test/Sources/S3Test/main.swift +++ b/Examples/S3Test/Sources/S3Test/main.swift @@ -18,7 +18,7 @@ import Foundation #endif import LambdaSwiftSprinterNioPlugin import Logging -import S3 +import AWSS3 import NIO import NIOFoundationCompat import AWSSDKSwiftCore @@ -36,18 +36,22 @@ let logger = Logger(label: "AWS.Lambda.S3Test") var s3: S3! +guard let awsClient: AWSHTTPClient = httpClient as? AWSHTTPClient else { + preconditionFailure() +} + if ProcessInfo.processInfo.environment["LAMB_CI_EXEC"] == "1" { //Used for local test s3 = S3(region: .useast1, endpoint: "http://localstack:4572") logger.info("Endpoint-URI: http://localstack:4572") -} else if let awsRegion = ProcessInfo.processInfo.environment["AWS_REGION"], +} else if let awsRegion = ProcessInfo.processInfo.environment["AWS_REGION"] { //The S3 Bucket must be in the same region of the Lambda - let region = Region(rawValue: awsRegion) { - s3 = S3(region: region) + let region = Region(rawValue: awsRegion) + s3 = S3(region: region, httpClientProvider: .shared(awsClient)) logger.info("AWS_REGION: \(region)") } else { //Default configuration - s3 = S3(region: .useast1) + s3 = S3(region: .useast1, httpClientProvider: .shared(awsClient)) logger.info("AWS_REGION: us-east-1") } diff --git a/Examples/S3Test/docker-compose.yml b/Examples/S3Test/docker-compose.yml index 56422bb..0339620 100644 --- a/Examples/S3Test/docker-compose.yml +++ b/Examples/S3Test/docker-compose.yml @@ -4,8 +4,8 @@ services: image: "lambci/lambda:provided" volumes: - ../../.build/local:/var/task:ro,delegated - - ../../bootstrap:/opt/bootstrap:ro,delegated - - ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated + - ../../swift-shared-libs/bootstrap:/opt/bootstrap:ro,delegated + - ../../swift-shared-libs:/opt/:ro,delegated environment: - DOCKER_LAMBDA_STAY_OPEN=1 - AWS_LAMBDA_FUNCTION_TIMEOUT=10 diff --git a/Makefile b/Makefile index 078101b..43402fe 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ - # Use this tag to build a customized local image -SWIFT_VERSION?=5.2.3 -LAYER_VERSION?=5-2-3 -# SWIFT_VERSION=5.1.5 -# LAYER_VERSION=5-1-5 +SWIFT_VERSION?=5.2.4-amazonlinux2 +LAYER_VERSION?=5.2.4-amazonlinux2 +DOCKER_OS?=amazonlinux2 +# SWIFT_VERSION=5.2.3-bionic +# LAYER_VERSION=5-2-3-bionic +# DOCKER_OS=bionic DOCKER_TAG=nio-swift:$(SWIFT_VERSION) SWIFT_DOCKER_IMAGE=$(DOCKER_TAG) SWIFT_LAMBDA_LIBRARY=nio-swift-lambda-runtime-$(LAYER_VERSION) @@ -58,6 +59,8 @@ LOCAL_LAMBDA_PATH=$(ROOT_BUILD_PATH)/local LOCALSTACK_TMP=$(ROOT_BUILD_PATH)/.tmp TMP_BUILD_PATH=$(ROOT_BUILD_PATH)/tmp DATETIME=$(shell date +'%y%m%d-%H%M%S') +DOCKER_FOLDER=docker +BOOTSTRAP=$(DOCKER_FOLDER)/$(SWIFT_VERSION)/bootstrap # use this for local development MOUNT_ROOT=$(shell pwd)/.. @@ -145,19 +148,22 @@ clean_layer: package_layer: create_build_directory clean_layer $(eval SHARED_LIBRARIES := $(shell cat docker/$(SWIFT_VERSION)/swift-shared-libraries.txt | tr '\n' ' ')) mkdir -p $(SHARED_LIBS_FOLDER)/lib +ifeq '$(DOCKER_OS)' 'xenial' docker run \ --rm \ --volume "$(shell pwd)/:/src" \ --workdir "/src" \ $(SWIFT_DOCKER_IMAGE) \ cp /lib64/ld-linux-x86-64.so.2 $(SHARED_LIBS_FOLDER) +endif docker run \ --rm \ --volume "$(shell pwd)/:/src" \ --workdir "/src" \ $(SWIFT_DOCKER_IMAGE) \ cp -t $(SHARED_LIBS_FOLDER)/lib $(SHARED_LIBRARIES) - zip -r $(LAYER_BUILD_PATH)/$(LAYER_ZIP) bootstrap $(SHARED_LIBS_FOLDER) + cp $(BOOTSTRAP) $(SHARED_LIBS_FOLDER) + cd $(SHARED_LIBS_FOLDER); pwd; zip -r ../$(LAYER_BUILD_PATH)/$(LAYER_ZIP) bootstrap lib upload_build_to_s3: create_lambda_s3_key aws s3 sync --acl public-read "$(LAMBDA_BUILD_PATH)" s3://$(AWS_BUCKET)/$(LAMBDA_S3_UPLOAD_PATH) --profile $(AWS_PROFILE) @@ -230,8 +236,8 @@ invoke_lambda_local_once: $(eval LOCAL_LAMBDA_EVENT := '$(shell cat $(SWIFT_PROJECT_PATH)/event.json)') docker run --rm \ -v "$(PWD)/$(LOCAL_LAMBDA_PATH)":/var/task:ro,delegated \ - -v "$(PWD)/bootstrap":/opt/bootstrap:ro,delegated \ - -v "$(PWD)/$(SHARED_LIBS_FOLDER)":/opt/swift-shared-libs:ro,delegated \ + -v "$(PWD)/$(BOOTSTRAP)":/opt/bootstrap:ro,delegated \ + -v "$(PWD)/$(SHARED_LIBS_FOLDER)":/opt:ro,delegated \ lambci/lambda:provided $(LAMBDA_HANDLER) $(LOCAL_LAMBDA_EVENT) start_lambda_local_env: @@ -239,8 +245,8 @@ start_lambda_local_env: -e DOCKER_LAMBDA_STAY_OPEN=1 \ -p 9001:9001 \ -v "$(PWD)/$(LOCAL_LAMBDA_PATH)":/var/task:ro,delegated \ - -v "$(PWD)/bootstrap":/opt/bootstrap:ro,delegated \ - -v "$(PWD)/$(SHARED_LIBS_FOLDER)":/opt/swift-shared-libs:ro,delegated \ + -v "$(PWD)/$(BOOTSTRAP)":/opt/bootstrap:ro,delegated \ + -v "$(PWD)/$(SHARED_LIBS_FOLDER)":/opt/:ro,delegated \ lambci/lambda:provided \ $(LAMBDA_HANDLER) @@ -255,4 +261,6 @@ stop_docker_compose_env: make -f $(SWIFT_PROJECT_PATH)/Makefile stop_docker_compose_env test_lambda_local_output: - cmp $(TMP_BUILD_PATH)/outfile $(SWIFT_PROJECT_PATH)/outfile.json \ No newline at end of file + cmp $(TMP_BUILD_PATH)/outfile $(SWIFT_PROJECT_PATH)/outfile.json + +run_test_example: build_lambda_local start_docker_compose_env invoke_lambda_local stop_docker_compose_env test_lambda_local_output \ No newline at end of file diff --git a/README.md b/README.md index c97d264..82ef84c 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,11 @@ # aws-lambda-swift-sprinter -[![Swift 5](https://img.shields.io/badge/Swift-5.0-blue.svg)](https://swift.org/download/) [![Swift 5.1.5](https://img.shields.io/badge/Swift-5.1.5-blue.svg)](https://swift.org/download/) [![Swift 5.2.3](https://img.shields.io/badge/Swift-5.2.3-blue.svg)](https://swift.org/download/) ![](https://img.shields.io/badge/version-1.0.0-green) ![](https://travis-ci.com/swift-sprinter/aws-lambda-swift-sprinter.svg?branch=master) ![](https://img.shields.io/badge/@AWS-Open-orange.svg) +![](https://img.shields.io/badge/@AWS-Open-orange.svg) [![Swift 5](https://img.shields.io/badge/Swift-5.0-blue.svg)](https://swift.org/download/) [![Swift 5.1.5](https://img.shields.io/badge/Swift-5.1.5-blue.svg)](https://swift.org/download/) [![Swift 5.2.4](https://img.shields.io/badge/Swift-5.2.4-blue.svg)](https://swift.org/download/) ![](https://img.shields.io/badge/version-1.1.0-green) ![](https://travis-ci.com/swift-sprinter/aws-lambda-swift-sprinter.svg?branch=master) ![](https://img.shields.io/badge/docker-amazonlinux2-orange.svg) ![](https://img.shields.io/badge/docker-ubuntu--xenial-pink.svg) ![](./images/aws-lambda-swift-sprinter.png) The goal of this project is to provide an environment to build an [AWS Lambda Custom Runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) for the **Swift** programming language and provide the required support to run [swift-nio 2.0](https://github.com/apple/swift-nio). -The support of **swift-nio 2.0** is crucial to allow HTTPS requests inside the Swift Lambda. - The project helps building a Swift Lambda based on the framework [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core). The project contains also some Examples: @@ -20,9 +18,9 @@ The project contains also some Examples: # Introduction -The AWS Lambdas run on [Amazon Linux](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html). Unfortunately, there is no support at the moment for Swift on Amazon Linux. This means that the Lambda cannot be built using `swift build` inside the Amazon Linux image. +The AWS Lambdas run on [Amazon Linux](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html). -The work-around to build swift on Amazon Linux is achieved by: +To build swift on Amazon Linux is required to: - building the code on the [official Docker Swift](https://hub.docker.com/_/swift/) - extracting the build and all the runtime's shared libraries - packaging the artefacts and use them as [AWS Lambda Custom Runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) @@ -177,6 +175,7 @@ make invoke_lambda \ | AWS_BUCKET | The AWS S3 bucket where the layer and lambdas zip files get uploaded. | aws-lambda-swift-sprinter | | SWIFT_VERSION | Version of Swift used / Matches Dockerfile location too from `docker/` folder. | 5.2.3 | | LAYER_VERSION | Version of the Swift layer that will be created and uploaded for the Lambda to run on. | 5-2-3 | +| DOCKER_OS | amazonlinux2 / xenial | amazonlinux2 | | SWIFT_EXECUTABLE | Name of the binary file. | HelloWorld | | SWIFT_PROJECT_PATH | Path to your Swift project. | Examples/HelloWorld | | LAMBDA_FUNCTION_NAME | Display name of your Lambda in AWS. | HelloWorld | diff --git a/bootstrap b/bootstrap deleted file mode 100755 index 212a2d6..0000000 --- a/bootstrap +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" -/opt/swift-shared-libs/ld-linux-x86-64.so.2 --library-path /opt/swift-shared-libs/lib $EXECUTABLE \ No newline at end of file diff --git a/docker/5.0.3/Dockerfile b/docker/5.0.3-bionic/Dockerfile similarity index 68% rename from docker/5.0.3/Dockerfile rename to docker/5.0.3-bionic/Dockerfile index 0f567bd..e9627e2 100644 --- a/docker/5.0.3/Dockerfile +++ b/docker/5.0.3-bionic/Dockerfile @@ -1,4 +1,4 @@ -FROM swift:5.0.3 as builder +FROM swift:5.0.3-bionic as builder RUN apt-get -qq update && apt-get -q -y install \ libssl-dev libicu-dev \ No newline at end of file diff --git a/docker/5.0.3-bionic/bootstrap b/docker/5.0.3-bionic/bootstrap new file mode 100755 index 0000000..ae0a9f7 --- /dev/null +++ b/docker/5.0.3-bionic/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" +/opt/ld-linux-x86-64.so.2 --library-path /opt/lib $EXECUTABLE \ No newline at end of file diff --git a/docker/5.0.3/swift-shared-libraries.txt b/docker/5.0.3-bionic/swift-shared-libraries.txt similarity index 100% rename from docker/5.0.3/swift-shared-libraries.txt rename to docker/5.0.3-bionic/swift-shared-libraries.txt diff --git a/docker/5.1.5/Dockerfile b/docker/5.1.5-bionic/Dockerfile similarity index 68% rename from docker/5.1.5/Dockerfile rename to docker/5.1.5-bionic/Dockerfile index ff7286f..4bbf737 100644 --- a/docker/5.1.5/Dockerfile +++ b/docker/5.1.5-bionic/Dockerfile @@ -1,4 +1,4 @@ -FROM swift:5.1.5 as builder +FROM swift:5.1.5-bionic as builder RUN apt-get -qq update && apt-get -q -y install \ libssl-dev libicu-dev diff --git a/docker/5.1.5-bionic/bootstrap b/docker/5.1.5-bionic/bootstrap new file mode 100755 index 0000000..ae0a9f7 --- /dev/null +++ b/docker/5.1.5-bionic/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" +/opt/ld-linux-x86-64.so.2 --library-path /opt/lib $EXECUTABLE \ No newline at end of file diff --git a/docker/5.1.5/swift-shared-libraries.txt b/docker/5.1.5-bionic/swift-shared-libraries.txt similarity index 100% rename from docker/5.1.5/swift-shared-libraries.txt rename to docker/5.1.5-bionic/swift-shared-libraries.txt diff --git a/docker/5.2.4-amazonlinux2/Dockerfile b/docker/5.2.4-amazonlinux2/Dockerfile new file mode 100644 index 0000000..9a4e2aa --- /dev/null +++ b/docker/5.2.4-amazonlinux2/Dockerfile @@ -0,0 +1,4 @@ +FROM swift:5.2.4-amazonlinux2 as builder + +RUN yum -y update && \ + yum -y install zlib-devel \ No newline at end of file diff --git a/docker/5.2.4-amazonlinux2/bootstrap b/docker/5.2.4-amazonlinux2/bootstrap new file mode 100755 index 0000000..acdb8f3 --- /dev/null +++ b/docker/5.2.4-amazonlinux2/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" +$EXECUTABLE \ No newline at end of file diff --git a/docker/5.2.4-amazonlinux2/swift-shared-libraries.txt b/docker/5.2.4-amazonlinux2/swift-shared-libraries.txt new file mode 100644 index 0000000..921623e --- /dev/null +++ b/docker/5.2.4-amazonlinux2/swift-shared-libraries.txt @@ -0,0 +1,12 @@ +/usr/lib/swift/linux/libBlocksRuntime.so +/usr/lib/swift/linux/libFoundation.so +/usr/lib/swift/linux/libFoundationNetworking.so +/usr/lib/swift/linux/libFoundationXML.so +/usr/lib/swift/linux/libdispatch.so +/usr/lib/swift/linux/libicudataswift.so.65 +/usr/lib/swift/linux/libicui18nswift.so.65 +/usr/lib/swift/linux/libicuucswift.so.65 +/usr/lib/swift/linux/libswiftCore.so +/usr/lib/swift/linux/libswiftDispatch.so +/usr/lib/swift/linux/libswiftGlibc.so +/usr/lib/swift/linux/libswiftSwiftOnoneSupport.so \ No newline at end of file diff --git a/docker/5.2.3/Dockerfile b/docker/5.2.4-bionic/Dockerfile similarity index 68% rename from docker/5.2.3/Dockerfile rename to docker/5.2.4-bionic/Dockerfile index abca1df..feaaa58 100644 --- a/docker/5.2.3/Dockerfile +++ b/docker/5.2.4-bionic/Dockerfile @@ -1,4 +1,4 @@ -FROM swift:5.2.3 as builder +FROM swift:5.2.4-bionic as builder RUN apt-get -qq update && apt-get -q -y install \ libssl-dev libicu-dev diff --git a/docker/5.2.4-bionic/bootstrap b/docker/5.2.4-bionic/bootstrap new file mode 100755 index 0000000..ae0a9f7 --- /dev/null +++ b/docker/5.2.4-bionic/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" +/opt/ld-linux-x86-64.so.2 --library-path /opt/lib $EXECUTABLE \ No newline at end of file diff --git a/docker/5.2.3/swift-shared-libraries.txt b/docker/5.2.4-bionic/swift-shared-libraries.txt similarity index 100% rename from docker/5.2.3/swift-shared-libraries.txt rename to docker/5.2.4-bionic/swift-shared-libraries.txt diff --git a/docker/nightly-amazonlinux2/Dockerfile b/docker/nightly-amazonlinux2/Dockerfile new file mode 100644 index 0000000..2064b26 --- /dev/null +++ b/docker/nightly-amazonlinux2/Dockerfile @@ -0,0 +1,4 @@ +FROM swiftlang/swift:nightly-amazonlinux2 as builder + +RUN yum -y update && \ + yum -y install zlib-devel \ No newline at end of file diff --git a/docker/nightly-amazonlinux2/bootstrap b/docker/nightly-amazonlinux2/bootstrap new file mode 100755 index 0000000..acdb8f3 --- /dev/null +++ b/docker/nightly-amazonlinux2/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +EXECUTABLE=$LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1)" +$EXECUTABLE \ No newline at end of file diff --git a/docker/nightly-amazonlinux2/swift-shared-libraries.txt b/docker/nightly-amazonlinux2/swift-shared-libraries.txt new file mode 100644 index 0000000..921623e --- /dev/null +++ b/docker/nightly-amazonlinux2/swift-shared-libraries.txt @@ -0,0 +1,12 @@ +/usr/lib/swift/linux/libBlocksRuntime.so +/usr/lib/swift/linux/libFoundation.so +/usr/lib/swift/linux/libFoundationNetworking.so +/usr/lib/swift/linux/libFoundationXML.so +/usr/lib/swift/linux/libdispatch.so +/usr/lib/swift/linux/libicudataswift.so.65 +/usr/lib/swift/linux/libicui18nswift.so.65 +/usr/lib/swift/linux/libicuucswift.so.65 +/usr/lib/swift/linux/libswiftCore.so +/usr/lib/swift/linux/libswiftDispatch.so +/usr/lib/swift/linux/libswiftGlibc.so +/usr/lib/swift/linux/libswiftSwiftOnoneSupport.so \ No newline at end of file