Skip to content

Commit c8fbdb5

Browse files
author
Stormacq, Sebastien
committed
preliminary support for HTTPS and async functions
1 parent 9ded126 commit c8fbdb5

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ let package = Package(
1414
dependencies: [
1515
// Dependencies declare other packages that this package depends on.
1616
// .package(url: /* package url */, from: "1.0.0"),
17-
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.8.0")
18-
],
17+
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.9.0"),
18+
.package(url: "https://github.com/IBM-Swift/SwiftyRequest.git", from: "3.1.0") ],
1919
targets: [
2020
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2121
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2222
.target(
2323
name: "HelloSwiftLambda",
24-
dependencies: ["LambdaRuntime", "HeliumLogger"],
24+
dependencies: ["LambdaRuntime", "HeliumLogger", "SwiftyRequest"],
2525
path: "lambda-function/Sources"
2626
),
2727
.target(

event.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

lambda-function/Sources/handler/main.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ import Foundation
1010
import LambdaRuntime
1111
import LoggerAPI
1212

13-
func handler(context: Context, event: LambdaEvent) throws -> LambdaResponse {
13+
import SwiftyRequest
14+
15+
func handler(context: Context, event: LambdaEvent, completion: @escaping LambdaCallback) throws -> Void {
1416
Log.debug("Starting lambda handler")
1517

16-
return [ "result": event["key1"] ?? "no key1 provided" ]
18+
let request = RestRequest(method: .get, url: "https://httpbin.org/get?value=\(event["key1"] ?? "no key1 provided")")
19+
20+
request.responseString { result in
21+
switch result {
22+
case .success(let response):
23+
print("Success")
24+
completion([ "result": "\(response.body)" ])
25+
case .failure(let error):
26+
print("Failure")
27+
completion([ "result": "\(error)" ])
28+
}
29+
}
30+
31+
return
1732

1833
}
1934

@@ -48,4 +63,4 @@ try LambdaRuntime(handler).run()
4863
// return [ "error": String(resp.statusCode) ]
4964
// }
5065

51-
// }
66+
// }

lambda-runtime/Sources/LambdaRuntime/LambdaRuntime.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ public class LambdaRuntime {
1515

1616
private var context : Context
1717
private let runtimeApi : LambaRuntimeAPI
18-
private let handler : LambdaHandler
18+
private let handler : AsyncLambdaHandler
1919

2020
private let MAX_RETRIES = 3
2121
private var retry = 0
22-
22+
23+
// // support sync handler for ease of programing
24+
// public init(_ handler: SyncLambdaHandler) throws {
25+
// }
26+
2327
// initialize the runtime and load shared libraries, if any
2428
// also takes the custom handler that will be the core of the lambda function
25-
public init(_ handler: @escaping LambdaHandler) throws {
29+
public init(_ handler: @escaping AsyncLambdaHandler) throws {
2630

2731
// initialize logger
2832
let logger = HeliumLogger(.verbose)
@@ -119,11 +123,23 @@ public class LambdaRuntime {
119123

120124
// 3. call handler
121125
do {
122-
let response = try handler(self.context, event)
123-
Log.debug("Handler returned : \(response)")
126+
var response: LambdaResponse?
127+
let dispatchGroup = DispatchGroup()
128+
dispatchGroup.enter()
129+
do {
130+
try handler(self.context, event, { (result) in
131+
response = result
132+
Log.debug("Handler returned : \(String(describing: response))")
133+
dispatchGroup.leave()
134+
})
135+
} catch {
136+
response = ["error" : "\(error)"]
137+
dispatchGroup.leave()
138+
}
139+
dispatchGroup.wait()
124140

125141
// 4. call success or error
126-
try runtimeApi.invocationSuccess(awsRequestId: awsRequestId, response: response)
142+
try runtimeApi.invocationSuccess(awsRequestId: awsRequestId, response: response!)
127143
} catch {
128144
Log.debug("An error occured in the handler or when signaling the success : \(error)")
129145
runtimeApi.invocationError(awsRequestId: awsRequestId, error: error)

lambda-runtime/Sources/LambdaRuntime/LambdaStructure.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public typealias LambdaResponse = [String:Any]
2121
//
2222
// Lambda handler function
2323
//
24-
public typealias LambdaHandler = (Context, LambdaEvent) throws -> LambdaResponse
24+
public typealias LambdaCallback = (LambdaResponse) -> Void
25+
public typealias AsyncLambdaHandler = (Context, LambdaEvent, @escaping LambdaCallback) throws -> Void
26+
//public typealias SyncLambdaHandler = (Context, LambdaEvent) throws -> LambdaResponse
2527

2628
// this is the Context object passed to the handler.
2729
// constant values are identical accross invocation and are initialized from the container env variables

shell-scripts/package_function.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ then
2323
fi
2424

2525
echo "Building"
26-
docker run -it --rm -v $(pwd):/$PROJECT_DIR --env PROJECT_DIR=/$PROJECT_DIR swift /bin/bash -c "cd $PROJECT_DIR && swift build -c $BUILD_TYPE"
26+
# TODO - should install zlib in a custom docker image
27+
docker run -it --rm -v $(pwd):/$PROJECT_DIR --env PROJECT_DIR=/$PROJECT_DIR swift /bin/bash -c "apt-get update && apt-get install -y zlib1g-dev && cd $PROJECT_DIR && swift build -c $BUILD_TYPE"
2728

2829
echo "Packaging"
2930
mkdir $LAMBDA_DIR 2>/dev/null # create the directory, silently fails when it already exists
@@ -36,7 +37,7 @@ zip $LAMBDA_ZIP bootstrap $EXECUTABLE_NAME
3637
popd >/dev/null
3738

3839
# create lambda function if it does not exist
39-
echo "Deploying to AWS (creating IAM role and function as needed)"
40+
echo 'Deploying to AWS (creating IAM role and function as needed)'
4041
IAM_ROLE_ARN=$(aws iam list-roles --query "Roles[? RoleName == '$IAM_ROLE_NAME'].Arn" --output text)
4142
if [ -z "$IAM_ROLE_ARN" ];
4243
then

0 commit comments

Comments
 (0)